Open History to see the last commit
1import {
2 type AuthMcpClientAccess,
3 type EnvironmentId,
4 McpCapabilityUnavailableError,
5 OrchestratorMcpFailure,
6 PreviewAutomationUnavailableError,
7 type ProviderInstanceId,
8 type RuntimeMode,
9 type ThreadId,
10} from "@t3tools/contracts";
11import * as Context from "effect/Context";
12import * as Effect from "effect/Effect";
13import * as Fiber from "effect/Fiber";
14
15const ALL_MCP_CAPABILITIES = [
16 "preview",
17 "orchestration",
18 "worktree",
19 "device",
20 "pull-requests",
21] as const;
22export type McpCapability = (typeof ALL_MCP_CAPABILITIES)[number];
23
24export interface McpThreadCaller {
25 readonly threadId: ThreadId;
26 readonly providerSessionId: string;
27 readonly providerInstanceId: ProviderInstanceId;
28 /** A delegated child task; its parent owns the work it hands back. */
29 readonly subagent?: boolean;
30}
31
32export interface McpClientCaller {
33 readonly sessionId: string;
34 readonly label: string;
35 readonly access: AuthMcpClientAccess;
36}
37
38/** Read-only clients map to the lowest mode because McpToolAccess blocks writes first. */
39export const clientRuntimeModeCeiling = (client: McpClientCaller | undefined): RuntimeMode =>
40 client === undefined || client.access === "read-only" ? "approval-required" : client.access;
41
42/** Thread callers default to their own thread; client callers need thread for tools that act as the caller. */
43export interface McpInvocationScope {
44 readonly environmentId: EnvironmentId;
45 readonly capabilities: ReadonlySet<McpCapability>;
46 readonly issuedAt: number;
47 readonly requestNamespace: string;
48 readonly thread: McpThreadCaller | undefined;
49 readonly client: McpClientCaller | undefined;
50}
51
52export class McpInvocationContext extends Context.Service<
53 McpInvocationContext,
54 McpInvocationScope
55>()("t3/mcp/McpInvocationContext") {}
56
57/**
58 * `McpSchema.EnabledWhen` for tools a delegated child must not see or call.
59 * EnabledWhen only receives the MCP client profile, so read the T3 caller from the request fiber.
60 */
61export const hiddenFromSubagents = (): boolean => {
62 const fiber = Fiber.getCurrent();
63 const scope = fiber && Context.getOrUndefined(fiber.context, McpInvocationContext);
64 return scope?.thread?.subagent !== true;
65};
66
67export type McpCapabilityError<C extends McpCapability> = C extends "preview"
68 ? PreviewAutomationUnavailableError
69 : McpCapabilityUnavailableError;
70
71const missingCapability = (
72 invocation: McpInvocationScope,
73 capability: McpCapability,
74): PreviewAutomationUnavailableError | McpCapabilityUnavailableError => {
75 const fields = {
76 environmentId: invocation.environmentId,
77 ...(invocation.thread === undefined
78 ? {}
79 : {
80 threadId: invocation.thread.threadId,
81 providerSessionId: invocation.thread.providerSessionId,
82 providerInstanceId: invocation.thread.providerInstanceId,
83 }),
84 };
85 return capability === "preview"
86 ? new PreviewAutomationUnavailableError({ capability, ...fields })
87 : new McpCapabilityUnavailableError({ capability, ...fields });
88};
89
90export const requireMcpCapability = <const C extends McpCapability>(
91 capability: C,
92): Effect.Effect<McpInvocationScope, McpCapabilityError<C>, McpInvocationContext> =>
93 McpInvocationContext.pipe(
94 Effect.filterOrFail(
95 (invocation) => invocation.capabilities.has(capability),
96 (invocation) => missingCapability(invocation, capability) as McpCapabilityError<C>,
97 ),
98 Effect.withSpan("mcp.requireCapability"),
99 );
100
101export const requireThreadMcpCapability = <const C extends "preview" | "device">(
102 capability: C,
103): Effect.Effect<McpThreadInvocationScope, McpCapabilityError<C>, McpInvocationContext> =>
104 McpInvocationContext.pipe(
105 Effect.filterOrFail(
106 (invocation): invocation is McpThreadInvocationScope =>
107 invocation.capabilities.has(capability) && invocation.thread !== undefined,
108 (invocation) => missingCapability(invocation, capability) as McpCapabilityError<C>,
109 ),
110 Effect.withSpan("mcp.requireCapability"),
111 );
112
113const threadCallerRequired = (operation: string) =>
114 new OrchestratorMcpFailure({
115 code: "thread_credential_required",
116 message: `${operation} acts as the calling T3 thread, so it needs an agent running inside T3 Code. This MCP client signed in from outside a thread.`,
117 });
118
119export type McpThreadInvocationScope = McpInvocationScope & { readonly thread: McpThreadCaller };
120
121export const requireThreadScope = (scope: McpInvocationScope, operation: string) =>
122 scope.thread === undefined
123 ? Effect.fail(threadCallerRequired(operation))
124 : Effect.succeed(scope as McpThreadInvocationScope);