@@ -11,7 +11,7 @@ import * as Schema from "effect/Schema"; |
11 | 11 | import * as Sink from "effect/Sink"; |
12 | 12 | import * as Stream from "effect/Stream"; |
13 | 13 | import type * as Types from "effect/Types"; |
14 | | -import { AiError, McpProtocol, McpSchema, McpServer, Tool } from "effect/ai"; |
| 14 | +import { AiError, McpProtocol, McpSchema, McpServer, Tool, type Toolkit } from "effect/ai"; |
15 | 15 | import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http"; |
16 | 16 | import { OrchestratorMcpFailure, PreviewAutomationError } from "@t3tools/contracts"; |
17 | 17 |
|
@@ -20,7 +20,9 @@ import * as ServerConfig from "../config.ts"; |
20 | 20 | import * as DeviceService from "../device/DeviceService.ts"; |
21 | 21 | import * as HtmlRender from "../htmlRender/HtmlRender.ts"; |
22 | 22 | import * as PreviewBrowser from "../htmlRender/PreviewBrowser.ts"; |
| 23 | +import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts"; |
23 | 24 | import * as McpInvocationContext from "./McpInvocationContext.ts"; |
| 25 | +import * as McpToolAccess from "./McpToolAccess.ts"; |
24 | 26 | import * as OrchestratorMcpService from "./OrchestratorMcpService.ts"; |
25 | 27 | import { PreviewControlsToolkit } from "./toolkits/previewControls/tools.ts"; |
26 | 28 | import * as PreviewControlsHandlers from "./toolkits/previewControls/handlers.ts"; |
@@ -385,6 +387,7 @@ const previewSnapshotFailure = <E>(cause: Cause.Cause<E>) => { |
385 | 387 | const registerPreviewSnapshot = Effect.fn("McpHttpServer.registerPreviewSnapshot")(function* () { |
386 | 388 | const server = yield* McpServer.McpServer; |
387 | 389 | const broker = yield* PreviewAutomationBroker.PreviewAutomationBroker; |
| 390 | + const threads = yield* ThreadManagementService.ThreadManagementService; |
388 | 391 | // The MCP tool runner only supplies the client, so hand the save path its services here. |
389 | 392 | const saveServices = yield* Effect.context< |
390 | 393 | ServerConfig.ServerConfig | FileSystem.FileSystem | Path.Path |
@@ -419,6 +422,7 @@ const registerPreviewSnapshot = Effect.fn("McpHttpServer.registerPreviewSnapshot |
419 | 422 | Stream.run(Sink.last()), |
420 | 423 | Effect.flatMap(Effect.fromOption), |
421 | 424 | Effect.provideService(PreviewAutomationBroker.PreviewAutomationBroker, broker), |
| 425 | + Effect.provideService(ThreadManagementService.ThreadManagementService, threads), |
422 | 426 | Effect.provideService(McpInvocationContext.McpInvocationContext, invocation), |
423 | 427 | Effect.flatMap(({ encodedResult }) => |
424 | 428 | Effect.gen(function* () { |
@@ -644,14 +648,19 @@ const registerImageTool = <T extends Tool.Any, E, R>( |
644 | 648 |
|
645 | 649 | const registerDeviceScreenshot = Effect.fn("McpHttpServer.registerDeviceScreenshot")(function* () { |
646 | 650 | const devices = yield* DeviceService.DeviceService; |
| 651 | + const threads = yield* ThreadManagementService.ThreadManagementService; |
647 | 652 | const built = yield* DeviceScreenshotToolkit; |
648 | 653 | yield* registerImageTool( |
649 | 654 | DeviceScreenshotTool, |
650 | 655 | (payload) => |
651 | 656 | built |
652 | 657 | .handle("device_screenshot", payload) |
653 | 658 | .pipe(Stream.unwrap, Stream.run(Sink.last()), Effect.flatMap(Effect.fromOption)), |
654 | | - (effect) => effect.pipe(Effect.provideService(DeviceService.DeviceService, devices)), |
| 659 | + (effect) => |
| 660 | + effect.pipe( |
| 661 | + Effect.provideService(DeviceService.DeviceService, devices), |
| 662 | + Effect.provideService(ThreadManagementService.ThreadManagementService, threads), |
| 663 | + ), |
655 | 664 | "screenshot", |
656 | 665 | "Device screenshot failed.", |
657 | 666 | ); |
@@ -661,14 +670,19 @@ const isOrchestratorMcpFailure = Schema.is(OrchestratorMcpFailure); |
661 | 670 |
|
662 | 671 | const registerHtmlPreview = Effect.fn("McpHttpServer.registerHtmlPreview")(function* () { |
663 | 672 | const htmlRender = yield* HtmlRender.HtmlRender; |
| 673 | + const threads = yield* ThreadManagementService.ThreadManagementService; |
664 | 674 | const built = yield* HtmlPreviewToolkit; |
665 | 675 | yield* registerImageTool( |
666 | 676 | HtmlPreviewTool, |
667 | 677 | (payload) => |
668 | 678 | built |
669 | 679 | .handle("html_preview", payload) |
670 | 680 | .pipe(Stream.unwrap, Stream.run(Sink.last()), Effect.flatMap(Effect.fromOption)), |
671 | | - (effect) => effect.pipe(Effect.provideService(HtmlRender.HtmlRender, htmlRender)), |
| 681 | + (effect) => |
| 682 | + effect.pipe( |
| 683 | + Effect.provideService(HtmlRender.HtmlRender, htmlRender), |
| 684 | + Effect.provideService(ThreadManagementService.ThreadManagementService, threads), |
| 685 | + ), |
672 | 686 | "preview", |
673 | 687 | // Parameter errors and HTML render errors are both written by the server for the agent. |
674 | 688 | (error) => |
@@ -678,65 +692,83 @@ const registerHtmlPreview = Effect.fn("McpHttpServer.registerHtmlPreview")(funct |
678 | 692 | ); |
679 | 693 | }); |
680 | 694 |
|
| 695 | +/** |
| 696 | + * `McpServer.toolkit` for handlers that declared their access (see |
| 697 | + * `McpToolAccess`). Every toolkit on `/mcp` registers through this. |
| 698 | + */ |
| 699 | +const toolkitRegistration = <Tools extends Record<string, Tool.Any>, EX, RX>( |
| 700 | + toolkit: Toolkit.Toolkit<Tools>, |
| 701 | + handlers: McpToolAccess.HandlersLayer<Tools, EX, RX>, |
| 702 | +) => McpServer.toolkit(toolkit).pipe(Layer.provide(handlers)); |
| 703 | + |
| 704 | +/** A hand-registered tool, also only with handlers that declared their access. */ |
| 705 | +const imageToolRegistration = <Tools extends Record<string, Tool.Any>, A, E, R, EX, RX>( |
| 706 | + register: Effect.Effect<A, E, R>, |
| 707 | + handlers: McpToolAccess.HandlersLayer<Tools, EX, RX>, |
| 708 | +) => Layer.effectDiscard(register).pipe(Layer.provide(handlers)); |
| 709 | + |
681 | 710 | export const layerHtmlToolkit = Layer.mergeAll( |
682 | | - McpServer.toolkit(HtmlRenderToolkit).pipe(Layer.provide(HtmlHandlers.layerRender)), |
683 | | - Layer.effectDiscard(registerHtmlPreview()).pipe(Layer.provide(HtmlHandlers.layerPreview)), |
| 711 | + toolkitRegistration(HtmlRenderToolkit, HtmlHandlers.layerRender), |
| 712 | + imageToolRegistration(registerHtmlPreview(), HtmlHandlers.layerPreview), |
684 | 713 | ).pipe(Layer.provide(HtmlRender.layer), Layer.provide(PreviewBrowser.layer)); |
685 | 714 |
|
686 | | -const layerPreviewStandardToolkitRegistration = McpServer.toolkit(PreviewStandardToolkit).pipe( |
687 | | - Layer.provide(PreviewHandlers.layerStandard), |
| 715 | +const layerPreviewStandardToolkitRegistration = toolkitRegistration( |
| 716 | + PreviewStandardToolkit, |
| 717 | + PreviewHandlers.layerStandard, |
688 | 718 | ); |
689 | 719 |
|
690 | | -const layerPreviewSnapshotRegistration = Layer.effectDiscard(registerPreviewSnapshot()).pipe( |
691 | | - Layer.provide(PreviewHandlers.layerSnapshot), |
| 720 | +const layerPreviewSnapshotRegistration = imageToolRegistration( |
| 721 | + registerPreviewSnapshot(), |
| 722 | + PreviewHandlers.layerSnapshot, |
692 | 723 | ); |
693 | 724 |
|
694 | 725 | export const layerPreviewToolkit = Layer.mergeAll( |
695 | 726 | layerPreviewStandardToolkitRegistration, |
696 | 727 | layerPreviewSnapshotRegistration, |
697 | 728 | ); |
698 | 729 |
|
699 | | -export const layerOrchestratorToolkit = McpServer.toolkit(OrchestratorToolkit).pipe( |
700 | | - Layer.provide(OrchestratorHandlers.layer), |
701 | | - Layer.provide(OrchestratorMcpService.layer), |
702 | | - Layer.provide(ThreadMetadataMcpService.layer), |
703 | | -); |
| 730 | +export const layerOrchestratorToolkit = toolkitRegistration( |
| 731 | + OrchestratorToolkit, |
| 732 | + OrchestratorHandlers.layer, |
| 733 | +).pipe(Layer.provide(OrchestratorMcpService.layer), Layer.provide(ThreadMetadataMcpService.layer)); |
704 | 734 |
|
705 | | -export const layerThreadToolkit = McpServer.toolkit(ThreadToolkit).pipe( |
706 | | - Layer.provide(ThreadHandlers.layer), |
707 | | -); |
| 735 | +export const layerThreadToolkit = toolkitRegistration(ThreadToolkit, ThreadHandlers.layer); |
708 | 736 |
|
709 | | -const layerWorktreeToolkitRegistration = McpServer.toolkit(WorktreeToolkit).pipe( |
710 | | - Layer.provide(WorktreeHandlers.layer), |
711 | | - Layer.provide(WorktreeMcpService.layer), |
712 | | -); |
| 737 | +const layerWorktreeToolkitRegistration = toolkitRegistration( |
| 738 | + WorktreeToolkit, |
| 739 | + WorktreeHandlers.layer, |
| 740 | +).pipe(Layer.provide(WorktreeMcpService.layer)); |
713 | 741 |
|
714 | | -const layerPreviewControlsRegistration = McpServer.toolkit(PreviewControlsToolkit).pipe( |
715 | | - Layer.provide(PreviewControlsHandlers.layer), |
| 742 | +const layerPreviewControlsRegistration = toolkitRegistration( |
| 743 | + PreviewControlsToolkit, |
| 744 | + PreviewControlsHandlers.layer, |
716 | 745 | ); |
717 | 746 |
|
718 | | -const layerEnvironmentRegistration = McpServer.toolkit(EnvironmentToolkit).pipe( |
719 | | - Layer.provide(EnvironmentHandlers.layer), |
| 747 | +const layerEnvironmentRegistration = toolkitRegistration( |
| 748 | + EnvironmentToolkit, |
| 749 | + EnvironmentHandlers.layer, |
720 | 750 | ); |
721 | 751 |
|
722 | | -const layerProjectRegistration = McpServer.toolkit(ProjectToolkit).pipe( |
723 | | - Layer.provide(ProjectHandlers.layer), |
724 | | -); |
| 752 | +const layerProjectRegistration = toolkitRegistration(ProjectToolkit, ProjectHandlers.layer); |
725 | 753 |
|
726 | | -const layerAttachmentRegistration = McpServer.toolkit(AttachmentToolkit).pipe( |
727 | | - Layer.provide(AttachmentHandlers.layer), |
| 754 | +const layerAttachmentRegistration = toolkitRegistration( |
| 755 | + AttachmentToolkit, |
| 756 | + AttachmentHandlers.layer, |
728 | 757 | ); |
729 | 758 |
|
730 | | -export const layerPullRequestsToolkit = McpServer.toolkit(PullRequestsToolkit).pipe( |
731 | | - Layer.provide(PullRequestsHandlers.layer), |
| 759 | +export const layerPullRequestsToolkit = toolkitRegistration( |
| 760 | + PullRequestsToolkit, |
| 761 | + PullRequestsHandlers.layer, |
732 | 762 | ); |
733 | 763 |
|
734 | | -const layerDeviceStandardToolkitRegistration = McpServer.toolkit(DeviceStandardToolkit).pipe( |
735 | | - Layer.provide(DeviceHandlers.layerStandard), |
| 764 | +const layerDeviceStandardToolkitRegistration = toolkitRegistration( |
| 765 | + DeviceStandardToolkit, |
| 766 | + DeviceHandlers.layerStandard, |
736 | 767 | ); |
737 | 768 |
|
738 | | -const layerDeviceScreenshotRegistration = Layer.effectDiscard(registerDeviceScreenshot()).pipe( |
739 | | - Layer.provide(DeviceHandlers.layerScreenshot), |
| 769 | +const layerDeviceScreenshotRegistration = imageToolRegistration( |
| 770 | + registerDeviceScreenshot(), |
| 771 | + DeviceHandlers.layerScreenshot, |
740 | 772 | ); |
741 | 773 |
|
742 | 774 | export const layerDeviceToolkit = Layer.mergeAll( |
|