Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import { HttpServer } from "effect/unstable/http";
6
7import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
8import * as McpSessionRegistry from "./McpSessionRegistry.ts";
9
10const environmentId = EnvironmentId.make("environment-1");
11const makeFakeHttpServer = (hostname: string, port = 43123) =>
12 HttpServer.HttpServer.of({
13 address: { _tag: "TcpAddress", hostname, port },
14 serve: (() => Effect.void) as HttpServer.HttpServer["Service"]["serve"],
15 });
16const fakeHttpServer = makeFakeHttpServer("127.0.0.1");
17const fakeEnvironment = ServerEnvironment.ServerEnvironment.of({
18 getEnvironmentId: Effect.succeed(environmentId),
19 getDescriptor: Effect.die("unused"),
20});
21
22const makeRegistry = (now: () => number, httpServer = fakeHttpServer) =>
23 McpSessionRegistry.__testing
24 .make({
25 now,
26 livenessWindowMs: 100,
27 })
28 .pipe(
29 Effect.provideService(HttpServer.HttpServer, httpServer),
30 Effect.provideService(ServerEnvironment.ServerEnvironment, fakeEnvironment),
31 Effect.provide(NodeServices.layer),
32 );
33
34it.effect("stores only a token hash, resolves the bearer token, and revokes by thread", () =>
35 Effect.gen(function* () {
36 let timestamp = 1_000;
37 const registry = yield* makeRegistry(() => timestamp);
38 const threadId = ThreadId.make("thread-1");
39 const issued = yield* registry.issue({
40 threadId,
41 providerInstanceId: ProviderInstanceId.make("codex"),
42 });
43 expect(issued.config.endpoint).toBe("http://127.0.0.1:43123/mcp");
44 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
45 expect(token.length).toBeGreaterThan(20);
46
47 const resolved = yield* registry.resolve(token);
48 expect(resolved?.threadId).toBe(threadId);
49
50 yield* registry.revokeThread(threadId);
51 expect(yield* registry.resolve(token)).toBeUndefined();
52
53 timestamp += 2_000;
54 }),
55);
56
57it.effect("builds MCP endpoints from the bound server host", () =>
58 Effect.gen(function* () {
59 const cases = [
60 ["100.64.0.40", "http://100.64.0.40:43123/mcp"],
61 ["0.0.0.0", "http://127.0.0.1:43123/mcp"],
62 ["localhost", "http://localhost:43123/mcp"],
63 ["127.0.0.1", "http://127.0.0.1:43123/mcp"],
64 ] as const;
65
66 for (const [hostname, expectedEndpoint] of cases) {
67 const registry = yield* makeRegistry(() => 1_000, makeFakeHttpServer(hostname));
68 const issued = yield* registry.issue({
69 threadId: ThreadId.make(`thread-${hostname}`),
70 providerInstanceId: ProviderInstanceId.make("codex"),
71 });
72 expect(issued.config.endpoint).toBe(expectedEndpoint);
73 }
74 }),
75);
76
77it.effect("expires credentials once their session stops showing signs of life", () =>
78 Effect.gen(function* () {
79 let timestamp = 1_000;
80 const registry = yield* makeRegistry(() => timestamp);
81 const issued = yield* registry.issue({
82 threadId: ThreadId.make("thread-2"),
83 providerInstanceId: ProviderInstanceId.make("claude"),
84 });
85 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
86 timestamp += 101;
87 expect(yield* registry.resolve(token)).toBeUndefined();
88 }),
89);
90
91it.effect("keeps a credential alive across turns that never touch an MCP tool", () =>
92 Effect.gen(function* () {
93 let timestamp = 1_000;
94 const registry = yield* makeRegistry(() => timestamp);
95 const threadId = ThreadId.make("thread-3");
96 const issued = yield* registry.issue({
97 threadId,
98 providerInstanceId: ProviderInstanceId.make("claude"),
99 });
100 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
101
102 // Well past the liveness window in total, but each turn reports in before
103 // it lapses — this is the long-session case that used to lose the toolkit.
104 for (let turn = 0; turn < 10; turn += 1) {
105 timestamp += 99;
106 yield* registry.touch(threadId);
107 }
108
109 expect((yield* registry.resolve(token))?.threadId).toBe(threadId);
110 }),
111);
112
113it.effect("does not keep credentials of other threads alive", () =>
114 Effect.gen(function* () {
115 let timestamp = 1_000;
116 const registry = yield* makeRegistry(() => timestamp);
117 const issued = yield* registry.issue({
118 threadId: ThreadId.make("thread-4"),
119 providerInstanceId: ProviderInstanceId.make("codex"),
120 });
121 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
122
123 timestamp += 99;
124 yield* registry.touch(ThreadId.make("thread-unrelated"));
125 timestamp += 2;
126
127 expect(yield* registry.resolve(token)).toBeUndefined();
128 }),
129);