Skip to content

Commit 2d41178

Browse files
Alb11747codexclaude
committed
fix(server): switching providers no longer revokes the other provider's T3 tool access
Each thread had a single MCP credential slot. Preparing a session for one provider revoked every credential on the thread and replaced the slot, so a provider session still attached from before the switch kept calling T3 tools with a revoked token (401 "Authentication required"), or read a token scoped to the other provider (parent_not_active). Credentials are now kept per thread and provider instance. Rotation revokes only the credential being replaced, and session release only defers revocation to other live sessions of the same provider instance. Fixes #16565 Co-Authored-By: GPT-6.1 Sol at high effort <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> Co-Authored-By: GPT-6.1 Sol at xhigh effort, review subagent for conflict rebase <[email protected]>
1 parent 8da749b commit 2d41178

11 files changed

Lines changed: 218 additions & 78 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8327,7 +8327,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
83278327
) {
83288328
const queryPolicy = claudeRuntimeQueryPolicyForRuntimePolicy(turnInput.runtimePolicy);
83298329
const mcpOverrides = claudeMcpQueryOverrides({
8330-
mcpSession: yield* mcpSessions.read(turnInput.threadId),
8330+
mcpSession: yield* mcpSessions.read(turnInput.threadId, adapterOptions.instanceId),
83318331
readOnlySandbox:
83328332
sandboxPolicyKindForClaudeRuntimePolicy(turnInput.runtimePolicy) === "readOnly",
83338333
...(queryPolicy.allowedTools === undefined

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
16791679
) {
16801680
const mcpSessions = yield* McpProviderSessions.McpProviderSessions;
16811681
const readMcpSession = (threadId: ThreadId | null) =>
1682-
threadId === null ? Effect.succeed(undefined) : mcpSessions.read(threadId);
1682+
threadId === null
1683+
? Effect.succeed(undefined)
1684+
: mcpSessions.read(threadId, adapterOptions.instanceId);
16831685
const { clientFactory, crypto, fileSystem, idAllocator, serverConfig } = adapterOptions;
16841686
const continuationRequests = adapterOptions.continuationRequests;
16851687

@@ -6184,7 +6186,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
61846186
) =>
61856187
Effect.gen(function* () {
61866188
const threadId = yield* getNativeThreadId(turnInput.providerThread);
6187-
const mcpSession = yield* mcpSessions.read(turnInput.threadId);
6189+
const mcpSession = yield* mcpSessions.read(
6190+
turnInput.threadId,
6191+
adapterOptions.instanceId,
6192+
);
61886193
const turnStartParams = yield* buildCodexTurnStartParams({
61896194
nativeThreadId: threadId,
61906195
codexInput,
@@ -6455,7 +6460,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
64556460
}),
64566461
ensureThread: (threadInput) =>
64576462
ensureInitialized.pipe(
6458-
Effect.andThen(mcpSessions.read(threadInput.threadId)),
6463+
Effect.andThen(mcpSessions.read(threadInput.threadId, adapterOptions.instanceId)),
64596464
Effect.flatMap((mcpSession) =>
64606465
client.request(
64616466
"thread/start",
@@ -7261,7 +7266,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
72617266
? {}
72627267
: { lastTurnId: boundary.lastTurnId }),
72637268
...codexThreadRuntimeParams({
7264-
mcpSession: yield* mcpSessions.read(threadInput.targetThreadId),
7269+
mcpSession: yield* mcpSessions.read(
7270+
threadInput.targetThreadId,
7271+
adapterOptions.instanceId,
7272+
),
72657273
...(threadInput.modelSelection === undefined
72667274
? {}
72677275
: { modelSelection: threadInput.modelSelection }),

‎apps/server/src/orchestration-v2/ProviderSessionManager.test.ts‎

Lines changed: 143 additions & 44 deletions
Large diffs are not rendered by default.

‎apps/server/src/orchestration-v2/ProviderSessionManager.ts‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ export const layerWithOptions = (
456456
>(["orchestration", "worktree", "pull-requests"]);
457457
if (browserToolsAvailable) capabilities.add("preview");
458458
if (deviceToolsAvailable) capabilities.add("device");
459-
const existing = yield* mcpSessions.read(threadId);
459+
const existing = yield* mcpSessions.read(threadId, providerInstanceId);
460460
if (existing !== undefined) {
461461
// Reserve before the async resolve so a release cannot
462462
// revoke the credential between validation and reservation.
@@ -483,8 +483,11 @@ export const layerWithOptions = (
483483
return { mcpCredentialId: existing.providerSessionId, issued: false };
484484
}
485485
dropMcpCredentialReservation(threadId, existing.providerSessionId);
486+
// Only this provider's credential rotates. Another provider's
487+
// session may still be attached to the thread with its MCP
488+
// client built around its own credential (#16565, #18852).
489+
yield* mcpSessionRegistry.revokeProviderSession(existing.providerSessionId);
486490
}
487-
yield* mcpSessionRegistry.revokeThread(threadId);
488491
const credential = yield* mcpSessionRegistry.issue({
489492
threadId,
490493
providerInstanceId,
@@ -501,15 +504,9 @@ export const layerWithOptions = (
501504
? mcpSessionRegistry
502505
.revokeThread(threadId)
503506
.pipe(Effect.tap(() => mcpSessions.clear(threadId)))
504-
: mcpSessionRegistry.revokeProviderSession(mcpCredentialId).pipe(
505-
Effect.tap(() =>
506-
Effect.gen(function* () {
507-
if ((yield* mcpSessions.read(threadId))?.providerSessionId === mcpCredentialId) {
508-
yield* mcpSessions.clear(threadId);
509-
}
510-
}),
511-
),
512-
);
507+
: mcpSessionRegistry
508+
.revokeProviderSession(mcpCredentialId)
509+
.pipe(Effect.tap(() => mcpSessions.clear(threadId, mcpCredentialId)));
513510

514511
const publishToSubscribers = (
515512
subscribers: Ref.Ref<
@@ -1072,7 +1069,10 @@ export const layerWithOptions = (
10721069
Array.from(current.values()).some(
10731070
(other) =>
10741071
other !== entry &&
1075-
(other.attachedThreadIds.has(threadId) ||
1072+
// Credentials are per provider instance, so only
1073+
// a session of the same instance can reuse it.
1074+
((other.runtime.instanceId === entry.runtime.instanceId &&
1075+
other.attachedThreadIds.has(threadId)) ||
10761076
other.mcpCredentialIdByThread.get(threadId) === mcpCredentialId),
10771077
);
10781078
return heldElsewhere
@@ -1412,7 +1412,8 @@ export const layerWithOptions = (
14121412
Array.from(current.values()).some(
14131413
(other) =>
14141414
other.runtime !== attachedRuntime &&
1415-
(other.attachedThreadIds.has(input.threadId) ||
1415+
((other.runtime.instanceId === input.providerInstanceId &&
1416+
other.attachedThreadIds.has(input.threadId)) ||
14161417
(mcpCredentialId !== undefined &&
14171418
other.mcpCredentialIdByThread.get(input.threadId) ===
14181419
mcpCredentialId)),

‎packages/provider-acp/src/server/adapter.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
12861286
const readMcpContext = (threadId: ThreadId | null) =>
12871287
threadId === null
12881288
? Effect.succeed(acpMcpContext(undefined, self))
1289-
: mcpSessions.read(threadId).pipe(Effect.map((session) => acpMcpContext(session, self)));
1289+
: mcpSessions
1290+
.read(threadId, options.instanceId)
1291+
.pipe(Effect.map((session) => acpMcpContext(session, self)));
12901292
const driver = flavor.driver;
12911293
const continuationRequests = options.continuationRequests;
12921294
const postSettleContinuationEnabled =

‎packages/provider-core/src/server/McpProviderSessions.ts‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ThreadId } from "@t3tools/contracts";
1+
import type { ProviderInstanceId, ThreadId } from "@t3tools/contracts";
22
import * as Context from "effect/Context";
33
import * as Effect from "effect/Effect";
44
import * as Layer from "effect/Layer";
@@ -10,21 +10,49 @@ export class McpProviderSessions extends Context.Service<
1010
McpProviderSessions,
1111
{
1212
readonly set: (config: McpProviderSessionConfig) => Effect.Effect<void>;
13-
readonly read: (threadId: ThreadId) => Effect.Effect<McpProviderSessionConfig | undefined>;
14-
readonly clear: (threadId: ThreadId) => Effect.Effect<void>;
13+
readonly read: (
14+
threadId: ThreadId,
15+
providerInstanceId: ProviderInstanceId,
16+
) => Effect.Effect<McpProviderSessionConfig | undefined>;
17+
/**
18+
* Clears the thread's credentials for every provider instance, or only the
19+
* one whose credential id is `providerSessionId`.
20+
*/
21+
readonly clear: (threadId: ThreadId, providerSessionId?: string) => Effect.Effect<void>;
1522
}
1623
>()("@t3tools/provider-core/server/McpProviderSessions") {}
1724

1825
const make = Effect.gen(function* () {
19-
const sessions = yield* Ref.make(new Map<ThreadId, McpProviderSessionConfig>());
26+
const sessions = yield* Ref.make(
27+
new Map<ThreadId, ReadonlyMap<ProviderInstanceId, McpProviderSessionConfig>>(),
28+
);
2029
return McpProviderSessions.of({
2130
set: (config) =>
22-
Ref.update(sessions, (current) => new Map(current).set(config.threadId, config)),
23-
read: (threadId) => Ref.get(sessions).pipe(Effect.map((current) => current.get(threadId))),
24-
clear: (threadId) =>
31+
Ref.update(sessions, (current) =>
32+
new Map(current).set(
33+
config.threadId,
34+
new Map(current.get(config.threadId)).set(config.providerInstanceId, config),
35+
),
36+
),
37+
read: (threadId, providerInstanceId) =>
38+
Ref.get(sessions).pipe(
39+
Effect.map((current) => current.get(threadId)?.get(providerInstanceId)),
40+
),
41+
clear: (threadId, providerSessionId) =>
2542
Ref.update(sessions, (current) => {
2643
const next = new Map(current);
27-
next.delete(threadId);
44+
const byInstance = current.get(threadId);
45+
if (providerSessionId === undefined || byInstance === undefined) {
46+
next.delete(threadId);
47+
return next;
48+
}
49+
const remaining = new Map(
50+
Array.from(byInstance).filter(
51+
([, config]) => config.providerSessionId !== providerSessionId,
52+
),
53+
);
54+
if (remaining.size === 0) next.delete(threadId);
55+
else next.set(threadId, remaining);
2856
return next;
2957
}),
3058
});

‎packages/provider-cursor/src/server/adapter.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,7 +2133,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
21332133
modelSelection: openInput.modelSelection,
21342134
runtimePolicy: openInput.runtimePolicy,
21352135
threadId: openInput.threadId,
2136-
mcpSession: yield* mcpSessions.read(openInput.threadId),
2136+
mcpSession: yield* mcpSessions.read(openInput.threadId, adapterOptions.instanceId),
21372137
}),
21382138
threadId: openInput.threadId,
21392139
providerSessionId: input.providerSessionId,
@@ -2242,7 +2242,9 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
22422242
modelSelection: turnInput.modelSelection,
22432243
runtimePolicy: turnInput.runtimePolicy,
22442244
});
2245-
const mcpServers = cursorMcpServers(yield* mcpSessions.read(turnInput.threadId));
2245+
const mcpServers = cursorMcpServers(
2246+
yield* mcpSessions.read(turnInput.threadId, adapterOptions.instanceId),
2247+
);
22462248
const message = yield* resolveUserMessage(turnInput, mcpServers);
22472249
const pendingUpdates: Array<InteractionUpdate> = [];
22482250
let context: ActiveCursorTurn | null = null;

‎packages/provider-muse/src/server/adapter.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
16051605
);
16061606
const launchHost = Effect.fnUntraced(function* () {
16071607
const epoch = ++hostEpoch;
1608-
const mcpSession = yield* mcpSessions.read(input.threadId);
1608+
const mcpSession = yield* mcpSessions.read(input.threadId, options.instanceId);
16091609
const environment = McpProviderSession.withAgentDeviceEnvironment(
16101610
options.environment,
16111611
mcpSession,
@@ -1726,7 +1726,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
17261726
let missingNativeSession = false;
17271727
return yield* Effect.gen(function* () {
17281728
nativeSessionId = requestedId ?? host.connection.mintCommandId();
1729-
const mcpSession = yield* mcpSessions.read(args.threadId);
1729+
const mcpSession = yield* mcpSessions.read(args.threadId, options.instanceId);
17301730
if (mcpSession && !host.initializeResult.grantedCapabilities.includes("sessionMcp"))
17311731
return yield* protocolError(
17321732
"Update Muse Code to a version that supports session MCP servers",

‎packages/provider-opencode/src/server/adapter.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
957957
: {}),
958958
});
959959

960-
const mcpSession = yield* mcpSessions.read(input.threadId);
960+
const mcpSession = yield* mcpSessions.read(input.threadId, options.instanceId);
961961
const hasT3Mcp = mcpSession !== undefined && !connection.external;
962962
const orchestrationSystemPrompt = t3OrchestrationSystemPrompt(hasT3Mcp);
963963
if (hasT3Mcp) {

‎packages/provider-opencode/src/server/v2/adapter.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2935,7 +2935,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
29352935
state: ThreadState,
29362936
turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
29372937
) {
2938-
const mcpSession = yield* mcpSessions.read(turnInput.threadId);
2938+
const mcpSession = yield* mcpSessions.read(turnInput.threadId, instanceId);
29392939
const directory = turnInput.runtimePolicy.cwd ?? host.paths.cwd;
29402940
const name = yield* mcpServerNameFor(turnInput.threadId);
29412941
// An external server may not reach T3's MCP endpoint, as with 1.x.

0 commit comments

Comments
 (0)