Skip to content

Commit 854d8fc

Browse files
fix(server): delegated MCP tokens no longer renew themselves
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9bc4138 commit 854d8fc

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

‎apps/server/src/mcp/McpSessionRegistry.test.ts‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { expect, it } from "@effect/vitest";
33
import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
44
import * as Effect from "effect/Effect";
5+
import * as TestClock from "effect/testing/TestClock";
56
import { HttpServer } from "effect/http";
67
import * as NetAddress from "effect/net/NetAddress";
78

@@ -20,10 +21,10 @@ const fakeEnvironment = ServerEnvironment.ServerEnvironment.of({
2021
getDescriptor: Effect.die("unused"),
2122
});
2223

23-
const makeRegistry = (now: () => number, httpServer = fakeHttpServer) =>
24+
const makeRegistry = (now?: () => number, httpServer = fakeHttpServer) =>
2425
McpSessionRegistry.__testing
2526
.make({
26-
now,
27+
...(now === undefined ? {} : { now }),
2728
livenessWindowMs: 100,
2829
})
2930
.pipe(
@@ -140,6 +141,32 @@ it.effect("expires credentials once their session stops showing signs of life",
140141
}),
141142
);
142143

144+
it.effect("MCP requests do not renew a token; only a trusted touch does", () =>
145+
Effect.gen(function* () {
146+
const registry = yield* makeRegistry();
147+
const threadId = ThreadId.make("thread-polling");
148+
const issued = yield* registry.issue({
149+
threadId,
150+
providerInstanceId: ProviderInstanceId.make("codex"),
151+
capabilities: new Set(["preview"]),
152+
});
153+
const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
154+
155+
for (let step = 0; step < 3; step++) {
156+
yield* TestClock.adjust(50);
157+
yield* registry.touch(threadId);
158+
expect(yield* registry.resolve(token)).toBeDefined();
159+
}
160+
161+
for (let step = 0; step < 2; step++) {
162+
yield* TestClock.adjust(50);
163+
expect(yield* registry.resolve(token)).toBeDefined();
164+
}
165+
yield* TestClock.adjust(1);
166+
expect(yield* registry.resolve(token)).toBeUndefined();
167+
}),
168+
);
169+
143170
it.effect("does not keep credentials of other threads alive", () =>
144171
Effect.gen(function* () {
145172
let timestamp = 1_000;

‎apps/server/src/mcp/McpSessionRegistry.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,9 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
142142
if (rawToken.length === 0) return undefined;
143143
const tokenHash = yield* hashToken(rawToken);
144144
const timestamp = yield* currentTimeMillis;
145-
return yield* SynchronizedRef.modify(state, ({ records }) => {
146-
const record = records.get(tokenHash);
147-
if (!record || timestamp - record.lastAliveAt > livenessWindowMs) {
148-
return [undefined, { records }] as const;
149-
}
150-
const next = new Map(records);
151-
next.set(tokenHash, { ...record, lastAliveAt: timestamp });
152-
return [record.scope, { records: next }] as const;
153-
});
145+
const record = (yield* SynchronizedRef.get(state)).records.get(tokenHash);
146+
if (!record || timestamp - record.lastAliveAt > livenessWindowMs) return undefined;
147+
return record.scope;
154148
},
155149
);
156150

0 commit comments

Comments
 (0)