Open History to see the last commit
1import { OrchestratorMcpFailure } from "@t3tools/contracts";
2import { OrchestratorToolkit } from "./tools.ts";
3import * as Effect from "effect/Effect";
4
5import * as PeerForwarding from "../../../peer/PeerForwarding.ts";
6import * as PeerLinkRequests from "../../../peer/PeerLinkRequests.ts";
7import * as PeerLinks from "../../../peer/PeerLinks.ts";
8import * as RemoteDelegation from "../../../peer/RemoteDelegation.ts";
9import { readCaller } from "../../threadAccess.ts";
10import * as McpInvocationContext from "../../McpInvocationContext.ts";
11import * as McpToolAccess from "../../McpToolAccess.ts";
12import * as OrchestratorMcpService from "../../OrchestratorMcpService.ts";
13import * as ThreadMetadataMcpService from "../../ThreadMetadataMcpService.ts";
14
15const handlers = {
16 orchestrator_capabilities: McpToolAccess.reads((input) =>
17 Effect.gen(function* () {
18 const scope = yield* McpInvocationContext.McpInvocationContext;
19 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
20 return yield* service.capabilities(scope, input);
21 }),
22 ),
23 delegate_task: McpToolAccess.actsAsCaller((input) =>
24 Effect.gen(function* () {
25 const scope = yield* McpInvocationContext.McpInvocationContext;
26 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
27 return yield* service.delegateTask(scope, input);
28 }),
29 ),
30 task_status: McpToolAccess.actsAsCaller(({ taskId }) =>
31 Effect.gen(function* () {
32 const scope = yield* McpInvocationContext.McpInvocationContext;
33 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
34 return yield* service.taskStatus(scope, taskId);
35 }),
36 ),
37 task_cancel: McpToolAccess.actsAsCaller((input) =>
38 Effect.gen(function* () {
39 const scope = yield* McpInvocationContext.McpInvocationContext;
40 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
41 return yield* service.cancelTask(scope, input);
42 }),
43 ),
44 t3_workflow_stop: McpToolAccess.writesThreads(
45 (input) => [input.threadId],
46 (input) =>
47 Effect.gen(function* () {
48 const { threads } = yield* readCaller();
49 return yield* threads.stopWorkflow(input).pipe(
50 Effect.as({}),
51 Effect.mapError(
52 (cause) =>
53 new OrchestratorMcpFailure({
54 code:
55 cause.reason === "unavailable" || cause.reason === "stop-failed"
56 ? "orchestration_error"
57 : "invalid_request",
58 message: cause.message,
59 }),
60 ),
61 );
62 }),
63 ),
64 t3_background_wait: McpToolAccess.actsAsCaller((input) =>
65 Effect.gen(function* () {
66 const { scope, threads } = yield* readCaller();
67 const caller = yield* McpInvocationContext.requireThreadScope(scope, "t3_background_wait");
68 return yield* threads
69 .awaitBackgroundTasks({
70 threadId: caller.thread.threadId,
71 taskIds: input.taskIds,
72 awaited: input.waiting ?? true,
73 })
74 .pipe(
75 Effect.mapError(
76 (cause) =>
77 new OrchestratorMcpFailure({
78 code:
79 cause.reason === "unsupported" || cause.reason === "unmatched"
80 ? "invalid_request"
81 : "orchestration_error",
82 message: cause.message,
83 }),
84 ),
85 );
86 }),
87 ),
88 schedule_task: McpToolAccess.either(
89 (input) => input.scheduledTaskId === undefined,
90 McpToolAccess.startsThreads(
91 () => ({}),
92 ({ prompt, schedule, ...input }) =>
93 Effect.gen(function* () {
94 if (prompt === undefined || schedule === undefined) {
95 return yield* new OrchestratorMcpFailure({
96 code: "invalid_request",
97 message: "A new scheduled task needs `prompt` and `schedule`.",
98 });
99 }
100 const scope = yield* McpInvocationContext.McpInvocationContext;
101 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
102 return yield* service.scheduleTask(scope, { ...input, prompt, schedule });
103 }),
104 // A scheduled run starts long after its caller, with nothing to carry the link.
105 { refused: "schedule tasks in this environment" },
106 ),
107 McpToolAccess.writes(
108 ({ scheduledTaskId, prompt, title, schedule, enabled, freshSession, bindToCurrentThread }) =>
109 Effect.gen(function* () {
110 if (scheduledTaskId === undefined) return yield* Effect.die("unreachable");
111 const scope = yield* McpInvocationContext.McpInvocationContext;
112 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
113 return yield* service.updateScheduledTask(scope, {
114 scheduledTaskId,
115 prompt,
116 title,
117 schedule,
118 enabled,
119 freshSession,
120 bindToCurrentThread,
121 });
122 }),
123 ),
124 ),
125 list_scheduled_tasks: McpToolAccess.reads((input) =>
126 Effect.gen(function* () {
127 const scope = yield* McpInvocationContext.McpInvocationContext;
128 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
129 return yield* service.listScheduledTasks(scope, input);
130 }),
131 ),
132 delete_scheduled_task: McpToolAccess.writes((input) =>
133 Effect.gen(function* () {
134 const scope = yield* McpInvocationContext.McpInvocationContext;
135 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
136 return yield* service.deleteScheduledTask(scope, input);
137 }),
138 ),
139 request_secret: McpToolAccess.actsAsCaller((input) =>
140 Effect.gen(function* () {
141 const scope = yield* McpInvocationContext.McpInvocationContext;
142 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
143 return yield* service.requestSecret(scope, input);
144 }),
145 ),
146 create_threads: McpToolAccess.actsAsCaller((input) =>
147 Effect.gen(function* () {
148 const scope = yield* McpInvocationContext.McpInvocationContext;
149 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
150 return yield* service.createThreads(scope, input);
151 }),
152 ),
153 t3_thread_list: McpToolAccess.reads((input) =>
154 Effect.gen(function* () {
155 const scope = yield* McpInvocationContext.McpInvocationContext;
156 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
157 return yield* service.listThreads(scope, input);
158 }),
159 ),
160 t3_thread_read: McpToolAccess.reads((input) =>
161 Effect.gen(function* () {
162 const scope = yield* McpInvocationContext.McpInvocationContext;
163 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
164 return yield* service.readThread(scope, input);
165 }),
166 ),
167 t3_thread_update: McpToolAccess.writesThreads(
168 (input) => [input.threadId],
169 (input) =>
170 Effect.gen(function* () {
171 const scope = yield* McpInvocationContext.McpInvocationContext;
172 const service = yield* ThreadMetadataMcpService.ThreadMetadataMcpService;
173 return yield* service.update(scope, input);
174 }),
175 ),
176 t3_thread_send: McpToolAccess.afterForwarding(
177 McpToolAccess.writesThreads(
178 (input) => [input.threadId],
179 (input) =>
180 Effect.gen(function* () {
181 const scope = yield* McpInvocationContext.McpInvocationContext;
182 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
183 return yield* service.sendToThread(scope, input);
184 }),
185 ),
186 // There the thread's parent is here, so only this side can make a follow-up a task.
187 (input, environmentId, sent) =>
188 Effect.gen(function* () {
189 const scope = yield* McpInvocationContext.McpInvocationContext;
190 const remote = yield* RemoteDelegation.RemoteDelegation;
191 return yield* remote.followUp(scope, environmentId, input, sent);
192 }),
193 ),
194 t3_thread_wait: McpToolAccess.reads((input) =>
195 Effect.gen(function* () {
196 const scope = yield* McpInvocationContext.McpInvocationContext;
197 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
198 return yield* service.waitForThread(scope, input);
199 }),
200 ),
201 t3_environment_links: McpToolAccess.reads(() =>
202 Effect.gen(function* () {
203 const scope = yield* McpInvocationContext.McpInvocationContext;
204 const forwarding = yield* PeerForwarding.PeerForwarding;
205 return yield* forwarding.links(scope);
206 }),
207 ),
208 // A link lets every agent here reach the other environment, so it is an
209 // environment setting: full access, and never for linked work.
210 t3_environment_link: McpToolAccess.writesEnvironment((input) =>
211 Effect.gen(function* () {
212 const scope = yield* McpInvocationContext.McpInvocationContext;
213 const requests = yield* PeerLinkRequests.PeerLinkRequests;
214 return yield* requests.request(scope, input);
215 }),
216 ),
217 t3_environment_unlink: McpToolAccess.writesEnvironment(({ environmentId }) =>
218 Effect.gen(function* () {
219 const links = yield* PeerLinks.PeerLinks;
220 const removed = yield* links
221 .unlink(environmentId)
222 .pipe(
223 Effect.mapError(
224 (error) =>
225 new OrchestratorMcpFailure({ code: "orchestration_error", message: error.message }),
226 ),
227 );
228 return { environmentId, removed };
229 }),
230 ),
231 t3_thread_interrupt: McpToolAccess.writesThreads(
232 (input) => [input.threadId],
233 (input) =>
234 Effect.gen(function* () {
235 const scope = yield* McpInvocationContext.McpInvocationContext;
236 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
237 return yield* service.interruptThread(scope, input);
238 }),
239 ),
240} satisfies McpToolAccess.Handlers<typeof OrchestratorToolkit.tools>;
241
242export const layer = McpToolAccess.toLayer(OrchestratorToolkit, handlers);