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";
13
14const ALL_MCP_CAPABILITIES = [
15 "preview",
16 "orchestration",
17 "worktree",
18 "device",
19 "pull-requests",
20] as const;
21export type McpCapability = (typeof ALL_MCP_CAPABILITIES)[number];
22
23export interface McpThreadCaller {
24 readonly threadId: ThreadId;
25 readonly providerSessionId: string;
26 readonly providerInstanceId: ProviderInstanceId;
27}
28
29export interface McpClientCaller {
30 readonly sessionId: string;
31 readonly label: string;
32 readonly access: AuthMcpClientAccess;
33}
34
35/**
36 * A read-only client never reaches a write (`McpToolAccess` refuses it first),
37 * so it maps to the lowest mode rather than to nothing.
38 */
39export const clientRuntimeModeCeiling = (client: McpClientCaller | undefined): RuntimeMode =>
40 client === undefined || client.access === "read-only" ? "approval-required" : client.access;
41
42/**
43 * A thread caller's omitted target falls back to its own thread; a client
44 * caller has no own thread, so tools that act as the caller (delegate_task,
45 * preview, worktree handoff) need `thread`.
46 */
47export interface McpInvocationScope {
48 readonly environmentId: EnvironmentId;
49 readonly capabilities: ReadonlySet<McpCapability>;
50 readonly issuedAt: number;
51 readonly requestNamespace: string;
52 readonly thread: McpThreadCaller | undefined;
53 readonly client: McpClientCaller | undefined;
54}
55
56export class McpInvocationContext extends Context.Service<
57 McpInvocationContext,
58 McpInvocationScope
59>()("t3/mcp/McpInvocationContext") {}
60
61export type McpCapabilityError<C extends McpCapability> = C extends "preview"
62 ? PreviewAutomationUnavailableError
63 : McpCapabilityUnavailableError;
64
65const missingCapability = (
66 invocation: McpInvocationScope,
67 capability: McpCapability,
68): PreviewAutomationUnavailableError | McpCapabilityUnavailableError => {
69 const fields = {
70 environmentId: invocation.environmentId,
71 ...(invocation.thread === undefined
72 ? {}
73 : {
74 threadId: invocation.thread.threadId,
75 providerSessionId: invocation.thread.providerSessionId,
76 providerInstanceId: invocation.thread.providerInstanceId,
77 }),
78 };
79 return capability === "preview"
80 ? new PreviewAutomationUnavailableError({ capability, ...fields })
81 : new McpCapabilityUnavailableError({ capability, ...fields });
82};
83
84export const requireMcpCapability = <const C extends McpCapability>(
85 capability: C,
86): Effect.Effect<McpInvocationScope, McpCapabilityError<C>, McpInvocationContext> =>
87 McpInvocationContext.pipe(
88 Effect.filterOrFail(
89 (invocation) => invocation.capabilities.has(capability),
90 (invocation) => missingCapability(invocation, capability) as McpCapabilityError<C>,
91 ),
92 Effect.withSpan("mcp.requireCapability"),
93 );
94
95export const requireThreadMcpCapability = <const C extends "preview" | "device">(
96 capability: C,
97): Effect.Effect<McpThreadInvocationScope, McpCapabilityError<C>, McpInvocationContext> =>
98 McpInvocationContext.pipe(
99 Effect.filterOrFail(
100 (invocation): invocation is McpThreadInvocationScope =>
101 invocation.capabilities.has(capability) && invocation.thread !== undefined,
102 (invocation) => missingCapability(invocation, capability) as McpCapabilityError<C>,
103 ),
104 Effect.withSpan("mcp.requireCapability"),
105 );
106
107const threadCallerRequired = (operation: string) =>
108 new OrchestratorMcpFailure({
109 code: "thread_credential_required",
110 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.`,
111 });
112
113export type McpThreadInvocationScope = McpInvocationScope & { readonly thread: McpThreadCaller };
114
115export const requireThreadScope = (scope: McpInvocationScope, operation: string) =>
116 scope.thread === undefined
117 ? Effect.fail(threadCallerRequired(operation))
118 : Effect.succeed(scope as McpThreadInvocationScope);