Skip to content

Commit afd9707

Browse files
Gourav224claude
andcommitted
fix(server): a Claude MCP registration timeout no longer discards the native session
When Claude never answered the MCP server registration, the turn failed before the prompt was offered, but the pending context-handoff marker was left in place. The next turn then treated delivery as uncertain, skipped resume, and replaced the native session with an empty one that needed a full-thread handoff, which could exceed the budget and wedge the thread. Give the timeout its own error and count it as a failure before the prompt, so the marker is restored like the other pre-prompt refusals. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 67c0fc0 commit afd9707

4 files changed

Lines changed: 27 additions & 10 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2390,6 +2390,9 @@ describe("ClaudeAdapterV2 MCP credential channel", () => {
23902390
const opened = yield* Fiber.join(opening);
23912391

23922392
assert.equal(opened._tag, "Failure");
2393+
if (opened._tag === "Failure") {
2394+
assert.instanceOf(opened.failure, ClaudeAdapterV2.ClaudeMcpRegistrationTimeoutError);
2395+
}
23932396
assert.isTrue(cli.closed());
23942397
}),
23952398
);

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,19 @@ export class ClaudeBackgroundWorkBlocksQueryReplacementError extends Schema.Tagg
517517
}
518518
}
519519

520+
/**
521+
* Raised while the query is opening, before the user's prompt is offered, so the
522+
* native session never saw the turn.
523+
*/
524+
export class ClaudeMcpRegistrationTimeoutError extends Schema.TaggedError<ClaudeMcpRegistrationTimeoutError>()(
525+
"ClaudeMcpRegistrationTimeoutError",
526+
{},
527+
) {
528+
override get message(): string {
529+
return "Claude Code did not answer the MCP server registration.";
530+
}
531+
}
532+
520533
export class ClaudeAppendSystemPromptFileError extends Schema.TaggedError<ClaudeAppendSystemPromptFileError>()(
521534
"ClaudeAppendSystemPromptFileError",
522535
{
@@ -542,7 +555,10 @@ export interface ClaudeAgentSdkQueryRunnerShape {
542555
readonly allocateSessionId: Effect.Effect<string, ClaudeAgentSdkQueryRunnerError>;
543556
readonly open: (
544557
input: ClaudeAgentSdkQueryOpenInput,
545-
) => Effect.Effect<ClaudeAgentSdkQuerySession, ClaudeAgentSdkQueryRunnerError>;
558+
) => Effect.Effect<
559+
ClaudeAgentSdkQuerySession,
560+
ClaudeAgentSdkQueryRunnerError | ClaudeMcpRegistrationTimeoutError
561+
>;
546562
readonly forkSession: (
547563
input: ClaudeAgentSdkSessionForkInput,
548564
) => Effect.Effect<
@@ -947,13 +963,7 @@ export const layerQueryRunner: Layer.Layer<
947963
Effect.orElseSucceed(() => Object.keys(mcpServers)),
948964
Effect.timeoutOrElse({
949965
duration: CLAUDE_MCP_REGISTRATION_TIMEOUT,
950-
orElse: () =>
951-
Effect.fail(
952-
queryRunnerError(
953-
"Claude Code did not answer the MCP server registration.",
954-
"setMcpServers",
955-
),
956-
),
966+
orElse: () => Effect.fail(new ClaudeMcpRegistrationTimeoutError()),
957967
}),
958968
);
959969
if (failed.length > 0) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ export class ProviderTurnStartError extends Schema.TaggedError<ProviderTurnStart
7070
const isProviderTurnStartError = Schema.is(ProviderTurnStartError);
7171

7272
/**
73-
* Claude refuses these before it reads the prompt: replacing a process running background work,
74-
* or launch arguments whose appended system prompt cannot be read.
73+
* Claude fails these before it reads the prompt: replacing a process running background work,
74+
* launch arguments whose appended system prompt cannot be read, or an MCP registration that
75+
* never answers while the query opens.
7576
*/
7677
const PROMPT_REFUSAL_TAGS = [
7778
"ClaudeBackgroundWorkBlocksQueryReplacementError",
79+
"ClaudeMcpRegistrationTimeoutError",
7880
"ClaudeAppendSystemPromptFileError",
7981
"ClaudeAppendSystemPromptMissingValueError",
8082
] as const;

‎apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
ClaudeAppendSystemPromptFileError,
3939
ClaudeAppendSystemPromptMissingValueError,
4040
ClaudeBackgroundWorkBlocksQueryReplacementError,
41+
ClaudeMcpRegistrationTimeoutError,
4142
ClaudeProviderCapabilitiesV2,
4243
} from "../Adapters/ClaudeAdapterV2.ts";
4344
import {
@@ -1207,6 +1208,7 @@ describe("orchestration v2 provider switching", () => {
12071208
"a missing append value",
12081209
new ClaudeAppendSystemPromptMissingValueError({ flag: "--append-system-prompt" }),
12091210
],
1211+
["an unanswered MCP registration", new ClaudeMcpRegistrationTimeoutError()],
12101212
] as const)("keeps the native session after turns refused for %s", ([, refusalCause]) =>
12111213
Effect.scoped(
12121214
Effect.gen(function* () {

0 commit comments

Comments
 (0)