Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

McpInvocationContext.ts

123 lines4.6 KB

1import {
2 type EnvironmentId,
3 McpCapabilityUnavailableError,
4 OrchestratorMcpFailure,
5 PreviewAutomationUnavailableError,
6 type ProviderInstanceId,
7 type RuntimeMode,
8 type ThreadId,
9} from "@t3tools/contracts";
10import * as Context from "effect/Context";
11import * as Effect from "effect/Effect";
12​
13const ALL_MCP_CAPABILITIES = [
14 "preview",
15 "orchestration",
16 "worktree",
17 "device",
18 "pull-requests",
19] as const;
20export type McpCapability = (typeof ALL_MCP_CAPABILITIES)[number];
21​
22/** A provider session T3 Code launched for one thread. */
23export interface McpThreadCaller {
24 readonly threadId: ThreadId;
25 readonly providerSessionId: string;
26 readonly providerInstanceId: ProviderInstanceId;
28​
29/** An agent T3 Code did not launch, signed in through MCP OAuth. */
30export interface McpClientCaller {
31 readonly sessionId: string;
32 readonly label: string;
33 readonly runtimeModeCeiling: RuntimeMode;
35​
36/**
37 * Who is calling and what they may do. Tool parameters choose the target
38 * (thread, project); the caller sets the limits. A thread caller's omitted
39 * target falls back to its own thread; a client caller has no own thread, so
40 * tools that act as the caller (delegate_task, preview, worktree handoff)
41 * need `thread`.
42 */
43export interface McpInvocationScope {
44 readonly environmentId: EnvironmentId;
45 readonly capabilities: ReadonlySet<McpCapability>;
46 readonly issuedAt: number;
47 /** Namespaces idempotency keys so two callers reusing a clientRequestId cannot collide. */
48 readonly requestNamespace: string;
49 readonly thread: McpThreadCaller | undefined;
50 readonly client: McpClientCaller | undefined;
52​
53export class McpInvocationContext extends Context.Service<
54 McpInvocationContext,
55 McpInvocationScope
56>()("t3/mcp/McpInvocationContext") {}
57​
58/** The error a missing capability surfaces as; preview keeps its own so the broker can route it. */
59export type McpCapabilityError<C extends McpCapability> = C extends "preview"
60 ? PreviewAutomationUnavailableError
61 : McpCapabilityUnavailableError;
62​
63const missingCapability = (
64 invocation: McpInvocationScope,
65 capability: McpCapability,
66): PreviewAutomationUnavailableError | McpCapabilityUnavailableError => {
67 const fields = {
68 environmentId: invocation.environmentId,
69 ...(invocation.thread === undefined
70 ? {}
71 : {
72 threadId: invocation.thread.threadId,
73 providerSessionId: invocation.thread.providerSessionId,
74 providerInstanceId: invocation.thread.providerInstanceId,
75 }),
76 };
77 return capability === "preview"
78 ? new PreviewAutomationUnavailableError({ capability, ...fields })
79 : new McpCapabilityUnavailableError({ capability, ...fields });
80};
81​
82export const requireMcpCapability = <const C extends McpCapability>(
83 capability: C,
84): Effect.Effect<McpInvocationScope, McpCapabilityError<C>, McpInvocationContext> =>
85 McpInvocationContext.pipe(
86 Effect.filterOrFail(
87 (invocation) => invocation.capabilities.has(capability),
88 // The conditional type narrows what the literal argument decided at runtime.
89 (invocation) => missingCapability(invocation, capability) as McpCapabilityError<C>,
90 ),
91 Effect.withSpan("mcp.requireCapability"),
92 );
93​
94/**
95 * Preview tabs and device sessions belong to the calling thread, so their
96 * capabilities are only ever granted to thread callers. A scope that carries
97 * one without a thread is refused the same way as a missing capability.
98 */
99export const requireThreadMcpCapability = <const C extends "preview" | "device">(
100 capability: C,
101): Effect.Effect<McpThreadInvocationScope, McpCapabilityError<C>, McpInvocationContext> =>
102 McpInvocationContext.pipe(
103 Effect.filterOrFail(
104 (invocation): invocation is McpThreadInvocationScope =>
105 invocation.capabilities.has(capability) && invocation.thread !== undefined,
106 (invocation) => missingCapability(invocation, capability) as McpCapabilityError<C>,
107 ),
108 Effect.withSpan("mcp.requireCapability"),
109 );
110​
111const threadCallerRequired = (operation: string) =>
112 new OrchestratorMcpFailure({
113 code: "thread_credential_required",
114 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.`,
115 });
116​
117/** A scope with a thread caller, for tools whose whole surface acts as the caller. */
118export type McpThreadInvocationScope = McpInvocationScope & { readonly thread: McpThreadCaller };
119​
120export const requireThreadScope = (scope: McpInvocationScope, operation: string) =>
121 scope.thread === undefined
122 ? Effect.fail(threadCallerRequired(operation))
123 : Effect.succeed(scope as McpThreadInvocationScope);