Skip to content

Commit ca4595d

Browse files
fix(server): resumed Claude sessions wait for their MCP servers
After an idle release, the next turn resumes the Claude session in a new process. Claude Code connects MCP servers without blocking, so the first request went out before they were ready, and Claude Code told the agent every MCP server and connector had disconnected, then reconnected later in the same turn. Resumed queries now start with MCP_CONNECTION_NONBLOCKING=0, so Claude Code waits (up to its connect timeout) for the servers before the first request. An explicit value in the instance environment still wins. Fixes #30141 AI-assisted; verified with the tests and tools listed above.
1 parent a18a458 commit ca4595d

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,22 @@ describe("ClaudeAdapterV2 runtime query policy", () => {
344344
);
345345
});
346346

347+
it("makes a resumed Claude session wait for its MCP servers before the first request", () => {
348+
const appThread = {
349+
lineage: {
350+
parentThreadId: null,
351+
relationshipToParent: null,
352+
rootThreadId: ThreadId.make("t"),
353+
},
354+
};
355+
const nonblocking = (environment: NodeJS.ProcessEnv, resume: boolean) =>
356+
ClaudeAdapterV2.claudeQueryEnvironment(environment, appThread, undefined, resume)
357+
.MCP_CONNECTION_NONBLOCKING;
358+
assert.equal(nonblocking({}, true), "0");
359+
assert.isUndefined(nonblocking({}, false));
360+
assert.equal(nonblocking({ MCP_CONNECTION_NONBLOCKING: "1" }, true), "1");
361+
});
362+
347363
it("pins Claude's auto tool search on through flag settings", () => {
348364
const override = ClaudeAdapterV2.claudeToolSearchOverride;
349365
for (const value of ["auto", "auto:10", "auto:99.5", "auto:x", "auto:Infinity"]) {

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,20 +1841,29 @@ function claudeLiveSelectionFlagSettings(
18411841

18421842
// A delegated child is its own SDK session, so Claude Code gives it the main-conversation cache
18431843
// TTL. When the user sets Claude's subagentPromptCacheTtl, the child follows it like a native
1844-
// subagent would. An explicit CLAUDE_CODE_PROMPT_CACHE_TTL on the instance still wins.
1844+
// subagent would. A resumed session's first request otherwise goes out before its MCP servers
1845+
// connect, and Claude Code tells the agent they disconnected. Explicit instance values still win.
18451846
export function claudeQueryEnvironment(
18461847
environment: NodeJS.ProcessEnv,
18471848
appThread: Pick<OrchestrationV2AppThread, "lineage">,
18481849
subagentPromptCacheTtl: "5m" | "1h" | undefined,
1850+
resume = false,
18491851
): NodeJS.ProcessEnv {
1850-
if (
1851-
appThread.lineage.relationshipToParent !== "subagent" ||
1852-
subagentPromptCacheTtl === undefined ||
1853-
environment.CLAUDE_CODE_PROMPT_CACHE_TTL !== undefined
1854-
) {
1852+
const promptCacheTtl =
1853+
appThread.lineage.relationshipToParent === "subagent" &&
1854+
subagentPromptCacheTtl !== undefined &&
1855+
environment.CLAUDE_CODE_PROMPT_CACHE_TTL === undefined
1856+
? subagentPromptCacheTtl
1857+
: undefined;
1858+
const blockingMcpConnect = resume && environment.MCP_CONNECTION_NONBLOCKING === undefined;
1859+
if (promptCacheTtl === undefined && !blockingMcpConnect) {
18551860
return environment;
18561861
}
1857-
return { ...environment, CLAUDE_CODE_PROMPT_CACHE_TTL: subagentPromptCacheTtl };
1862+
return {
1863+
...environment,
1864+
...(promptCacheTtl === undefined ? {} : { CLAUDE_CODE_PROMPT_CACHE_TTL: promptCacheTtl }),
1865+
...(blockingMcpConnect ? { MCP_CONNECTION_NONBLOCKING: "0" } : {}),
1866+
};
18581867
}
18591868

18601869
// Claude Code's auto tool search sends every MCP schema in full while they fit under a share of the
@@ -11786,6 +11795,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1178611795
turnInput.appThread.lineage.relationshipToParent === "subagent"
1178711796
? yield* resolveSubagentPromptCacheTtl(turnInput.runtimePolicy.cwd)
1178811797
: undefined,
11798+
shouldResume,
1178911799
),
1179011800
tools: queryPolicy.tools ?? CLAUDE_CODE_PRESET_TOOLS,
1179111801
...(mcpOverrides.allowedTools === undefined

0 commit comments

Comments
 (0)