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