From be243ae91a495fd181533afdfbae80db027bcf01 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 20 Jun 2026 22:38:33 -0700 Subject: [PATCH] Remove MCP credential expiration --- ...rchestration-v2-application-integration.md | 1 + .../server/src/mcp/McpSessionRegistry.test.ts | 14 +++--- .../src/mcp/McpSessionRegistry.testkit.ts | 1 - apps/server/src/mcp/McpSessionRegistry.ts | 45 +++---------------- ...OrchestratorMcpToolkit.integration.test.ts | 1 - 5 files changed, 14 insertions(+), 48 deletions(-) diff --git a/.plans/21-orchestration-v2-application-integration.md b/.plans/21-orchestration-v2-application-integration.md index 09cc3b5ada..3e5811e836 100644 --- a/.plans/21-orchestration-v2-application-integration.md +++ b/.plans/21-orchestration-v2-application-integration.md @@ -171,6 +171,7 @@ Make process sharing a provider capability, not an orchestrator special case: - when it is false, allocate an isolated provider session per app thread - Codex enables sharing now; ACP, Claude, Cursor, and OpenCode remain isolated until their adapters are independently proven multiplex-safe - model selection, cwd, runtime policy, and MCP authorization are applied on thread start/resume/fork rather than process launch +- MCP bearer credentials have no idle or maximum lifetime; reissue, thread detachment, provider-session release, and server shutdown revoke them explicitly - the manager tracks the loaded native thread configuration per app thread, so reattachment or a model/cwd/policy change reapplies thread-scoped settings without redundantly resuming unchanged threads - provider-session projections use explicit many-to-many thread bindings - archiving, deleting, or switching one thread detaches that binding; it does not release a shared runtime used by sibling threads diff --git a/apps/server/src/mcp/McpSessionRegistry.test.ts b/apps/server/src/mcp/McpSessionRegistry.test.ts index 5877b636ac..43d4dd1d2b 100644 --- a/apps/server/src/mcp/McpSessionRegistry.test.ts +++ b/apps/server/src/mcp/McpSessionRegistry.test.ts @@ -21,10 +21,7 @@ const fakeEnvironment = ServerEnvironment.ServerEnvironment.of({ const makeRegistry = (now: () => number, httpServer = fakeHttpServer) => McpSessionRegistry.__testing - .make({ - now, - livenessWindowMs: 100, - }) + .make({ now }) .pipe( Effect.provideService(HttpServer.HttpServer, httpServer), Effect.provideService(ServerEnvironment.ServerEnvironment, fakeEnvironment), @@ -75,7 +72,7 @@ it.effect("builds MCP endpoints from the bound server host", () => }), ); -it.effect("expires credentials once their session stops showing signs of life", () => +it.effect("keeps credentials valid until they are explicitly revoked", () => Effect.gen(function* () { let timestamp = 1_000; const registry = yield* makeRegistry(() => timestamp); @@ -84,7 +81,12 @@ it.effect("expires credentials once their session stops showing signs of life", providerInstanceId: ProviderInstanceId.make("claude"), }); const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, ""); - timestamp += 101; + const resolved = yield* registry.resolve(token); + expect(resolved?.providerSessionId).toBe(issued.config.providerSessionId); + timestamp += 365 * 24 * 60 * 60 * 1_000; + expect(yield* registry.resolve(token)).toEqual(resolved); + + yield* registry.revokeProviderSession(issued.config.providerSessionId); expect(yield* registry.resolve(token)).toBeUndefined(); }), ); diff --git a/apps/server/src/mcp/McpSessionRegistry.testkit.ts b/apps/server/src/mcp/McpSessionRegistry.testkit.ts index 8c9818ce03..71b0913274 100644 --- a/apps/server/src/mcp/McpSessionRegistry.testkit.ts +++ b/apps/server/src/mcp/McpSessionRegistry.testkit.ts @@ -17,7 +17,6 @@ export const layer = Layer.succeed( endpoint: "http://127.0.0.1/mcp", authorizationHeader: `Bearer mcp-test:${threadId}`, }, - expiresAt: Number.MAX_SAFE_INTEGER, }), resolve: () => Effect.succeed(undefined), revokeProviderSession: () => Effect.void, diff --git a/apps/server/src/mcp/McpSessionRegistry.ts b/apps/server/src/mcp/McpSessionRegistry.ts index f99814c0ab..b52b2018c1 100644 --- a/apps/server/src/mcp/McpSessionRegistry.ts +++ b/apps/server/src/mcp/McpSessionRegistry.ts @@ -21,6 +21,7 @@ export interface McpIssuedCredential { } export interface McpSessionRegistryShape { + /** Credentials have no time-based expiry and must be explicitly revoked by their owner. */ readonly issue: (request: McpCredentialRequest) => Effect.Effect; readonly resolve: ( rawToken: string, @@ -42,9 +43,7 @@ export class McpSessionRegistry extends Context.Service< >()("t3/mcp/McpSessionRegistry") {} interface CredentialRecord { - readonly tokenHash: string; readonly scope: McpInvocationContext.McpInvocationScope; - readonly lastAliveAt: number; } interface RegistryState { @@ -52,26 +51,9 @@ interface RegistryState { } export interface McpSessionRegistryOptions { - readonly livenessWindowMs?: number; readonly now?: () => number; } -/** - * How long a credential outlives the last sign of life from its provider - * session. - * - * Liveness is refreshed both by MCP traffic and by `touch` on every provider - * turn, so a session that is still doing work never expires no matter how long - * it goes between browser tool calls. This window therefore only bounds - * credentials whose session died without a clean stop — the normal paths - * (`stopSession`, `stopAll`) revoke eagerly and do not wait for it. - * - * The bound matters because `/mcp` is mounted outside the environment auth - * stack and is reachable on whatever host the server binds to, so this token is - * the only thing guarding the preview toolkit on a remote-reachable server. - */ -const DEFAULT_LIVENESS_WINDOW_MS = 24 * 60 * 60 * 1_000; - const bytesToHex = (bytes: Uint8Array): string => Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join(""); @@ -97,7 +79,6 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* ( const httpServer = yield* HttpServer.HttpServer; const state = yield* SynchronizedRef.make({ records: new Map() }); const currentTimeMillis = options.now ? Effect.sync(options.now) : Clock.currentTimeMillis; - const livenessWindowMs = options.livenessWindowMs ?? DEFAULT_LIVENESS_WINDOW_MS; const endpoint = httpServer.address._tag === "TcpAddress" ? `http://${getHttpMcpEndpointHost(httpServer.address.hostname)}:${httpServer.address.port}/mcp` @@ -108,15 +89,6 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* ( .digest("SHA-256", new TextEncoder().encode(token)) .pipe(Effect.map(bytesToHex), Effect.orDie); - const pruneDead = (records: ReadonlyMap, timestamp: number) => { - const next = new Map( - Array.from(records).filter( - ([, record]) => timestamp - record.lastAliveAt <= livenessWindowMs, - ), - ); - return next.size === records.size ? records : next; - }; - const issue: McpSessionRegistryShape["issue"] = Effect.fn("McpSessionRegistry.issue")( function* (request) { const issuedAt = yield* currentTimeMillis; @@ -132,8 +104,8 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* ( issuedAt, }; yield* SynchronizedRef.update(state, ({ records }) => { - const next = new Map(pruneDead(records, issuedAt)); - next.set(tokenHash, { tokenHash, scope, lastAliveAt: issuedAt }); + const next = new Map(records); + next.set(tokenHash, { scope }); return { records: next }; }); return { @@ -153,15 +125,8 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* ( function* (rawToken) { if (rawToken.length === 0) return undefined; const tokenHash = yield* hashToken(rawToken); - const timestamp = yield* currentTimeMillis; - return yield* SynchronizedRef.modify(state, ({ records }) => { - const current = pruneDead(records, timestamp); - const record = current.get(tokenHash); - if (!record) return [undefined, { records: current }] as const; - const next = new Map(current); - next.set(tokenHash, { ...record, lastAliveAt: timestamp }); - return [record.scope, { records: next }] as const; - }); + const record = (yield* SynchronizedRef.get(state)).records.get(tokenHash); + return record?.scope; }, ); diff --git a/apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts b/apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts index 1a7574d067..146a0faa08 100644 --- a/apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts @@ -454,7 +454,6 @@ describe("orchestrator MCP toolkit", () => { providerInstanceId: codexInstanceId, capabilities: new Set(["orchestration"]), issuedAt: 1, - expiresAt: Number.MAX_SAFE_INTEGER, }; const invoke = (name: string, args: Record) => server