From 9e4993ca8e0dccaf2663ef1a05a4c61a95d3e11f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 13:10:22 +0000 Subject: [PATCH 1/4] fix(server): messages typed through the t3 CLI reach the model as the user's t3_thread_send, t3_thread_launch and t3_thread_send_attachments recorded every message as createdBy "agent", so a message the user typed with `t3 thread start` or `t3 thread send` reached the model after the line saying it was not from the user. A caller outside any thread that is not a linked environment is a client the user signed in to act for them, so those tools now record its messages as the user's. Thread agents and linked environments keep the label. The launch still caps a replayed message at the caller's modes, keyed on the MCP source instead of the actor. Fixes pingdotgg/t3code#39711 Co-Authored-By: Claude Opus 5.5 --- apps/server/src/mcp/McpInvocationContext.ts | 12 ++++++++++++ apps/server/src/mcp/OrchestratorMcpService.test.ts | 9 +++++++-- apps/server/src/mcp/OrchestratorMcpService.ts | 3 ++- apps/server/src/mcp/toolkits/attachment/handlers.ts | 4 +++- .../server/src/mcp/toolkits/project/handlers.test.ts | 1 + apps/server/src/mcp/toolkits/project/handlers.ts | 2 +- .../src/orchestration-v2/ThreadLaunchService.ts | 4 ++-- 7 files changed, 28 insertions(+), 7 deletions(-) diff --git a/apps/server/src/mcp/McpInvocationContext.ts b/apps/server/src/mcp/McpInvocationContext.ts index 893c17f23311b..7a126d0a8307d 100644 --- a/apps/server/src/mcp/McpInvocationContext.ts +++ b/apps/server/src/mcp/McpInvocationContext.ts @@ -6,6 +6,7 @@ import { PreviewAutomationUnavailableError, type ProviderInstanceId, type ProviderInteractionMode, + type OrchestrationV2Actor, type OrchestrationV2LinkedSender, type RuntimeMode, type ThreadId, @@ -161,6 +162,17 @@ export interface McpInvocationScope { readonly forge?: boolean; } +/** + * Who a message an MCP caller sends comes from. A client the user signed in from outside a thread, + * such as the `t3` CLI, acts for the user; a thread's agent or a linked environment's does not. + */ +export const mcpMessageActor = ( + scope: Pick, +): OrchestrationV2Actor => + scope.thread === undefined && scope.client !== undefined && scope.client.linked !== true + ? "user" + : "agent"; + /** * T3 Code renders `t3-thread://` links as thread chips. Other MCP clients cannot open them, so * they get the hosted link Copy thread copies, which opens the thread in either app or a browser. diff --git a/apps/server/src/mcp/OrchestratorMcpService.test.ts b/apps/server/src/mcp/OrchestratorMcpService.test.ts index 2dad1fe68ee6d..218dc2e775079 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.test.ts @@ -5129,7 +5129,7 @@ describe("OrchestratorMcpService thread lookup", () => { }), ); - it.effect("records the linked environment's thread that sent a message", () => + it.effect("records whether the user or a linked environment's thread sent a message", () => Effect.gen(function* () { const target = liveThreadShell(ThreadId.make("thread:target")); const sender = { @@ -5174,7 +5174,12 @@ describe("OrchestratorMcpService thread lookup", () => { const fromLink = yield* sendAs(linked(sender)); assert.deepStrictEqual(fromLink?.linkedSender, sender); assert.isUndefined(fromLink?.senderThreadId); - assert.isUndefined((yield* sendAs(linked()))?.linkedSender); + assert.strictEqual(fromLink?.createdBy, "agent"); + const fromBareLink = yield* sendAs(linked()); + assert.isUndefined(fromBareLink?.linkedSender); + assert.strictEqual(fromBareLink?.createdBy, "agent"); + // A client the user signed in, such as the `t3` CLI, sends as the user. + assert.strictEqual((yield* sendAs(client))?.createdBy, "user"); }), ); diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index 7b8c99877d974..a65e0ef77e262 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -123,6 +123,7 @@ import { clientModeCeiling, type McpInvocationScope, type McpThreadInvocationScope, + mcpMessageActor, requireThreadScope, threadLinkFor, } from "./McpInvocationContext.ts"; @@ -3645,7 +3646,7 @@ const make = Effect.gen(function* () { attachments: [], mode, ...(input.freshContext ? { freshContext: true } : {}), - createdBy: "agent", + createdBy: mcpMessageActor(scope), creationSource: "mcp", }) .pipe( diff --git a/apps/server/src/mcp/toolkits/attachment/handlers.ts b/apps/server/src/mcp/toolkits/attachment/handlers.ts index 25d0ac7fad15d..6fb6b499d9e0f 100644 --- a/apps/server/src/mcp/toolkits/attachment/handlers.ts +++ b/apps/server/src/mcp/toolkits/attachment/handlers.ts @@ -110,7 +110,9 @@ export const layer = McpToolAccess.toLayer( text: input.message ?? "", attachments, mode: "auto", - createdBy: "agent", + createdBy: McpInvocationContext.mcpMessageActor( + yield* McpInvocationContext.McpInvocationContext, + ), creationSource: "mcp", }).pipe( Effect.mapError((error) => diff --git a/apps/server/src/mcp/toolkits/project/handlers.test.ts b/apps/server/src/mcp/toolkits/project/handlers.test.ts index 0664c03f60223..9559880c1327f 100644 --- a/apps/server/src/mcp/toolkits/project/handlers.test.ts +++ b/apps/server/src/mcp/toolkits/project/handlers.test.ts @@ -990,6 +990,7 @@ it.effect("a client launches with the fallback for an exhausted default", () => modelSelection: fallbackModel, }); expect(launched[0]?.modelSelection).toEqual(fallbackModel); + expect(launched[0]?.createdBy).toBe("user"); }), ); diff --git a/apps/server/src/mcp/toolkits/project/handlers.ts b/apps/server/src/mcp/toolkits/project/handlers.ts index dfb346156d40a..2770fdbebe2bc 100644 --- a/apps/server/src/mcp/toolkits/project/handlers.ts +++ b/apps/server/src/mcp/toolkits/project/handlers.ts @@ -429,7 +429,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { attachments, }, }), - createdBy: "agent", + createdBy: McpInvocationContext.mcpMessageActor(scope), creationSource: "mcp", ...(linkOrigin === undefined ? {} : { linkOrigin }), // Kept only alongside linkOrigin: thread.create drops it otherwise. diff --git a/apps/server/src/orchestration-v2/ThreadLaunchService.ts b/apps/server/src/orchestration-v2/ThreadLaunchService.ts index 593393cae48eb..49bac58b67e46 100644 --- a/apps/server/src/orchestration-v2/ThreadLaunchService.ts +++ b/apps/server/src/orchestration-v2/ThreadLaunchService.ts @@ -1431,11 +1431,11 @@ const make = Effect.gen(function* () { creationSource: input.creationSource, }) .pipe( - // An agent's launch can land on a thread that a replay, or a concurrent + // An MCP launch can land on a thread that a replay, or a concurrent // launch with the same key, created with broader modes. The orchestrator // refuses the message under the thread's lock; the user's own clients // launch into their own drafts, whatever modes those hold. - input.createdBy === "agent" + input.creationSource === "mcp" ? Effect.provideService(DispatchModeLimit, { runtimeMode: input.runtimeMode, interactionMode: input.interactionMode, From 4893a73a17340b1df60f082d9a0b4a0f3dc27b2b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 13:22:37 +0000 Subject: [PATCH 2/4] fix(server): only the t3 CLI's own session sends as the user Outside agents (Claude Code, Codex, bots) sign in over OAuth as the same non-linked MCP clients, so treating every such client as the user dropped their sender line and let them pass the person-only provider sign-out. The CLI's session now carries a signed `usr` claim, set only when the CLI issues it in process, and only a client with that claim sends as the user. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/auth/EnvironmentAuth.test.ts | 23 +++++++++++++++++++ apps/server/src/auth/EnvironmentAuth.ts | 5 ++++ apps/server/src/auth/McpOAuth.ts | 1 + apps/server/src/auth/SessionStore.ts | 7 ++++++ apps/server/src/cli/thread.ts | 2 ++ apps/server/src/mcp/McpInvocationContext.ts | 9 ++++---- .../src/mcp/OrchestratorMcpService.test.ts | 14 +++++++++-- .../src/mcp/toolkits/project/handlers.test.ts | 1 - 8 files changed, 54 insertions(+), 8 deletions(-) diff --git a/apps/server/src/auth/EnvironmentAuth.test.ts b/apps/server/src/auth/EnvironmentAuth.test.ts index 6eceb824a8cba..d028db9364e48 100644 --- a/apps/server/src/auth/EnvironmentAuth.test.ts +++ b/apps/server/src/auth/EnvironmentAuth.test.ts @@ -591,6 +591,29 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodePlatformCrypto.layer))( }).pipe(Effect.provide(layerEnvironmentAuth({ mode: "web", host: "192.168.1.50" }))), ); + it.effect("marks only an MCP session issued as the user's own client as acting for them", () => + Effect.gen(function* () { + const serverAuth = yield* EnvironmentAuth.EnvironmentAuth; + const actsForUser = (asUser: boolean) => + serverAuth + .issueMcpClientSession({ + label: asUser ? "t3 thread cli" : "Claude Code", + access: "full-access", + peerLink: false, + ...(asUser ? { actsForUser: true } : {}), + client: requestMetadata, + }) + .pipe( + Effect.flatMap((session) => + serverAuth.authenticateMcpClient(makeBearerRequest(session.token)), + ), + Effect.map((client) => client.actsForUser), + ); + expect(yield* actsForUser(true)).toBe(true); + expect(yield* actsForUser(false)).toBe(false); + }).pipe(Effect.provide(layerEnvironmentAuth({ mode: "web", host: "192.168.1.50" }))), + ); + it.effect("preserves pairing grants after rejecting scopes they do not grant", () => Effect.gen(function* () { const serverAuth = yield* EnvironmentAuth.EnvironmentAuth; diff --git a/apps/server/src/auth/EnvironmentAuth.ts b/apps/server/src/auth/EnvironmentAuth.ts index 202ca25310d13..93d3adbd1efa6 100644 --- a/apps/server/src/auth/EnvironmentAuth.ts +++ b/apps/server/src/auth/EnvironmentAuth.ts @@ -103,6 +103,8 @@ export interface McpClientSession { readonly peerLink: boolean; /** When a peer link's session ends unless it is used again; the linking side keeps this. */ readonly linkExpiresAt?: DateTime.Utc; + /** Issued to a client the user types into, such as the `t3` CLI, never over OAuth. */ + readonly actsForUser: boolean; } export interface AuthenticatedSession { @@ -639,6 +641,7 @@ export class EnvironmentAuth extends Context.Service< readonly label: string; readonly access: AuthMcpClientAccess; readonly peerLink: boolean; + readonly actsForUser?: boolean; readonly client: AuthClientMetadata; }) => Effect.Effect< { @@ -1625,6 +1628,7 @@ export const make = Effect.gen(function* () { ? (session.runtimeModeCeiling ?? "approval-required") : "read-only", peerLink: session.peerLink === true, + actsForUser: session.actsForUser === true, } satisfies McpClientSession; if (session.peerLink !== true || session.expiresAt === undefined) return client; return { @@ -1646,6 +1650,7 @@ export const make = Effect.gen(function* () { ttl: MCP_CLIENT_SESSION_TTL, ...(input.access === "read-only" ? {} : { runtimeModeCeiling: input.access }), peerLink: input.peerLink, + ...(input.actsForUser === true ? { actsForUser: true } : {}), client: { ...input.client, label: input.label, deviceType: "bot" }, }) .pipe( diff --git a/apps/server/src/auth/McpOAuth.ts b/apps/server/src/auth/McpOAuth.ts index 5912adb7b4d6f..a430c32baea22 100644 --- a/apps/server/src/auth/McpOAuth.ts +++ b/apps/server/src/auth/McpOAuth.ts @@ -632,6 +632,7 @@ export const layerMcpClientAuthenticator = Layer.effect( access: client.access, linked: client.peerLink, linkExpiresAt: client.linkExpiresAt, + ...(client.actsForUser ? { actsForUser: true } : {}), ...(client.peerLink ? {} : { hostedAppUrl }), }, capabilities: new Set([ diff --git a/apps/server/src/auth/SessionStore.ts b/apps/server/src/auth/SessionStore.ts index 4057cbdb4540c..73e1423ad4dfb 100644 --- a/apps/server/src/auth/SessionStore.ts +++ b/apps/server/src/auth/SessionStore.ts @@ -65,6 +65,8 @@ export interface VerifiedSession { readonly runtimeModeCeiling?: RuntimeMode; /** An MCP client that is another T3 Code environment's peer link. */ readonly peerLink?: true; + /** An MCP client the user drives directly, such as the `t3` CLI. */ + readonly actsForUser?: true; } export interface VerifiedWebSocketTicket extends VerifiedSession { @@ -412,6 +414,7 @@ export class SessionStore extends Context.Service< readonly proofKeyThumbprint?: string; readonly runtimeModeCeiling?: RuntimeMode; readonly peerLink?: boolean; + readonly actsForUser?: boolean; readonly replaceActiveForSubjectAndMethod?: boolean; readonly replaceSessionId?: AuthSessionId; /** Keeps the session alive this long past each renewal; `ttl` then bounds only the token. */ @@ -503,6 +506,8 @@ const SessionClaims = Schema.Struct({ rtc: Schema.optionalKey(RuntimeMode), /** Signed, so a client cannot shed its peer link's limits. */ plk: Schema.optionalKey(Schema.Literal(1)), + /** Signed, so an OAuth client cannot claim to be the user typing. */ + usr: Schema.optionalKey(Schema.Literal(1)), /** Signed, so only sessions issued as renewable can renew. */ rnw: Schema.optionalKey(Schema.Literal(1)), iat: Schema.Number, @@ -748,6 +753,7 @@ const make = Effect.gen(function* () { ...(input?.proofKeyThumbprint ? { jkt: input.proofKeyThumbprint } : {}), ...(input?.runtimeModeCeiling ? { rtc: input.runtimeModeCeiling } : {}), ...(input?.peerLink === true ? { plk: 1 as const } : {}), + ...(input?.actsForUser === true ? { usr: 1 as const } : {}), ...(input?.renewalWindow ? { rnw: 1 as const } : {}), iat: issuedAt.epochMilliseconds, exp: expiresAt.epochMilliseconds, @@ -931,6 +937,7 @@ const make = Effect.gen(function* () { ...(claims.jkt ? { proofKeyThumbprint: claims.jkt } : {}), ...(claims.rtc ? { runtimeModeCeiling: claims.rtc } : {}), ...(claims.plk === 1 ? { peerLink: true as const } : {}), + ...(claims.usr === 1 ? { actsForUser: true as const } : {}), } satisfies VerifiedSession; }, ); diff --git a/apps/server/src/cli/thread.ts b/apps/server/src/cli/thread.ts index 3566b75e20773..5cea35c819bec 100644 --- a/apps/server/src/cli/thread.ts +++ b/apps/server/src/cli/thread.ts @@ -236,6 +236,8 @@ const withThreadTools = Effect.fn("cli.thread.withThreadTools")(function* diff --git a/apps/server/src/mcp/McpInvocationContext.ts b/apps/server/src/mcp/McpInvocationContext.ts index 7a126d0a8307d..6d6b6849e09a9 100644 --- a/apps/server/src/mcp/McpInvocationContext.ts +++ b/apps/server/src/mcp/McpInvocationContext.ts @@ -52,6 +52,8 @@ export interface McpClientCaller { * another environment passing on the limits of the agent it calls for. */ readonly narrowedTo?: ClientModes | undefined; + /** The user types what this client sends, as in the `t3` CLI; set only by a signed session claim. */ + readonly actsForUser?: boolean | undefined; /** A linked environment's thread this request is made for (`T3-Sender-Thread`). */ readonly senderThread?: OrchestrationV2LinkedSender | undefined; /** An outside client's thread links go through this hosted app, as Copy thread's do. */ @@ -162,14 +164,11 @@ export interface McpInvocationScope { readonly forge?: boolean; } -/** - * Who a message an MCP caller sends comes from. A client the user signed in from outside a thread, - * such as the `t3` CLI, acts for the user; a thread's agent or a linked environment's does not. - */ +/** Outside agents sign in as MCP clients too, so only a client marked as the user's sends as them. */ export const mcpMessageActor = ( scope: Pick, ): OrchestrationV2Actor => - scope.thread === undefined && scope.client !== undefined && scope.client.linked !== true + scope.thread === undefined && scope.client?.actsForUser === true && scope.client.linked !== true ? "user" : "agent"; diff --git a/apps/server/src/mcp/OrchestratorMcpService.test.ts b/apps/server/src/mcp/OrchestratorMcpService.test.ts index 218dc2e775079..1399cb3fca5f5 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.test.ts @@ -5178,8 +5178,18 @@ describe("OrchestratorMcpService thread lookup", () => { const fromBareLink = yield* sendAs(linked()); assert.isUndefined(fromBareLink?.linkedSender); assert.strictEqual(fromBareLink?.createdBy, "agent"); - // A client the user signed in, such as the `t3` CLI, sends as the user. - assert.strictEqual((yield* sendAs(client))?.createdBy, "user"); + // An outside agent signed in over OAuth is not the user; the `t3` CLI's own session is. + assert.strictEqual((yield* sendAs(client))?.createdBy, "agent"); + const typedByUser: McpInvocationScope = { + ...client, + client: { + sessionId: "cli", + label: "t3 thread cli", + access: "full-access", + actsForUser: true, + }, + }; + assert.strictEqual((yield* sendAs(typedByUser))?.createdBy, "user"); }), ); diff --git a/apps/server/src/mcp/toolkits/project/handlers.test.ts b/apps/server/src/mcp/toolkits/project/handlers.test.ts index 9559880c1327f..0664c03f60223 100644 --- a/apps/server/src/mcp/toolkits/project/handlers.test.ts +++ b/apps/server/src/mcp/toolkits/project/handlers.test.ts @@ -990,7 +990,6 @@ it.effect("a client launches with the fallback for an exhausted default", () => modelSelection: fallbackModel, }); expect(launched[0]?.modelSelection).toEqual(fallbackModel); - expect(launched[0]?.createdBy).toBe("user"); }), ); From a7a3b70101354097780cb216a8adf36417e83563 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 13:40:34 +0000 Subject: [PATCH 3/4] fix(server): a client can say a person typed its MCP message A signed-in app such as T3 WoW lets a person type to their threads, but only the CLI's own session could send as the user, so its follow-ups still reached the model as not the user's. t3_thread_send, t3_thread_launch and t3_thread_send_attachments now take `typedByUser`, honored only for a client outside any thread that is not a linked environment; the CLI sets it, replacing the signed session claim. Provider sign-out from chat also refuses MCP messages, so the field cannot unlock it. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/auth/EnvironmentAuth.test.ts | 23 ------------------- apps/server/src/auth/EnvironmentAuth.ts | 5 ---- apps/server/src/auth/McpOAuth.ts | 1 - apps/server/src/auth/SessionStore.ts | 7 ------ apps/server/src/cli/thread.ts | 5 ++-- .../src/mcp/McpInvocationContext.test.ts | 14 +++++++++++ apps/server/src/mcp/McpInvocationContext.ts | 13 +++++++---- .../src/mcp/OrchestratorMcpService.test.ts | 23 ++++--------------- apps/server/src/mcp/OrchestratorMcpService.ts | 2 +- .../src/mcp/toolkits/attachment/handlers.ts | 1 + .../src/mcp/toolkits/attachment/tools.ts | 2 ++ .../src/mcp/toolkits/project/handlers.ts | 2 +- apps/server/src/mcp/toolkits/project/tools.ts | 2 ++ .../ProviderTurnStartService.test.ts | 5 +++- .../ProviderTurnStartService.ts | 4 +++- .../src/provider/agentContextBudget.json | 2 +- packages/contracts/src/orchestratorMcp.ts | 8 +++++++ 17 files changed, 53 insertions(+), 66 deletions(-) diff --git a/apps/server/src/auth/EnvironmentAuth.test.ts b/apps/server/src/auth/EnvironmentAuth.test.ts index d028db9364e48..6eceb824a8cba 100644 --- a/apps/server/src/auth/EnvironmentAuth.test.ts +++ b/apps/server/src/auth/EnvironmentAuth.test.ts @@ -591,29 +591,6 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodePlatformCrypto.layer))( }).pipe(Effect.provide(layerEnvironmentAuth({ mode: "web", host: "192.168.1.50" }))), ); - it.effect("marks only an MCP session issued as the user's own client as acting for them", () => - Effect.gen(function* () { - const serverAuth = yield* EnvironmentAuth.EnvironmentAuth; - const actsForUser = (asUser: boolean) => - serverAuth - .issueMcpClientSession({ - label: asUser ? "t3 thread cli" : "Claude Code", - access: "full-access", - peerLink: false, - ...(asUser ? { actsForUser: true } : {}), - client: requestMetadata, - }) - .pipe( - Effect.flatMap((session) => - serverAuth.authenticateMcpClient(makeBearerRequest(session.token)), - ), - Effect.map((client) => client.actsForUser), - ); - expect(yield* actsForUser(true)).toBe(true); - expect(yield* actsForUser(false)).toBe(false); - }).pipe(Effect.provide(layerEnvironmentAuth({ mode: "web", host: "192.168.1.50" }))), - ); - it.effect("preserves pairing grants after rejecting scopes they do not grant", () => Effect.gen(function* () { const serverAuth = yield* EnvironmentAuth.EnvironmentAuth; diff --git a/apps/server/src/auth/EnvironmentAuth.ts b/apps/server/src/auth/EnvironmentAuth.ts index 93d3adbd1efa6..202ca25310d13 100644 --- a/apps/server/src/auth/EnvironmentAuth.ts +++ b/apps/server/src/auth/EnvironmentAuth.ts @@ -103,8 +103,6 @@ export interface McpClientSession { readonly peerLink: boolean; /** When a peer link's session ends unless it is used again; the linking side keeps this. */ readonly linkExpiresAt?: DateTime.Utc; - /** Issued to a client the user types into, such as the `t3` CLI, never over OAuth. */ - readonly actsForUser: boolean; } export interface AuthenticatedSession { @@ -641,7 +639,6 @@ export class EnvironmentAuth extends Context.Service< readonly label: string; readonly access: AuthMcpClientAccess; readonly peerLink: boolean; - readonly actsForUser?: boolean; readonly client: AuthClientMetadata; }) => Effect.Effect< { @@ -1628,7 +1625,6 @@ export const make = Effect.gen(function* () { ? (session.runtimeModeCeiling ?? "approval-required") : "read-only", peerLink: session.peerLink === true, - actsForUser: session.actsForUser === true, } satisfies McpClientSession; if (session.peerLink !== true || session.expiresAt === undefined) return client; return { @@ -1650,7 +1646,6 @@ export const make = Effect.gen(function* () { ttl: MCP_CLIENT_SESSION_TTL, ...(input.access === "read-only" ? {} : { runtimeModeCeiling: input.access }), peerLink: input.peerLink, - ...(input.actsForUser === true ? { actsForUser: true } : {}), client: { ...input.client, label: input.label, deviceType: "bot" }, }) .pipe( diff --git a/apps/server/src/auth/McpOAuth.ts b/apps/server/src/auth/McpOAuth.ts index a430c32baea22..5912adb7b4d6f 100644 --- a/apps/server/src/auth/McpOAuth.ts +++ b/apps/server/src/auth/McpOAuth.ts @@ -632,7 +632,6 @@ export const layerMcpClientAuthenticator = Layer.effect( access: client.access, linked: client.peerLink, linkExpiresAt: client.linkExpiresAt, - ...(client.actsForUser ? { actsForUser: true } : {}), ...(client.peerLink ? {} : { hostedAppUrl }), }, capabilities: new Set([ diff --git a/apps/server/src/auth/SessionStore.ts b/apps/server/src/auth/SessionStore.ts index 73e1423ad4dfb..4057cbdb4540c 100644 --- a/apps/server/src/auth/SessionStore.ts +++ b/apps/server/src/auth/SessionStore.ts @@ -65,8 +65,6 @@ export interface VerifiedSession { readonly runtimeModeCeiling?: RuntimeMode; /** An MCP client that is another T3 Code environment's peer link. */ readonly peerLink?: true; - /** An MCP client the user drives directly, such as the `t3` CLI. */ - readonly actsForUser?: true; } export interface VerifiedWebSocketTicket extends VerifiedSession { @@ -414,7 +412,6 @@ export class SessionStore extends Context.Service< readonly proofKeyThumbprint?: string; readonly runtimeModeCeiling?: RuntimeMode; readonly peerLink?: boolean; - readonly actsForUser?: boolean; readonly replaceActiveForSubjectAndMethod?: boolean; readonly replaceSessionId?: AuthSessionId; /** Keeps the session alive this long past each renewal; `ttl` then bounds only the token. */ @@ -506,8 +503,6 @@ const SessionClaims = Schema.Struct({ rtc: Schema.optionalKey(RuntimeMode), /** Signed, so a client cannot shed its peer link's limits. */ plk: Schema.optionalKey(Schema.Literal(1)), - /** Signed, so an OAuth client cannot claim to be the user typing. */ - usr: Schema.optionalKey(Schema.Literal(1)), /** Signed, so only sessions issued as renewable can renew. */ rnw: Schema.optionalKey(Schema.Literal(1)), iat: Schema.Number, @@ -753,7 +748,6 @@ const make = Effect.gen(function* () { ...(input?.proofKeyThumbprint ? { jkt: input.proofKeyThumbprint } : {}), ...(input?.runtimeModeCeiling ? { rtc: input.runtimeModeCeiling } : {}), ...(input?.peerLink === true ? { plk: 1 as const } : {}), - ...(input?.actsForUser === true ? { usr: 1 as const } : {}), ...(input?.renewalWindow ? { rnw: 1 as const } : {}), iat: issuedAt.epochMilliseconds, exp: expiresAt.epochMilliseconds, @@ -937,7 +931,6 @@ const make = Effect.gen(function* () { ...(claims.jkt ? { proofKeyThumbprint: claims.jkt } : {}), ...(claims.rtc ? { runtimeModeCeiling: claims.rtc } : {}), ...(claims.plk === 1 ? { peerLink: true as const } : {}), - ...(claims.usr === 1 ? { actsForUser: true as const } : {}), } satisfies VerifiedSession; }, ); diff --git a/apps/server/src/cli/thread.ts b/apps/server/src/cli/thread.ts index 5cea35c819bec..ca485db32f15f 100644 --- a/apps/server/src/cli/thread.ts +++ b/apps/server/src/cli/thread.ts @@ -236,8 +236,6 @@ const withThreadTools = Effect.fn("cli.thread.withThreadTools")(function* @@ -872,6 +870,7 @@ const threadStartCommand = Command.make("start", { projectId: project.id, title, message, + typedByUser: true, ...(workspaceStrategy === undefined ? {} : { workspaceStrategy }), ...(modelSelection === undefined ? {} : { modelSelection }), }, @@ -914,7 +913,7 @@ const threadSendCommand = Command.make("send", { const followWakes = flags.wait ? yield* threadIsIdle(tools, threadId) : false; const sent = yield* tools.call( "t3_thread_send", - { threadId, message, mode: "queue" }, + { threadId, message, mode: "queue", typedByUser: true }, OrchestratorMcpThreadSendResult, ); if (flags.wait) { diff --git a/apps/server/src/mcp/McpInvocationContext.test.ts b/apps/server/src/mcp/McpInvocationContext.test.ts index 96c71fcd773f9..9ed48b791653b 100644 --- a/apps/server/src/mcp/McpInvocationContext.test.ts +++ b/apps/server/src/mcp/McpInvocationContext.test.ts @@ -95,3 +95,17 @@ it("acts at an administrator's ceiling for a client approved above it", () => { expect(modes(scope("auto-accept-edits"), "auto").runtimeMode).toBe("auto-accept-edits"); expect(modes(scope("full-access")).runtimeMode).toBe("full-access"); }); + +it("takes a message as the user's only from an outside client that says a person typed it", () => { + const client = { sessionId: "s", label: "T3 WoW", access: "full-access" } as const; + const thread = { + threadId: ThreadId.make("thread-1"), + providerSessionId: "provider-session-1", + providerInstanceId: ProviderInstanceId.make("codex"), + }; + const actor = McpInvocationContext.mcpMessageActor; + expect(actor({ thread: undefined, client }, true)).toBe("user"); + expect(actor({ thread: undefined, client }, undefined)).toBe("agent"); + expect(actor({ thread: undefined, client: { ...client, linked: true } }, true)).toBe("agent"); + expect(actor({ thread, client: undefined }, true)).toBe("agent"); +}); diff --git a/apps/server/src/mcp/McpInvocationContext.ts b/apps/server/src/mcp/McpInvocationContext.ts index 6d6b6849e09a9..68c8ce912f6d8 100644 --- a/apps/server/src/mcp/McpInvocationContext.ts +++ b/apps/server/src/mcp/McpInvocationContext.ts @@ -52,8 +52,6 @@ export interface McpClientCaller { * another environment passing on the limits of the agent it calls for. */ readonly narrowedTo?: ClientModes | undefined; - /** The user types what this client sends, as in the `t3` CLI; set only by a signed session claim. */ - readonly actsForUser?: boolean | undefined; /** A linked environment's thread this request is made for (`T3-Sender-Thread`). */ readonly senderThread?: OrchestrationV2LinkedSender | undefined; /** An outside client's thread links go through this hosted app, as Copy thread's do. */ @@ -164,11 +162,18 @@ export interface McpInvocationScope { readonly forge?: boolean; } -/** Outside agents sign in as MCP clients too, so only a client marked as the user's sends as them. */ +/** + * Outside agents sign in as MCP clients too, so a client's message is the user's only when it says a + * person typed it. A thread's agent or a linked environment cannot say so. + */ export const mcpMessageActor = ( scope: Pick, + typedByUser: boolean | undefined, ): OrchestrationV2Actor => - scope.thread === undefined && scope.client?.actsForUser === true && scope.client.linked !== true + typedByUser === true && + scope.thread === undefined && + scope.client !== undefined && + scope.client.linked !== true ? "user" : "agent"; diff --git a/apps/server/src/mcp/OrchestratorMcpService.test.ts b/apps/server/src/mcp/OrchestratorMcpService.test.ts index 1399cb3fca5f5..bdf0ff1c35751 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.test.ts @@ -5129,7 +5129,7 @@ describe("OrchestratorMcpService thread lookup", () => { }), ); - it.effect("records whether the user or a linked environment's thread sent a message", () => + it.effect("records who sent a message: a linked environment's thread, or the user", () => Effect.gen(function* () { const target = liveThreadShell(ThreadId.make("thread:target")); const sender = { @@ -5147,13 +5147,13 @@ describe("OrchestratorMcpService thread lookup", () => { ...(senderThread === undefined ? {} : { senderThread }), }, }); - const sendAs = (scope: McpInvocationScope) => + const sendAs = (scope: McpInvocationScope, typedByUser?: true) => Effect.gen(function* () { const inputs: Array = []; const sent = yield* Ref.make(0); yield* Effect.gen(function* () { const mcp = yield* OrchestratorMcpService.OrchestratorMcpService; - yield* mcp.sendToThread(scope, { threadId: target.id, message: "hello" }); + yield* mcp.sendToThread(scope, { threadId: target.id, message: "hello", typedByUser }); }).pipe( Effect.provide( service([target], sent, { @@ -5174,22 +5174,9 @@ describe("OrchestratorMcpService thread lookup", () => { const fromLink = yield* sendAs(linked(sender)); assert.deepStrictEqual(fromLink?.linkedSender, sender); assert.isUndefined(fromLink?.senderThreadId); - assert.strictEqual(fromLink?.createdBy, "agent"); - const fromBareLink = yield* sendAs(linked()); - assert.isUndefined(fromBareLink?.linkedSender); - assert.strictEqual(fromBareLink?.createdBy, "agent"); - // An outside agent signed in over OAuth is not the user; the `t3` CLI's own session is. + assert.isUndefined((yield* sendAs(linked()))?.linkedSender); assert.strictEqual((yield* sendAs(client))?.createdBy, "agent"); - const typedByUser: McpInvocationScope = { - ...client, - client: { - sessionId: "cli", - label: "t3 thread cli", - access: "full-access", - actsForUser: true, - }, - }; - assert.strictEqual((yield* sendAs(typedByUser))?.createdBy, "user"); + assert.strictEqual((yield* sendAs(client, true))?.createdBy, "user"); }), ); diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index a65e0ef77e262..f683e55ec4378 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -3646,7 +3646,7 @@ const make = Effect.gen(function* () { attachments: [], mode, ...(input.freshContext ? { freshContext: true } : {}), - createdBy: mcpMessageActor(scope), + createdBy: mcpMessageActor(scope, input.typedByUser), creationSource: "mcp", }) .pipe( diff --git a/apps/server/src/mcp/toolkits/attachment/handlers.ts b/apps/server/src/mcp/toolkits/attachment/handlers.ts index 6fb6b499d9e0f..88ec345b3460e 100644 --- a/apps/server/src/mcp/toolkits/attachment/handlers.ts +++ b/apps/server/src/mcp/toolkits/attachment/handlers.ts @@ -112,6 +112,7 @@ export const layer = McpToolAccess.toLayer( mode: "auto", createdBy: McpInvocationContext.mcpMessageActor( yield* McpInvocationContext.McpInvocationContext, + input.typedByUser, ), creationSource: "mcp", }).pipe( diff --git a/apps/server/src/mcp/toolkits/attachment/tools.ts b/apps/server/src/mcp/toolkits/attachment/tools.ts index 59cecace84386..dd2850df42ceb 100644 --- a/apps/server/src/mcp/toolkits/attachment/tools.ts +++ b/apps/server/src/mcp/toolkits/attachment/tools.ts @@ -8,6 +8,7 @@ import { ThreadId, OrchestrationV2RunStatus, OrchestratorMcpFailure, + OrchestratorMcpTypedByUser, } from "@t3tools/contracts"; import * as SecureRandom from "effect/crypto/SecureRandom"; import * as FileSystem from "effect/FileSystem"; @@ -59,6 +60,7 @@ const AttachmentSendTool = Tool.make("t3_thread_send_attachments", { Schema.isMinLength(1), Schema.isMaxLength(8), ), + typedByUser: OrchestratorMcpTypedByUser, }), success: Schema.Struct({ threadId: ThreadId, diff --git a/apps/server/src/mcp/toolkits/project/handlers.ts b/apps/server/src/mcp/toolkits/project/handlers.ts index 2770fdbebe2bc..b26cfd2f166ad 100644 --- a/apps/server/src/mcp/toolkits/project/handlers.ts +++ b/apps/server/src/mcp/toolkits/project/handlers.ts @@ -429,7 +429,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { attachments, }, }), - createdBy: McpInvocationContext.mcpMessageActor(scope), + createdBy: McpInvocationContext.mcpMessageActor(scope, input.typedByUser), creationSource: "mcp", ...(linkOrigin === undefined ? {} : { linkOrigin }), // Kept only alongside linkOrigin: thread.create drops it otherwise. diff --git a/apps/server/src/mcp/toolkits/project/tools.ts b/apps/server/src/mcp/toolkits/project/tools.ts index 57ba4467fd9de..4483ac2873b27 100644 --- a/apps/server/src/mcp/toolkits/project/tools.ts +++ b/apps/server/src/mcp/toolkits/project/tools.ts @@ -16,6 +16,7 @@ import { ProjectUpdatePayload, ProjectId, OrchestratorMcpClientRequestId, + OrchestratorMcpTypedByUser, OrchestratorMcpEnvironmentTarget, OrchestratorMcpFailure, SourceControlCloneRepositoryInput, @@ -157,6 +158,7 @@ const ThreadLaunchTool = Tool.make("t3_thread_launch", { ), attachments: Schema.optional(Schema.Array(McpAttachmentInput).check(Schema.isMaxLength(8))), clientRequestId: Schema.optional(OrchestratorMcpClientRequestId), + typedByUser: OrchestratorMcpTypedByUser, /** Set by a linked environment launching its agent's delegated task here; ignored otherwise. */ delegatedFrom: Schema.optional(OrchestrationV2DelegatedFrom), }), diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts index e4edafd70942b..fe0f6d7857d93 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts @@ -468,6 +468,7 @@ function makeLocalCommandHarness(input: { readonly previousNativeSession?: boolean; readonly previousMessages?: ReadonlyArray; readonly createdBy?: "agent" | "system"; + readonly creationSource?: "mcp"; readonly scheduledTaskId?: ScheduledTaskId; readonly logoutFailure?: string; readonly openFailure?: unknown; @@ -568,7 +569,7 @@ function makeLocalCommandHarness(input: { attachments: [], streaming: false, createdBy: input.createdBy ?? "user", - creationSource: input.createdBy === undefined ? "web" : "mcp", + creationSource: input.creationSource ?? (input.createdBy === undefined ? "web" : "mcp"), ...(input.scheduledTaskId === undefined ? {} : { scheduledTaskId: input.scheduledTaskId }), createdAt: now, updatedAt: now, @@ -3332,6 +3333,8 @@ effectIt.effect.each([ { name: "an agent", createdBy: "agent" as const }, // A scheduled delivery keeps its task author's createdBy. { name: "a scheduled task", scheduledTaskId: ScheduledTaskId.make("task") }, + // An MCP client's `typedByUser` is its own word, so it does not reach sign-out. + { name: "an MCP client's message typed by the user", creationSource: "mcp" as const }, ])("does not let $name sign a provider out", ({ name: _name, ...sender }) => Effect.gen(function* () { const harness = makeLocalCommandHarness({ diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index d423d0c2ea675..ccfbb0d3b9007 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -742,9 +742,11 @@ export const layer: Layer.Layer< records: providerMessage.context?.records ?? [], }), hasAttachments: false, - // A scheduled delivery keeps its task author's createdBy. + // A scheduled delivery keeps its task author's createdBy. An MCP client's own + // word that a person typed a message does not reach provider sign-out. sentByPerson: providerMessage.createdBy === "user" && + providerMessage.creationSource !== "mcp" && providerMessage.scheduledTaskId === undefined, }), ); diff --git a/apps/server/src/provider/agentContextBudget.json b/apps/server/src/provider/agentContextBudget.json index 267fff59a6021..73fc90731c1d4 100644 --- a/apps/server/src/provider/agentContextBudget.json +++ b/apps/server/src/provider/agentContextBudget.json @@ -1,5 +1,5 @@ { "claudeSystemAppendBytes": 6245, "codexAdditionalContextBytes": 7639, - "mcpToolListJsonBytes": 57299 + "mcpToolListJsonBytes": 57629 } diff --git a/packages/contracts/src/orchestratorMcp.ts b/packages/contracts/src/orchestratorMcp.ts index d7c607744d4a7..c2af3461eca58 100644 --- a/packages/contracts/src/orchestratorMcp.ts +++ b/packages/contracts/src/orchestratorMcp.ts @@ -550,6 +550,13 @@ export const OrchestratorMcpThreadReadResult = Schema.Struct({ }); export type OrchestratorMcpThreadReadResult = typeof OrchestratorMcpThreadReadResult.Type; +/** Honored only for a client outside any T3 thread that is not a linked environment. */ +export const OrchestratorMcpTypedByUser = Schema.optional( + Schema.Boolean.annotate({ + description: "Outside a T3 thread: a person typed this message as written.", + }), +); + export const OrchestratorMcpThreadSendInput = Schema.Struct({ environmentId: OrchestratorMcpEnvironmentTarget, threadId: ThreadId, @@ -562,6 +569,7 @@ export const OrchestratorMcpThreadSendInput = Schema.Struct({ }), ), clientRequestId: Schema.optional(OrchestratorMcpClientRequestId), + typedByUser: OrchestratorMcpTypedByUser, }); export type OrchestratorMcpThreadSendInput = typeof OrchestratorMcpThreadSendInput.Type; From 53ea9600d21148aa1bb459c104adfbccfaa48515 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 14:12:13 +0000 Subject: [PATCH 4/4] fix(server): say a person typed an MCP message with a header, not a tool field The `typedByUser` tool field grew the tool list every agent reads by 330 bytes. A client now sends `T3-Typed-By-User: 1` with the request instead: it costs agents nothing, and an agent cannot set it through tool arguments. `/mcp` reads it into the client scope, `mcpMessageActor` honors it only for a client outside any thread that is not a linked environment, and the `t3` CLI sends it on its own session. The tool schemas and the agent context budget are back to main's. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/cli/thread.ts | 6 ++++-- apps/server/src/mcp/AcpMcpStdioBridge.ts | 2 ++ apps/server/src/mcp/McpHttpServer.test.ts | 18 +++++++++++++++--- apps/server/src/mcp/McpHttpServer.ts | 14 +++++++++++++- .../src/mcp/McpInvocationContext.test.ts | 9 +++++---- apps/server/src/mcp/McpInvocationContext.ts | 16 ++++++++++++---- .../src/mcp/OrchestratorMcpService.test.ts | 10 +++++++--- apps/server/src/mcp/OrchestratorMcpService.ts | 2 +- .../src/mcp/toolkits/attachment/handlers.ts | 1 - .../src/mcp/toolkits/attachment/tools.ts | 2 -- .../src/mcp/toolkits/project/handlers.ts | 2 +- apps/server/src/mcp/toolkits/project/tools.ts | 2 -- .../ProviderTurnStartService.test.ts | 2 +- .../src/provider/agentContextBudget.json | 2 +- docs/user/outside-agents.md | 3 +++ packages/contracts/src/orchestratorMcp.ts | 8 -------- 16 files changed, 65 insertions(+), 34 deletions(-) diff --git a/apps/server/src/cli/thread.ts b/apps/server/src/cli/thread.ts index ca485db32f15f..c33ac052d9d83 100644 --- a/apps/server/src/cli/thread.ts +++ b/apps/server/src/cli/thread.ts @@ -30,6 +30,7 @@ import { Argument, Command, Flag, GlobalFlag } from "effect/cli"; import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts"; import * as ServerConfig from "../config.ts"; import { mcpToolSession, type McpToolSession } from "../mcp/AcpMcpStdioBridge.ts"; +import { TYPED_BY_USER_HEADER } from "../mcp/McpInvocationContext.ts"; import { expandHomePath } from "../os-jank.ts"; import * as ProcessRunner from "../processRunner.ts"; import { readLiveServerRuntimeState } from "../serverRuntimeState.ts"; @@ -245,6 +246,8 @@ const withThreadTools = Effect.fn("cli.thread.withThreadTools")(function* new ThreadServerUnreachableError({ origin, cause }))); return yield* run(makeThreadTools(session, origin)); }).pipe( @@ -870,7 +873,6 @@ const threadStartCommand = Command.make("start", { projectId: project.id, title, message, - typedByUser: true, ...(workspaceStrategy === undefined ? {} : { workspaceStrategy }), ...(modelSelection === undefined ? {} : { modelSelection }), }, @@ -913,7 +915,7 @@ const threadSendCommand = Command.make("send", { const followWakes = flags.wait ? yield* threadIsIdle(tools, threadId) : false; const sent = yield* tools.call( "t3_thread_send", - { threadId, message, mode: "queue", typedByUser: true }, + { threadId, message, mode: "queue" }, OrchestratorMcpThreadSendResult, ); if (flags.wait) { diff --git a/apps/server/src/mcp/AcpMcpStdioBridge.ts b/apps/server/src/mcp/AcpMcpStdioBridge.ts index ffddcf7d5866b..07742d135b47d 100644 --- a/apps/server/src/mcp/AcpMcpStdioBridge.ts +++ b/apps/server/src/mcp/AcpMcpStdioBridge.ts @@ -68,6 +68,7 @@ export interface McpToolSessionOptions { readonly endpoint: string; readonly authorization: string; readonly clientName: string; + readonly headers?: Readonly>; readonly fetchImplementation?: (url: string, init?: RequestInit) => Promise; } @@ -92,6 +93,7 @@ export const mcpToolSession = ( let nextId = 0; const headers = () => ({ + ...options.headers, accept: "application/json, text/event-stream", authorization: options.authorization, ...(sessionId === null ? {} : { "mcp-session-id": sessionId }), diff --git a/apps/server/src/mcp/McpHttpServer.test.ts b/apps/server/src/mcp/McpHttpServer.test.ts index e5d47410817f2..2668d44527619 100644 --- a/apps/server/src/mcp/McpHttpServer.test.ts +++ b/apps/server/src/mcp/McpHttpServer.test.ts @@ -1881,7 +1881,7 @@ it.effect( ); it.effect( - "narrows an OAuth client's modes to the T3-Mode-Limit it sends, and never widens them", + "narrows an OAuth client's modes to the T3-Mode-Limit it sends, and reads T3-Typed-By-User", () => Effect.scoped( Effect.gen(function* () { @@ -1918,7 +1918,9 @@ it.effect( starts: McpToolAccess.startsThreads( (input) => input, (_, modes) => - Effect.succeed({ modes: `${modes.runtimeMode}/${modes.interactionMode}` }), + Effect.map(McpInvocationContext.McpInvocationContext, (scope) => ({ + modes: `${modes.runtimeMode}/${modes.interactionMode}${McpInvocationContext.mcpMessageActor(scope) === "user" ? " by user" : ""}`, + })), "stamped", ), }), @@ -1964,9 +1966,14 @@ it.effect( ), ); /** Starts a thread as the client, with `limit` as T3-Mode-Limit; the HTTP status or the outcome. */ - const start = (limit: string | undefined, args: Record = {}) => + const start = ( + limit: string | undefined, + args: Record = {}, + extraHeaders: Record = {}, + ) => Effect.gen(function* () { const headers = { + ...extraHeaders, accept: "application/json, text/event-stream", authorization: `Bearer ${clientToken}`, ...(limit === undefined ? {} : { "t3-mode-limit": limit }), @@ -2021,6 +2028,11 @@ it.effect( expect(yield* start("auto")).toBe(400); expect(yield* start("auto/default/extra")).toBe(400); expect(yield* start("root/default")).toBe(400); + // Only a client that says a person typed the request's message sends as the user. + expect(yield* start(undefined, {}, { "t3-typed-by-user": "1" })).toBe( + "auto/default by user", + ); + expect(yield* start(undefined, {}, { "t3-typed-by-user": "yes" })).toBe("auto/default"); }), ).pipe(Effect.provide(NodeHttpServer.layerTest)), ); diff --git a/apps/server/src/mcp/McpHttpServer.ts b/apps/server/src/mcp/McpHttpServer.ts index d7bea75493b73..3d31b062077bd 100644 --- a/apps/server/src/mcp/McpHttpServer.ts +++ b/apps/server/src/mcp/McpHttpServer.ts @@ -264,6 +264,15 @@ const withSenderThread = ( : { ...invocation, client: { ...invocation.client, senderThread: decoded.value } }; }; +const withTypedByUser = ( + invocation: McpInvocationContext.McpInvocationScope, + request: HttpServerRequest.HttpServerRequest, +): McpInvocationContext.McpInvocationScope => + request.headers[McpInvocationContext.TYPED_BY_USER_HEADER]?.trim() === "1" && + invocation.client !== undefined + ? { ...invocation, client: { ...invocation.client, typedByUser: true } } + : invocation; + // The Linked environments beta is off here: links wait until it is on again. const linkedEnvironmentsOff = HttpServerResponse.jsonUnsafe( { @@ -327,7 +336,10 @@ const makeMcpAuthMiddleware = Effect.gen(function* () { offerOAuth: Option.isSome(clients) && !looksLikeProviderToken(token), }); } - const asked = withModeLimit(withSenderThread(invocation, request), request); + const asked = withModeLimit( + withTypedByUser(withSenderThread(invocation, request), request), + request, + ); if (asked === undefined) return invalidModeLimit; const limited = McpInvocationContext.withRuntimeModeCeiling(asked, runtimeModeCeiling); const linkedEnvironments = yield* linksEnabled; diff --git a/apps/server/src/mcp/McpInvocationContext.test.ts b/apps/server/src/mcp/McpInvocationContext.test.ts index 9ed48b791653b..5eba947cb8d41 100644 --- a/apps/server/src/mcp/McpInvocationContext.test.ts +++ b/apps/server/src/mcp/McpInvocationContext.test.ts @@ -103,9 +103,10 @@ it("takes a message as the user's only from an outside client that says a person providerSessionId: "provider-session-1", providerInstanceId: ProviderInstanceId.make("codex"), }; + const typed = { ...client, typedByUser: true }; const actor = McpInvocationContext.mcpMessageActor; - expect(actor({ thread: undefined, client }, true)).toBe("user"); - expect(actor({ thread: undefined, client }, undefined)).toBe("agent"); - expect(actor({ thread: undefined, client: { ...client, linked: true } }, true)).toBe("agent"); - expect(actor({ thread, client: undefined }, true)).toBe("agent"); + expect(actor({ thread: undefined, client: typed })).toBe("user"); + expect(actor({ thread: undefined, client })).toBe("agent"); + expect(actor({ thread: undefined, client: { ...typed, linked: true } })).toBe("agent"); + expect(actor({ thread, client: typed })).toBe("agent"); }); diff --git a/apps/server/src/mcp/McpInvocationContext.ts b/apps/server/src/mcp/McpInvocationContext.ts index 68c8ce912f6d8..d46f4a28ca59e 100644 --- a/apps/server/src/mcp/McpInvocationContext.ts +++ b/apps/server/src/mcp/McpInvocationContext.ts @@ -52,6 +52,8 @@ export interface McpClientCaller { * another environment passing on the limits of the agent it calls for. */ readonly narrowedTo?: ClientModes | undefined; + /** The request says a person typed what it sends (`T3-Typed-By-User`). */ + readonly typedByUser?: boolean | undefined; /** A linked environment's thread this request is made for (`T3-Sender-Thread`). */ readonly senderThread?: OrchestrationV2LinkedSender | undefined; /** An outside client's thread links go through this hosted app, as Copy thread's do. */ @@ -79,6 +81,13 @@ export const LINK_EXPIRES_AT_HEADER = "t3-link-expires-at"; */ export const SENDER_THREAD_HEADER = "t3-sender-thread"; +/** + * `T3-Typed-By-User: 1`: a person typed the message this request sends, as in the `t3` CLI or an + * app that passes on what its user types. Honored only from a client outside any thread that is + * not a linked environment (`mcpMessageActor`). + */ +export const TYPED_BY_USER_HEADER = "t3-typed-by-user"; + export interface ClientModes { readonly runtimeMode: RuntimeMode; readonly interactionMode: ProviderInteractionMode; @@ -163,14 +172,13 @@ export interface McpInvocationScope { } /** - * Outside agents sign in as MCP clients too, so a client's message is the user's only when it says a - * person typed it. A thread's agent or a linked environment cannot say so. + * Outside agents sign in as MCP clients too, so a client's message is the user's only on a request + * that says a person typed it. A thread's agent or a linked environment cannot say so. */ export const mcpMessageActor = ( scope: Pick, - typedByUser: boolean | undefined, ): OrchestrationV2Actor => - typedByUser === true && + scope.client?.typedByUser === true && scope.thread === undefined && scope.client !== undefined && scope.client.linked !== true diff --git a/apps/server/src/mcp/OrchestratorMcpService.test.ts b/apps/server/src/mcp/OrchestratorMcpService.test.ts index bdf0ff1c35751..c3bf37680cce9 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.test.ts @@ -5147,13 +5147,13 @@ describe("OrchestratorMcpService thread lookup", () => { ...(senderThread === undefined ? {} : { senderThread }), }, }); - const sendAs = (scope: McpInvocationScope, typedByUser?: true) => + const sendAs = (scope: McpInvocationScope) => Effect.gen(function* () { const inputs: Array = []; const sent = yield* Ref.make(0); yield* Effect.gen(function* () { const mcp = yield* OrchestratorMcpService.OrchestratorMcpService; - yield* mcp.sendToThread(scope, { threadId: target.id, message: "hello", typedByUser }); + yield* mcp.sendToThread(scope, { threadId: target.id, message: "hello" }); }).pipe( Effect.provide( service([target], sent, { @@ -5176,7 +5176,11 @@ describe("OrchestratorMcpService thread lookup", () => { assert.isUndefined(fromLink?.senderThreadId); assert.isUndefined((yield* sendAs(linked()))?.linkedSender); assert.strictEqual((yield* sendAs(client))?.createdBy, "agent"); - assert.strictEqual((yield* sendAs(client, true))?.createdBy, "user"); + const typed: McpInvocationScope = { + ...client, + client: { sessionId: "app", label: "T3 WoW", access: "full-access", typedByUser: true }, + }; + assert.strictEqual((yield* sendAs(typed))?.createdBy, "user"); }), ); diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index f683e55ec4378..a65e0ef77e262 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -3646,7 +3646,7 @@ const make = Effect.gen(function* () { attachments: [], mode, ...(input.freshContext ? { freshContext: true } : {}), - createdBy: mcpMessageActor(scope, input.typedByUser), + createdBy: mcpMessageActor(scope), creationSource: "mcp", }) .pipe( diff --git a/apps/server/src/mcp/toolkits/attachment/handlers.ts b/apps/server/src/mcp/toolkits/attachment/handlers.ts index 88ec345b3460e..6fb6b499d9e0f 100644 --- a/apps/server/src/mcp/toolkits/attachment/handlers.ts +++ b/apps/server/src/mcp/toolkits/attachment/handlers.ts @@ -112,7 +112,6 @@ export const layer = McpToolAccess.toLayer( mode: "auto", createdBy: McpInvocationContext.mcpMessageActor( yield* McpInvocationContext.McpInvocationContext, - input.typedByUser, ), creationSource: "mcp", }).pipe( diff --git a/apps/server/src/mcp/toolkits/attachment/tools.ts b/apps/server/src/mcp/toolkits/attachment/tools.ts index dd2850df42ceb..59cecace84386 100644 --- a/apps/server/src/mcp/toolkits/attachment/tools.ts +++ b/apps/server/src/mcp/toolkits/attachment/tools.ts @@ -8,7 +8,6 @@ import { ThreadId, OrchestrationV2RunStatus, OrchestratorMcpFailure, - OrchestratorMcpTypedByUser, } from "@t3tools/contracts"; import * as SecureRandom from "effect/crypto/SecureRandom"; import * as FileSystem from "effect/FileSystem"; @@ -60,7 +59,6 @@ const AttachmentSendTool = Tool.make("t3_thread_send_attachments", { Schema.isMinLength(1), Schema.isMaxLength(8), ), - typedByUser: OrchestratorMcpTypedByUser, }), success: Schema.Struct({ threadId: ThreadId, diff --git a/apps/server/src/mcp/toolkits/project/handlers.ts b/apps/server/src/mcp/toolkits/project/handlers.ts index b26cfd2f166ad..2770fdbebe2bc 100644 --- a/apps/server/src/mcp/toolkits/project/handlers.ts +++ b/apps/server/src/mcp/toolkits/project/handlers.ts @@ -429,7 +429,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { attachments, }, }), - createdBy: McpInvocationContext.mcpMessageActor(scope, input.typedByUser), + createdBy: McpInvocationContext.mcpMessageActor(scope), creationSource: "mcp", ...(linkOrigin === undefined ? {} : { linkOrigin }), // Kept only alongside linkOrigin: thread.create drops it otherwise. diff --git a/apps/server/src/mcp/toolkits/project/tools.ts b/apps/server/src/mcp/toolkits/project/tools.ts index 4483ac2873b27..57ba4467fd9de 100644 --- a/apps/server/src/mcp/toolkits/project/tools.ts +++ b/apps/server/src/mcp/toolkits/project/tools.ts @@ -16,7 +16,6 @@ import { ProjectUpdatePayload, ProjectId, OrchestratorMcpClientRequestId, - OrchestratorMcpTypedByUser, OrchestratorMcpEnvironmentTarget, OrchestratorMcpFailure, SourceControlCloneRepositoryInput, @@ -158,7 +157,6 @@ const ThreadLaunchTool = Tool.make("t3_thread_launch", { ), attachments: Schema.optional(Schema.Array(McpAttachmentInput).check(Schema.isMaxLength(8))), clientRequestId: Schema.optional(OrchestratorMcpClientRequestId), - typedByUser: OrchestratorMcpTypedByUser, /** Set by a linked environment launching its agent's delegated task here; ignored otherwise. */ delegatedFrom: Schema.optional(OrchestrationV2DelegatedFrom), }), diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts index fe0f6d7857d93..005043b622095 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts @@ -3333,7 +3333,7 @@ effectIt.effect.each([ { name: "an agent", createdBy: "agent" as const }, // A scheduled delivery keeps its task author's createdBy. { name: "a scheduled task", scheduledTaskId: ScheduledTaskId.make("task") }, - // An MCP client's `typedByUser` is its own word, so it does not reach sign-out. + // An MCP client's word that a person typed it (`T3-Typed-By-User`) does not reach sign-out. { name: "an MCP client's message typed by the user", creationSource: "mcp" as const }, ])("does not let $name sign a provider out", ({ name: _name, ...sender }) => Effect.gen(function* () { diff --git a/apps/server/src/provider/agentContextBudget.json b/apps/server/src/provider/agentContextBudget.json index 73fc90731c1d4..267fff59a6021 100644 --- a/apps/server/src/provider/agentContextBudget.json +++ b/apps/server/src/provider/agentContextBudget.json @@ -1,5 +1,5 @@ { "claudeSystemAppendBytes": 6245, "codexAdditionalContextBytes": 7639, - "mcpToolListJsonBytes": 57629 + "mcpToolListJsonBytes": 57299 } diff --git a/docs/user/outside-agents.md b/docs/user/outside-agents.md index 97bc0d385a3d7..df0913cadb2d2 100644 --- a/docs/user/outside-agents.md +++ b/docs/user/outside-agents.md @@ -95,6 +95,9 @@ T3 Code. `projectId` or `threadId`. - To list threads across projects, call `t3_thread_list` with `allProjects: true`, or use `t3_thread_search`. +- Messages an outside agent sends reach the thread's agent marked as not from + you. An app that passes on what you type sends the `T3-Typed-By-User: 1` + header with those requests, and the thread's agent then reads them as yours. ## Claude Code diff --git a/packages/contracts/src/orchestratorMcp.ts b/packages/contracts/src/orchestratorMcp.ts index c2af3461eca58..d7c607744d4a7 100644 --- a/packages/contracts/src/orchestratorMcp.ts +++ b/packages/contracts/src/orchestratorMcp.ts @@ -550,13 +550,6 @@ export const OrchestratorMcpThreadReadResult = Schema.Struct({ }); export type OrchestratorMcpThreadReadResult = typeof OrchestratorMcpThreadReadResult.Type; -/** Honored only for a client outside any T3 thread that is not a linked environment. */ -export const OrchestratorMcpTypedByUser = Schema.optional( - Schema.Boolean.annotate({ - description: "Outside a T3 thread: a person typed this message as written.", - }), -); - export const OrchestratorMcpThreadSendInput = Schema.Struct({ environmentId: OrchestratorMcpEnvironmentTarget, threadId: ThreadId, @@ -569,7 +562,6 @@ export const OrchestratorMcpThreadSendInput = Schema.Struct({ }), ), clientRequestId: Schema.optional(OrchestratorMcpClientRequestId), - typedByUser: OrchestratorMcpTypedByUser, }); export type OrchestratorMcpThreadSendInput = typeof OrchestratorMcpThreadSendInput.Type;