Remove MCP credential expiration

This commit is contained in:
Julius Marminge authored and Julius Marminge committed 2026-06-24 16:12:46 -07:00
1 parent 9f19b4f06c
commit 6a295b93ea
8 files changed
+14 -46

No files matched your search

@@ -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
@@ -21,7 +21,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,
@@ -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<
@@ -21,11 +21,7 @@ const fakeEnvironment = ServerEnvironment.ServerEnvironment.of({
const makeRegistry = (now: () => number, httpServer = fakeHttpServer) =>
McpSessionRegistry.__testing
.make({
now,
idleTimeoutMs: 100,
maximumLifetimeMs: 1_000,
})
.make({ now })
.pipe(
Effect.provideService(HttpServer.HttpServer, httpServer),
Effect.provideService(ServerEnvironment.ServerEnvironment, fakeEnvironment),
@@ -76,7 +72,7 @@ it.effect("builds MCP endpoints from the bound server host", () =>
}),
);
it.effect("expires credentials after inactivity", () =>
it.effect("keeps credentials valid until they are explicitly revoked", () =>
Effect.gen(function* () {
let timestamp = 1_000;
const registry = yield* makeRegistry(() => timestamp);
@@ -85,7 +81,12 @@ it.effect("expires credentials after inactivity", () =>
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();
}),
);
@@ -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,
+5 -34
View File
@@ -18,10 +18,10 @@ export interface McpCredentialRequest {
export interface McpIssuedCredential {
readonly config: McpProviderSession.McpProviderSessionConfig;
readonly expiresAt: number;
}
export interface McpSessionRegistryShape {
/** Credentials have no time-based expiry and must be explicitly revoked by their owner. */
readonly issue: (request: McpCredentialRequest) => Effect.Effect<McpIssuedCredential>;
readonly resolve: (
rawToken: string,
@@ -37,9 +37,7 @@ export class McpSessionRegistry extends Context.Service<
>()("t3/mcp/McpSessionRegistry") {}
interface CredentialRecord {
readonly tokenHash: string;
readonly scope: McpInvocationContext.McpInvocationScope;
readonly lastUsedAt: number;
}
interface RegistryState {
@@ -47,14 +45,9 @@ interface RegistryState {
}
export interface McpSessionRegistryOptions {
readonly idleTimeoutMs?: number;
readonly maximumLifetimeMs?: number;
readonly now?: () => number;
}
const DEFAULT_IDLE_TIMEOUT_MS = 30 * 60 * 1_000;
const DEFAULT_MAXIMUM_LIFETIME_MS = 8 * 60 * 60 * 1_000;
const bytesToHex = (bytes: Uint8Array): string =>
Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
@@ -80,8 +73,6 @@ 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 endpoint =
httpServer.address._tag === "TcpAddress"
? `http://${getHttpMcpEndpointHost(httpServer.address.hostname)}:${httpServer.address.port}/mcp`
@@ -92,23 +83,12 @@ 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 next = new Map(
Array.from(records).filter(
([, record]) =>
timestamp <= record.scope.expiresAt && timestamp - record.lastUsedAt <= idleTimeoutMs,
),
);
return next.size === records.size ? records : next;
};
const issue: McpSessionRegistryShape["issue"] = Effect.fn("McpSessionRegistry.issue")(
function* (request) {
const issuedAt = yield* currentTimeMillis;
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 +96,10 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
providerInstanceId: ProviderInstanceId.make(request.providerInstanceId),
capabilities: new Set(["preview", "orchestration"]),
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(records);
next.set(tokenHash, { scope });
return { records: next };
});
return {
@@ -132,7 +111,6 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
endpoint,
authorizationHeader: `Bearer ${rawToken}`,
},
expiresAt,
};
},
);
@@ -141,15 +119,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 = pruneExpired(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 });
return [record.scope, { records: next }] as const;
});
const record = (yield* SynchronizedRef.get(state)).records.get(tokenHash);
return record?.scope;
},
);
@@ -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<string, unknown>) =>
server
@@ -22,7 +22,6 @@ const scope = {
providerInstanceId: ProviderInstanceId.make("codex"),
capabilities: new Set(["preview"] as const),
issuedAt: 1,
expiresAt: 2,
};
const makeOwner = (overrides: Partial<PreviewAutomationOwner> = {}): PreviewAutomationOwner => ({