The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { OrchestratorMcpFailure } from "@t3tools/contracts"; |
| 2 | import { OrchestratorToolkit } from "./tools.ts"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | |
| 5 | import * as PeerForwarding from "../../../peer/PeerForwarding.ts"; |
| 6 | import * as PeerLinkRequests from "../../../peer/PeerLinkRequests.ts"; |
| 7 | import * as PeerLinks from "../../../peer/PeerLinks.ts"; |
| 8 | import * as RemoteDelegation from "../../../peer/RemoteDelegation.ts"; |
| 9 | import { readCaller } from "../../threadAccess.ts"; |
| 10 | import * as McpInvocationContext from "../../McpInvocationContext.ts"; |
| 11 | import * as McpToolAccess from "../../McpToolAccess.ts"; |
| 12 | import * as OrchestratorMcpService from "../../OrchestratorMcpService.ts"; |
| 13 | import * as ThreadMetadataMcpService from "../../ThreadMetadataMcpService.ts"; |
| 14 | |
| 15 | const 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 | // Claude can start a turn by itself before T3 starts its run, so a live-run check would refuse the mark. |
| 65 | t3_background_wait: McpToolAccess.readsAsCaller((input) => |
| 66 | Effect.gen(function* () { |
| 67 | const { scope, threads } = yield* readCaller(); |
| 68 | const caller = yield* McpInvocationContext.requireThreadScope(scope, "t3_background_wait"); |
| 69 | return yield* threads |
| 70 | .awaitBackgroundTasks({ |
| 71 | threadId: caller.thread.threadId, |
| 72 | taskIds: input.taskIds, |
| 73 | awaited: input.waiting ?? true, |
| 74 | }) |
| 75 | .pipe( |
| 76 | Effect.mapError( |
| 77 | (cause) => |
| 78 | new OrchestratorMcpFailure({ |
| 79 | code: |
| 80 | cause.reason === "unsupported" || cause.reason === "unmatched" |
| 81 | ? "invalid_request" |
| 82 | : "orchestration_error", |
| 83 | message: cause.message, |
| 84 | }), |
| 85 | ), |
| 86 | ); |
| 87 | }), |
| 88 | ), |
| 89 | schedule_task: McpToolAccess.either( |
| 90 | (input) => input.scheduledTaskId === undefined, |
| 91 | McpToolAccess.startsThreads( |
| 92 | () => ({}), |
| 93 | ({ prompt, schedule, ...input }) => |
| 94 | Effect.gen(function* () { |
| 95 | if (prompt === undefined || schedule === undefined) { |
| 96 | return yield* new OrchestratorMcpFailure({ |
| 97 | code: "invalid_request", |
| 98 | message: "A new scheduled task needs `prompt` and `schedule`.", |
| 99 | }); |
| 100 | } |
| 101 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 102 | const service = yield* OrchestratorMcpService.OrchestratorMcpService; |
| 103 | return yield* service.scheduleTask(scope, { ...input, prompt, schedule }); |
| 104 | }), |
| 105 | // A scheduled run starts long after its caller, with nothing to carry the link. |
| 106 | { refused: "schedule tasks in this environment" }, |
| 107 | ), |
| 108 | McpToolAccess.writes( |
| 109 | ({ scheduledTaskId, prompt, title, schedule, enabled, freshSession, bindToCurrentThread }) => |
| 110 | Effect.gen(function* () { |
| 111 | if (scheduledTaskId === undefined) return yield* Effect.die("unreachable"); |
| 112 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 113 | const service = yield* OrchestratorMcpService.OrchestratorMcpService; |
| 114 | return yield* service.updateScheduledTask(scope, { |
| 115 | scheduledTaskId, |
| 116 | prompt, |
| 117 | title, |
| 118 | schedule, |
| 119 | enabled, |
| 120 | freshSession, |
| 121 | bindToCurrentThread, |
| 122 | }); |
| 123 | }), |
| 124 | ), |
| 125 | ), |
| 126 | list_scheduled_tasks: McpToolAccess.reads((input) => |
| 127 | Effect.gen(function* () { |
| 128 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 129 | const service = yield* OrchestratorMcpService.OrchestratorMcpService; |
| 130 | return yield* service.listScheduledTasks(scope, input); |
| 131 | }), |
| 132 | ), |
| 133 | delete_scheduled_task: McpToolAccess.writes((input) => |
| 134 | Effect.gen(function* () { |
| 135 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 136 | const service = yield* OrchestratorMcpService.OrchestratorMcpService; |
| 137 | return yield* service.deleteScheduledTask(scope, input); |
| 138 | }), |
| 139 | ), |
| 140 | request_secret: McpToolAccess.actsAsCaller((input) => |
| 141 | Effect.gen(function* () { |
| 142 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 143 | const service = yield* OrchestratorMcpService.OrchestratorMcpService; |
| 144 | return yield* service.requestSecret(scope, input); |
| 145 | }), |
| 146 | ), |
| 147 | create_threads: McpToolAccess.actsAsCaller((input) => |
| 148 | Effect.gen(function* () { |
| 149 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 150 | const service = yield* OrchestratorMcpService.OrchestratorMcpService; |
| 151 | return yield* service.createThreads(scope, input); |
| 152 | }), |
| 153 | ), |
| 154 | t3_thread_list: McpToolAccess.reads((input) => |
| 155 | Effect.gen(function* () { |
| 156 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 157 | const service = yield* OrchestratorMcpService.OrchestratorMcpService; |
| 158 | return yield* service.listThreads(scope, input); |
| 159 | }), |
| 160 | ), |
| 161 | t3_thread_read: McpToolAccess.reads((input) => |
| 162 | Effect.gen(function* () { |
| 163 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 164 | const service = yield* OrchestratorMcpService.OrchestratorMcpService; |
| 165 | return yield* service.readThread(scope, input); |
| 166 | }), |
| 167 | ), |
| 168 | t3_thread_update: McpToolAccess.writesThreads( |
| 169 | (input) => [input.threadId], |
| 170 | (input) => |
| 171 | Effect.gen(function* () { |
| 172 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 173 | const service = yield* ThreadMetadataMcpService.ThreadMetadataMcpService; |
| 174 | return yield* service.update(scope, input); |
| 175 | }), |
| 176 | ), |
| 177 | t3_thread_send: McpToolAccess.afterForwarding( |
| 178 | McpToolAccess.writesThreads( |
| 179 | (input) => [input.threadId], |
| 180 | (input) => |
| 181 | Effect.gen(function* () { |
| 182 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 183 | const service = yield* OrchestratorMcpService.OrchestratorMcpService; |
| 184 | return yield* service.sendToThread(scope, input); |
| 185 | }), |
| 186 | ), |
| 187 | // There the thread's parent is here, so only this side can make a follow-up a task. |
| 188 | (input, environmentId, sent) => |
| 189 | Effect.gen(function* () { |
| 190 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 191 | const remote = yield* RemoteDelegation.RemoteDelegation; |
| 192 | return yield* remote.followUp(scope, environmentId, input, sent); |
| 193 | }), |
| 194 | ), |
| 195 | t3_thread_wait: McpToolAccess.reads((input) => |
| 196 | Effect.gen(function* () { |
| 197 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 198 | const service = yield* OrchestratorMcpService.OrchestratorMcpService; |
| 199 | return yield* service.waitForThread(scope, input); |
| 200 | }), |
| 201 | ), |
| 202 | t3_environment_links: McpToolAccess.reads(() => |
| 203 | Effect.gen(function* () { |
| 204 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 205 | const forwarding = yield* PeerForwarding.PeerForwarding; |
| 206 | return yield* forwarding.links(scope); |
| 207 | }), |
| 208 | ), |
| 209 | // A link lets every agent here reach the other environment, so it is an |
| 210 | // environment setting: full access, and never for linked work. |
| 211 | t3_environment_link: McpToolAccess.writesEnvironment((input) => |
| 212 | Effect.gen(function* () { |
| 213 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 214 | const requests = yield* PeerLinkRequests.PeerLinkRequests; |
| 215 | return yield* requests.request(scope, input); |
| 216 | }), |
| 217 | ), |
| 218 | t3_environment_unlink: McpToolAccess.writesEnvironment(({ environmentId }) => |
| 219 | Effect.gen(function* () { |
| 220 | const links = yield* PeerLinks.PeerLinks; |
| 221 | const removed = yield* links |
| 222 | .unlink(environmentId) |
| 223 | .pipe( |
| 224 | Effect.mapError( |
| 225 | (error) => |
| 226 | new OrchestratorMcpFailure({ code: "orchestration_error", message: error.message }), |
| 227 | ), |
| 228 | ); |
| 229 | return { environmentId, removed }; |
| 230 | }), |
| 231 | ), |
| 232 | t3_thread_interrupt: McpToolAccess.writesThreads( |
| 233 | (input) => [input.threadId], |
| 234 | (input) => |
| 235 | Effect.gen(function* () { |
| 236 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 237 | const service = yield* OrchestratorMcpService.OrchestratorMcpService; |
| 238 | return yield* service.interruptThread(scope, input); |
| 239 | }), |
| 240 | ), |
| 241 | } satisfies McpToolAccess.Handlers<typeof OrchestratorToolkit.tools>; |
| 242 | |
| 243 | export const layer = McpToolAccess.toLayer(OrchestratorToolkit, handlers); |