Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions apps/server/src/mcp/McpSessionRegistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,56 @@ it.effect.each(["provider-session", "thread", "all"] as const)(
expect(yield* registry.resolve(token)).toBeUndefined();
}),
);

it.effect(
"derives a durable per-thread requestNamespace across restarts and credential rotations",
() =>
Effect.gen(function* () {
const threadId = ThreadId.make("thread-durable");
const otherThreadId = ThreadId.make("thread-other");
const firstRegistry = yield* makeRegistry(() => 1_000);
const firstIssued = yield* firstRegistry.issue({
threadId,
providerInstanceId: ProviderInstanceId.make("codex"),
});
const firstToken = firstIssued.config.authorizationHeader.replace(/^Bearer\s+/, "");
const firstScope = yield* firstRegistry.resolve(firstToken);

// Re-issuing on the same registry rotates token and providerSessionId, keeping requestNamespace.
const rotatedIssued = yield* firstRegistry.issue({
threadId,
providerInstanceId: ProviderInstanceId.make("codex"),
});
const rotatedScope = yield* firstRegistry.resolve(
rotatedIssued.config.authorizationHeader.replace(/^Bearer\s+/, ""),
);

// A brand-new registry instance (simulating a server restart) preserves the same namespace.
const restartedRegistry = yield* makeRegistry(() => 2_000);
const restartedIssued = yield* restartedRegistry.issue({
threadId,
providerInstanceId: ProviderInstanceId.make("codex"),
});
const restartedScope = yield* restartedRegistry.resolve(
restartedIssued.config.authorizationHeader.replace(/^Bearer\s+/, ""),
);
const otherIssued = yield* restartedRegistry.issue({
threadId: otherThreadId,
providerInstanceId: ProviderInstanceId.make("codex"),
});
const otherScope = yield* restartedRegistry.resolve(
otherIssued.config.authorizationHeader.replace(/^Bearer\s+/, ""),
);

const expectedNamespace = `thread:${threadId}`;
expect(firstScope?.requestNamespace).toBe(expectedNamespace);
expect(rotatedScope?.requestNamespace).toBe(expectedNamespace);
expect(restartedScope?.requestNamespace).toBe(expectedNamespace);
expect(otherScope?.requestNamespace).toBe(`thread:${otherThreadId}`);
expect(otherScope?.requestNamespace).not.toBe(expectedNamespace);
expect(rotatedScope?.thread.providerSessionId).not.toBe(firstScope?.thread.providerSessionId);
expect(restartedScope?.thread.providerSessionId).not.toBe(
firstScope?.thread.providerSessionId,
);
}),
);
13 changes: 11 additions & 2 deletions apps/server/src/mcp/McpSessionRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ const getHttpMcpEndpointHost = (address: NetAddress.IpAddress): string =>
? "127.0.0.1"
: NetAddress.formatUrlHostString(NetAddress.formatIp(address));

/**
* Durable per-thread idempotency namespace for MCP tool calls (`clientRequestId`).
* Derived from the thread's durable identity rather than the ephemeral `providerSessionId`
* so retries after an origin server restart or credential rotation reconcile with prior
* local and remote operations instead of starting duplicates or stranding remote lanes.
*/
const threadRequestNamespace = (threadId: ThreadId): string => `thread:${threadId}`;

const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
options: McpSessionRegistryOptions = {},
) {
Expand Down Expand Up @@ -125,11 +133,12 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
const rawToken = yield* random.randomBytes(32).pipe(Effect.map(tokenFromBytes), Effect.orDie);
const tokenHash = yield* hashToken(rawToken);
const browserToolsAvailable = request.browserToolsAvailable ?? true;
const threadId = ThreadId.make(request.threadId);
const scope: McpInvocationContext.McpThreadInvocationScope = {
environmentId,
requestNamespace: providerSessionId,
requestNamespace: threadRequestNamespace(threadId),
thread: {
threadId: ThreadId.make(request.threadId),
threadId,
providerSessionId,
providerInstanceId: ProviderInstanceId.make(request.providerInstanceId),
subagent: request.subagent === true,
Expand Down
Loading
Loading