Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
Keep MCP credentials alive across provider turns (#4659)
Julius Marminge0e19c103ef07
diff --git a/apps/server/src/mcp/McpHttpServer.test.ts b/apps/server/src/mcp/McpHttpServer.test.tsindex ca3341be7..e1cdf992f 100644--- a/apps/server/src/mcp/McpHttpServer.test.ts+++ b/apps/server/src/mcp/McpHttpServer.test.ts@@ -23,7 +23,6 @@ const invocation = {providerInstanceId: ProviderInstanceId.make("codex"),capabilities: new Set(["preview"] as const),issuedAt: 1,- expiresAt: Number.MAX_SAFE_INTEGER,};const client = McpSchema.McpServerClient.of({clientId: 1,diff --git a/apps/server/src/mcp/McpHttpServer.ts b/apps/server/src/mcp/McpHttpServer.tsindex 6774731a7..3ead60748 100644--- a/apps/server/src/mcp/McpHttpServer.ts+++ b/apps/server/src/mcp/McpHttpServer.ts@@ -74,7 +74,15 @@ const makeMcpAuthMiddleware = McpSessionRegistry.McpSessionRegistry.pipe(? authorization.slice("Bearer ".length).trim(): "";const invocation = yield* registry.resolve(token);- if (!invocation) return unauthorized;+ if (!invocation) {+ // Without this the only symptom of a dead credential is the agent+ // quietly losing the whole `t3-code` toolkit for the rest of its+ // session, with nothing on the server to explain why.+ yield* Effect.logWarning("rejected MCP request with an unusable credential", {+ reason: token.length === 0 ? "missing_bearer_token" : "unknown_or_expired_token",+ });+ return unauthorized;+ }return yield* httpEffect.pipe(Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),Effect.map(normalizeMcpHttpResponse),diff --git a/apps/server/src/mcp/McpInvocationContext.test.ts b/apps/server/src/mcp/McpInvocationContext.test.tsindex 39c686890..569917325 100644--- a/apps/server/src/mcp/McpInvocationContext.test.ts+++ b/apps/server/src/mcp/McpInvocationContext.test.ts@@ -17,7 +17,6 @@ it.effect("reports the scoped credential context when preview capability is unavproviderInstanceId: ProviderInstanceId.make("codex"),capabilities: new Set(),issuedAt: 1,- expiresAt: 2,};return Effect.gen(function* () {diff --git a/apps/server/src/mcp/McpInvocationContext.ts b/apps/server/src/mcp/McpInvocationContext.tsindex b13bf2d31..49273485a 100644--- a/apps/server/src/mcp/McpInvocationContext.ts+++ b/apps/server/src/mcp/McpInvocationContext.ts@@ -16,7 +16,6 @@ export interface McpInvocationScope {readonly providerInstanceId: ProviderInstanceId;readonly capabilities: ReadonlySet<McpCapability>;readonly issuedAt: number;- readonly expiresAt: number;}export class McpInvocationContext extends Context.Service<diff --git a/apps/server/src/mcp/McpSessionRegistry.test.ts b/apps/server/src/mcp/McpSessionRegistry.test.tsindex a91d98feb..1d8aead99 100644--- a/apps/server/src/mcp/McpSessionRegistry.test.ts+++ b/apps/server/src/mcp/McpSessionRegistry.test.ts@@ -23,8 +23,7 @@ const makeRegistry = (now: () => number, httpServer = fakeHttpServer) =>McpSessionRegistry.__testing.make({now,- idleTimeoutMs: 100,- maximumLifetimeMs: 1_000,+ livenessWindowMs: 100,}).pipe(Effect.provideService(HttpServer.HttpServer, httpServer),@@ -75,7 +74,7 @@ it.effect("builds MCP endpoints from the bound server host", () =>}),);-it.effect("expires credentials after inactivity", () =>+it.effect("expires credentials once their session stops showing signs of life", () =>Effect.gen(function* () {let timestamp = 1_000;const registry = yield* makeRegistry(() => timestamp);@@ -88,3 +87,43 @@ it.effect("expires credentials after inactivity", () =>expect(yield* registry.resolve(token)).toBeUndefined();}),);++it.effect("keeps a credential alive across turns that never touch an MCP tool", () =>+ Effect.gen(function* () {+ let timestamp = 1_000;+ const registry = yield* makeRegistry(() => timestamp);+ const threadId = ThreadId.make("thread-3");+ const issued = yield* registry.issue({+ threadId,+ providerInstanceId: ProviderInstanceId.make("claude"),+ });+ const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");++ // Well past the liveness window in total, but each turn reports in before+ // it lapses — this is the long-session case that used to lose the toolkit.+ for (let turn = 0; turn < 10; turn += 1) {+ timestamp += 99;+ yield* registry.touch(threadId);+ }++ expect((yield* registry.resolve(token))?.threadId).toBe(threadId);+ }),+);++it.effect("does not keep credentials of other threads alive", () =>+ Effect.gen(function* () {+ let timestamp = 1_000;+ const registry = yield* makeRegistry(() => timestamp);+ const issued = yield* registry.issue({+ threadId: ThreadId.make("thread-4"),+ providerInstanceId: ProviderInstanceId.make("codex"),+ });+ const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");++ timestamp += 99;+ yield* registry.touch(ThreadId.make("thread-unrelated"));+ timestamp += 2;++ expect(yield* registry.resolve(token)).toBeUndefined();+ }),+);diff --git a/apps/server/src/mcp/McpSessionRegistry.ts b/apps/server/src/mcp/McpSessionRegistry.tsindex 67c4f2f0f..f19a4f4e8 100644--- a/apps/server/src/mcp/McpSessionRegistry.ts+++ b/apps/server/src/mcp/McpSessionRegistry.ts@@ -18,7 +18,6 @@ export interface McpCredentialRequest {export interface McpIssuedCredential {readonly config: McpProviderSession.McpProviderSessionConfig;- readonly expiresAt: number;}export interface McpSessionRegistryShape {@@ -26,6 +25,12 @@ export interface McpSessionRegistryShape {readonly resolve: (rawToken: string,) => Effect.Effect<McpInvocationContext.McpInvocationScope | undefined>;+ /**+ * Records a sign of life for every credential bound to `threadId`. Provider+ * turns call this so that a session which is plainly alive keeps its+ * credential even when it goes a long time without touching an MCP tool.+ */+ readonly touch: (threadId: ThreadId) => Effect.Effect<void>;readonly revokeProviderSession: (providerSessionId: string) => Effect.Effect<void>;readonly revokeThread: (threadId: ThreadId) => Effect.Effect<void>;readonly revokeAll: Effect.Effect<void>;@@ -39,7 +44,7 @@ export class McpSessionRegistry extends Context.Service<interface CredentialRecord {readonly tokenHash: string;readonly scope: McpInvocationContext.McpInvocationScope;- readonly lastUsedAt: number;+ readonly lastAliveAt: number;}interface RegistryState {@@ -47,13 +52,25 @@ interface RegistryState {}export interface McpSessionRegistryOptions {- readonly idleTimeoutMs?: number;- readonly maximumLifetimeMs?: number;+ readonly livenessWindowMs?: number;readonly now?: () => number;}-const DEFAULT_IDLE_TIMEOUT_MS = 30 * 60 * 1_000;-const DEFAULT_MAXIMUM_LIFETIME_MS = 8 * 60 * 60 * 1_000;+/**+ * 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("");@@ -80,8 +97,7 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (const httpServer = yield* HttpServer.HttpServer;const state = yield* SynchronizedRef.make<RegistryState>({ records: new Map() });const currentTimeMillis = options.now ? Effect.sync(options.now) : Clock.currentTimeMillis;- const idleTimeoutMs = options.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS;- const maximumLifetimeMs = options.maximumLifetimeMs ?? DEFAULT_MAXIMUM_LIFETIME_MS;+ const livenessWindowMs = options.livenessWindowMs ?? DEFAULT_LIVENESS_WINDOW_MS;const endpoint =httpServer.address._tag === "TcpAddress"? `http://${getHttpMcpEndpointHost(httpServer.address.hostname)}:${httpServer.address.port}/mcp`@@ -92,11 +108,10 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (.digest("SHA-256", new TextEncoder().encode(token)).pipe(Effect.map(bytesToHex), Effect.orDie);- const pruneExpired = (records: ReadonlyMap<string, CredentialRecord>, timestamp: number) => {+ const pruneDead = (records: ReadonlyMap<string, CredentialRecord>, timestamp: number) => {const next = new Map(Array.from(records).filter(- ([, record]) =>- timestamp <= record.scope.expiresAt && timestamp - record.lastUsedAt <= idleTimeoutMs,+ ([, record]) => timestamp - record.lastAliveAt <= livenessWindowMs,),);return next.size === records.size ? records : next;@@ -108,7 +123,6 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (const providerSessionId = yield* crypto.randomUUIDv4.pipe(Effect.orDie);const rawToken = yield* crypto.randomBytes(32).pipe(Effect.map(tokenFromBytes), Effect.orDie);const tokenHash = yield* hashToken(rawToken);- const expiresAt = issuedAt + maximumLifetimeMs;const scope: McpInvocationContext.McpInvocationScope = {environmentId,threadId: ThreadId.make(request.threadId),@@ -116,11 +130,10 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (providerInstanceId: ProviderInstanceId.make(request.providerInstanceId),capabilities: new Set(["preview"]),issuedAt,- expiresAt,};yield* SynchronizedRef.update(state, ({ records }) => {- const next = new Map(pruneExpired(records, issuedAt));- next.set(tokenHash, { tokenHash, scope, lastUsedAt: issuedAt });+ const next = new Map(pruneDead(records, issuedAt));+ next.set(tokenHash, { tokenHash, scope, lastAliveAt: issuedAt });return { records: next };});return {@@ -132,7 +145,6 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (endpoint,authorizationHeader: `Bearer ${rawToken}`,},- expiresAt,};},);@@ -143,16 +155,32 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (const tokenHash = yield* hashToken(rawToken);const timestamp = yield* currentTimeMillis;return yield* SynchronizedRef.modify(state, ({ records }) => {- const current = pruneExpired(records, timestamp);+ 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, lastUsedAt: timestamp });+ next.set(tokenHash, { ...record, lastAliveAt: timestamp });return [record.scope, { records: next }] as const;});},);+ const touch: McpSessionRegistryShape["touch"] = Effect.fn("McpSessionRegistry.touch")(+ function* (threadId) {+ const timestamp = yield* currentTimeMillis;+ yield* SynchronizedRef.update(state, ({ records }) => {+ const current = pruneDead(records, timestamp);+ const next = new Map(current);+ for (const [tokenHash, record] of current) {+ if (record.scope.threadId === threadId) {+ next.set(tokenHash, { ...record, lastAliveAt: timestamp });+ }+ }+ return { records: next };+ });+ },+ );+const revokeWhere = (predicate: (record: CredentialRecord) => boolean) =>SynchronizedRef.update(state, ({ records }) => ({records: new Map(Array.from(records).filter(([, record]) => !predicate(record))),@@ -161,6 +189,7 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (return McpSessionRegistry.of({issue,resolve,+ touch,revokeProviderSession: Effect.fn("McpSessionRegistry.revokeProviderSession")(function* (providerSessionId) {yield* revokeWhere((record) => record.scope.providerSessionId === providerSessionId);@@ -202,6 +231,13 @@ export const issueActiveMcpCredential = (.pipe(Effect.andThen(activeMcpSessionRegistry.issue(request))): Effect.sync((): McpIssuedCredential | undefined => undefined);+/**+ * Refreshes the liveness of a thread's MCP credential. Called on every provider+ * turn so an active session is never mistaken for an abandoned one.+ */+export const touchActiveMcpThread = (threadId: ThreadId): Effect.Effect<void> =>+ activeMcpSessionRegistry ? activeMcpSessionRegistry.touch(threadId) : Effect.void;+export const revokeActiveMcpThread = (threadId: ThreadId): Effect.Effect<void> =>activeMcpSessionRegistry ? activeMcpSessionRegistry.revokeThread(threadId) : Effect.void;diff --git a/apps/server/src/mcp/PreviewAutomationBroker.test.ts b/apps/server/src/mcp/PreviewAutomationBroker.test.tsindex 5bd665e51..3bc0fd713 100644--- a/apps/server/src/mcp/PreviewAutomationBroker.test.ts+++ b/apps/server/src/mcp/PreviewAutomationBroker.test.ts@@ -31,7 +31,6 @@ const scope = {providerInstanceId: ProviderInstanceId.make("codex"),capabilities: new Set(["preview"] as const),issuedAt: 1,- expiresAt: Number.MAX_SAFE_INTEGER,};const makeHost = (overrides: Partial<PreviewAutomationHost> = {}): PreviewAutomationHost => ({diff --git a/apps/server/src/mcp/PreviewAutomationBroker.ts b/apps/server/src/mcp/PreviewAutomationBroker.tsindex 5a9528030..3e9bfaac2 100644--- a/apps/server/src/mcp/PreviewAutomationBroker.ts+++ b/apps/server/src/mcp/PreviewAutomationBroker.ts@@ -22,7 +22,6 @@ import {type PreviewAutomationStreamEvent,} from "@t3tools/contracts";import * as Context from "effect/Context";-import * as Clock from "effect/Clock";import * as Crypto from "effect/Crypto";import * as Deferred from "effect/Deferred";import * as Effect from "effect/Effect";@@ -75,11 +74,18 @@ interface PendingRequest {readonly context: PreviewAutomationRequestErrorContext;}+/**+ * A lease pinning one provider session to one desktop runtime. It lives exactly+ * as long as the connection it names: `connectionId`/`queue` identity is what+ * makes a lease valid, so a disconnected or replaced host is dropped on the next+ * lookup. The lease deliberately has no clock of its own — it used to inherit+ * the MCP credential's expiry, which coupled host stickiness to an unrelated+ * auth deadline and could migrate a live session to another runtime mid-flow.+ */interface HostAssignment {readonly clientId: ClientConnection["clientId"];readonly connectionId: ClientConnection["connectionId"];readonly queue: ClientConnection["queue"];- readonly expiresAt: number;readonly tabId?: PreviewTabId;readonly tabSequence?: number;}@@ -422,13 +428,11 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {): Effect.fn.Return<A, PreviewAutomationError> {const timeoutMs = input.timeoutMs ?? 15_000;const deferred = yield* Deferred.make<unknown, PreviewAutomationError>();- const now = yield* Clock.currentTimeMillis;const route = yield* SynchronizedRef.modify(state, (current) => {const assignments = new Map(Array.from(current.assignments).filter(([, assignment]) => {const connection = current.clients.get(assignment.clientId);return (- assignment.expiresAt > now &&connection?.connectionId === assignment.connectionId &&connection.queue === assignment.queue);@@ -473,7 +477,6 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {clientId: connection.clientId,connectionId: connection.connectionId,queue: connection.queue,- expiresAt: input.scope.expiresAt,...(canReuseAssignedTab && assigned.tabId !== undefined ? { tabId: assigned.tabId } : {}),...(canReuseAssignedTab && assigned.tabSequence !== undefined? { tabSequence: assigned.tabSequence }diff --git a/apps/server/src/provider/Layers/ProviderService.ts b/apps/server/src/provider/Layers/ProviderService.tsindex 2eaaeb8ce..ecf26a914 100644--- a/apps/server/src/provider/Layers/ProviderService.ts+++ b/apps/server/src/provider/Layers/ProviderService.ts@@ -679,6 +679,12 @@ const makeProviderService = Effect.fn("makeProviderService")(function* ("provider.kind": routed.adapter.provider,...(input.modelSelection?.model ? { "provider.model": input.modelSelection.model } : {}),});+ // A turn is the clearest sign a session is still alive. The MCP+ // credential is minted once at session start and cannot be rotated into+ // an already-spawned agent process, so we keep the existing token valid+ // rather than issuing a new one: sessions that go a long time between+ // browser tool calls used to lose the toolkit outright.+ yield* McpSessionRegistry.touchActiveMcpThread(input.threadId);const turn = yield* routed.adapter.sendTurn(input);yield* directory.upsert({threadId: input.threadId,