Open History to see the last commit
1import {
2 CommandId,
3 OrchestratorMcpFailure,
4 type ThreadId,
5 type OrchestrationV2ThreadShell,
6} from "@t3tools/contracts";
7import * as Crypto from "effect/Crypto";
8import * as Effect from "effect/Effect";
9
10import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
11import * as OrchestrationMcp from "./OrchestratorMcpService.ts";
12import { type McpInvocationScope, McpInvocationContext } from "./McpInvocationContext.ts";
13
14export const unavailable = () =>
15 new OrchestratorMcpFailure({
16 code: "orchestration_error",
17 message: "The operation could not be completed.",
18 });
19
20export const readCaller = Effect.fn("mcp.readCaller")(function* () {
21 const scope = yield* McpInvocationContext;
22 if (!scope.capabilities.has("orchestration")) {
23 return yield* new OrchestratorMcpFailure({
24 code: "capability_denied",
25 message: "This credential cannot control threads.",
26 });
27 }
28 const threads = yield* ThreadManagement.ThreadManagementService;
29 const caller = yield* threads.getThreadShell(scope.threadId).pipe(Effect.mapError(unavailable));
30 if (caller === null || caller.deletedAt !== null) {
31 return yield* new OrchestratorMcpFailure({
32 code: "thread_not_found",
33 message: "The calling thread was not found.",
34 });
35 }
36 return { scope, threads, caller };
37});
38
39function assertLiveCaller({
40 caller,
41 scope,
42}: {
43 caller: OrchestrationV2ThreadShell;
44 scope: McpInvocationScope;
45}) {
46 return caller.archivedAt !== null ||
47 caller.activeRunId === null ||
48 caller.providerInstanceId !== scope.providerInstanceId
49 ? Effect.fail(
50 new OrchestratorMcpFailure({
51 code: "parent_not_active",
52 message: "The calling provider no longer owns an active thread run.",
53 }),
54 )
55 : Effect.void;
56}
57export const readMutationCaller = Effect.fn("mcp.readMutationCaller")(function* () {
58 const context = yield* readCaller();
59 yield* assertLiveCaller(context);
60 return context;
61});
62
63/** Resolve the credential's project before looking up a caller-supplied thread. */
64export const readThread = Effect.fn("mcp.readThread")(function* (threadId?: ThreadId) {
65 const { scope, threads, caller } = yield* readCaller();
66 const projection = yield* threads
67 .getProjectThread({ projectId: caller.projectId, threadId: threadId ?? caller.id })
68 .pipe(
69 Effect.mapError((error) =>
70 error._tag === "ThreadManagementThreadNotFoundError"
71 ? new OrchestratorMcpFailure({
72 code: "thread_not_found",
73 message: "The thread was not found in the calling project.",
74 })
75 : unavailable(),
76 ),
77 );
78 return { scope, threads, caller, projection };
79});
80
81export const readWritableThread = Effect.fn("mcp.readWritableThread")(function* (
82 threadId?: ThreadId,
83) {
84 const context = yield* readThread(threadId);
85 yield* assertLiveCaller(context);
86 yield* OrchestrationMcp.resolveRuntimeMode(
87 context.caller.runtimeMode,
88 context.projection.thread.runtimeMode,
89 );
90 yield* OrchestrationMcp.resolveInteractionMode(
91 context.caller.interactionMode,
92 context.projection.thread.interactionMode,
93 );
94 return context;
95});
96
97export const newCommandId = Effect.fn("mcp.newCommandId")(function* () {
98 const crypto = yield* Crypto.Crypto;
99 return CommandId.make(`mcp:${yield* crypto.randomUUIDv4.pipe(Effect.orDie)}`);
100});