Skip to content

Commit 4c36119

Browse files
t3dotggclaude
andauthored
fix(server): Claude threads can allow an MCP app for the session (#33707)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 934c7f8 commit 4c36119

2 files changed

Lines changed: 52 additions & 13 deletions

File tree

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

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { beforeEach, vi } from "vite-plus/test";
99

1010
import type {
1111
ElicitationRequest,
12+
ElicitationResult,
1213
Query as ClaudeQuery,
1314
SDKMessage,
1415
SDKResultMessage,
@@ -5703,6 +5704,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
57035704
options: [
57045705
{ decision: "cancel", label: "Cancel" },
57055706
{ decision: "decline", label: "Decline" },
5707+
{ decision: "acceptForSession", label: "Always allow this session" },
57065708
{ decision: "accept", label: "Approve" },
57075709
],
57085710
});
@@ -5732,16 +5734,41 @@ describe("ClaudeAdapterV2 background wake turns", () => {
57325734
}).pipe(Effect.scoped, Effect.provide(testLayer)),
57335735
);
57345736

5735-
it.effect.each(["acceptForSession", "acceptAlways"] as const)(
5736-
"declines an unsupported %s decision",
5737-
(decision) =>
5738-
Effect.gen(function* () {
5739-
const { harness, elicit, awaitCard } = yield* startElicitationTurn();
5740-
const answer = yield* elicit(approvalOnly);
5741-
const request = yield* awaitCard(1);
5742-
yield* harness.runtime.respondToRuntimeRequest({ requestId: request.id, decision });
5743-
assert.deepEqual(yield* Fiber.join(answer), { action: "decline" });
5744-
}).pipe(Effect.scoped, Effect.provide(testLayer)),
5737+
it.effect("declines an unsupported acceptAlways decision", () =>
5738+
Effect.gen(function* () {
5739+
const { harness, elicit, awaitCard } = yield* startElicitationTurn();
5740+
const answer = yield* elicit(approvalOnly);
5741+
const request = yield* awaitCard(1);
5742+
yield* harness.runtime.respondToRuntimeRequest({
5743+
requestId: request.id,
5744+
decision: "acceptAlways",
5745+
});
5746+
assert.deepEqual(yield* Fiber.join(answer), { action: "decline" });
5747+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
5748+
);
5749+
5750+
it.effect("answers a repeated prompt from a session grant without a card", () =>
5751+
Effect.gen(function* () {
5752+
const { harness, elicit, awaitCard, runtimeRequests } = yield* startElicitationTurn();
5753+
const first = yield* elicit(approvalOnly);
5754+
const request = yield* awaitCard(1);
5755+
yield* harness.runtime.respondToRuntimeRequest({
5756+
requestId: request.id,
5757+
decision: "acceptForSession",
5758+
});
5759+
const granted = { action: "accept", content: {} } satisfies ElicitationResult;
5760+
assert.deepEqual(yield* Fiber.join(first), granted);
5761+
5762+
assert.deepEqual(yield* Fiber.join(yield* elicit(approvalOnly)), granted);
5763+
assert.equal(runtimeRequests().length, 1);
5764+
5765+
const otherMessage = yield* elicit({ ...approvalOnly, message: "Deploy api?" });
5766+
yield* awaitCard(2);
5767+
const otherServer = yield* elicit({ ...approvalOnly, serverName: "builds" });
5768+
yield* awaitCard(3);
5769+
assert.isUndefined(otherMessage.pollUnsafe());
5770+
assert.isUndefined(otherServer.pollUnsafe());
5771+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
57455772
);
57465773

57475774
it.effect("declines URL and value forms without a card", () =>

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3825,11 +3825,12 @@ export const awaitClaudeApprovalDecision = Effect.fn("awaitClaudeApprovalDecisio
38253825
return yield* Effect.raceFirst(Deferred.await(decision), cancellation);
38263826
});
38273827

3828-
// Web and mobile offer a session grant when options are omitted, and this
3829-
// path only ever answers once.
3828+
// MCP servers such as Computer Use ask on every call and expect the client to
3829+
// remember a session grant, as Codex does. The CLI drops a reply's `_meta.persist`.
38303830
const CLAUDE_MCP_ELICITATION_OPTIONS: ReadonlyArray<ProviderApprovalOption> = [
38313831
{ decision: "cancel", label: "Cancel" },
38323832
{ decision: "decline", label: "Decline" },
3833+
{ decision: "acceptForSession", label: "Always allow this session" },
38333834
{ decision: "accept", label: "Approve" },
38343835
];
38353836

@@ -5212,6 +5213,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
52125213
const pendingRuntimeRequests = yield* Ref.make(
52135214
new Map<string, PendingClaudeRuntimeRequest>(),
52145215
);
5216+
const mcpElicitationSessionGrants = yield* Ref.make(new Set<string>());
52155217
// Background local_bash tasks can trigger wake turns on the query stream with no active provider turn.
52165218
const lastTurnRouteByNativeThread = yield* Ref.make(
52175219
new Map<
@@ -10620,6 +10622,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1062010622
);
1062110623
return { action: "decline" } satisfies ElicitationResult;
1062210624
}
10625+
const grantKey = `${request.serverName}\u0000${request.message}`;
10626+
if ((yield* Ref.get(mcpElicitationSessionGrants)).has(grantKey)) {
10627+
return acceptance;
10628+
}
1062310629
// The CLI waits on this answer, so a turn held for its prompt echo
1062410630
// must show its output now, as canUseTool does.
1062510631
if (context.heldRootFrames.length > 0) {
@@ -10706,8 +10712,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1070610712
runtimeRequest: { ...artifacts.request, status: "cancelled", resolvedAt: now },
1070710713
});
1070810714
}
10715+
if (resolved === "acceptForSession") {
10716+
yield* Ref.update(mcpElicitationSessionGrants, (current) =>
10717+
new Set(current).add(grantKey),
10718+
);
10719+
return acceptance;
10720+
}
1070910721
// Never null: the SDK reads null as answered out of band and leaves
10710-
// the server waiting. Every accept is one-time; no grant is offered.
10722+
// the server waiting.
1071110723
return resolved === "accept"
1071210724
? acceptance
1071310725
: ({

0 commit comments

Comments
 (0)