From ca4595d873b3dd22c98ae7c8c6a906a78663d28c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 06:58:19 +0000 Subject: [PATCH] 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 pingdotgg/t3code#30141 AI-assisted; verified with the tests and tools listed above. --- .../Adapters/ClaudeAdapterV2.test.ts | 16 +++++++++++++ .../Adapters/ClaudeAdapterV2.ts | 24 +++++++++++++------ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 82004eee5e035..5cb15ddf92833 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -344,6 +344,22 @@ describe("ClaudeAdapterV2 runtime query policy", () => { ); }); + it("makes a resumed Claude session wait for its MCP servers before the first request", () => { + const appThread = { + lineage: { + parentThreadId: null, + relationshipToParent: null, + rootThreadId: ThreadId.make("t"), + }, + }; + const nonblocking = (environment: NodeJS.ProcessEnv, resume: boolean) => + ClaudeAdapterV2.claudeQueryEnvironment(environment, appThread, undefined, resume) + .MCP_CONNECTION_NONBLOCKING; + assert.equal(nonblocking({}, true), "0"); + assert.isUndefined(nonblocking({}, false)); + assert.equal(nonblocking({ MCP_CONNECTION_NONBLOCKING: "1" }, true), "1"); + }); + it("pins Claude's auto tool search on through flag settings", () => { const override = ClaudeAdapterV2.claudeToolSearchOverride; for (const value of ["auto", "auto:10", "auto:99.5", "auto:x", "auto:Infinity"]) { diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 19e77d4c98eee..5b04618750f2a 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -1841,20 +1841,29 @@ function claudeLiveSelectionFlagSettings( // A delegated child is its own SDK session, so Claude Code gives it the main-conversation cache // TTL. When the user sets Claude's subagentPromptCacheTtl, the child follows it like a native -// subagent would. An explicit CLAUDE_CODE_PROMPT_CACHE_TTL on the instance still wins. +// subagent would. A resumed session's first request otherwise goes out before its MCP servers +// connect, and Claude Code tells the agent they disconnected. Explicit instance values still win. export function claudeQueryEnvironment( environment: NodeJS.ProcessEnv, appThread: Pick, subagentPromptCacheTtl: "5m" | "1h" | undefined, + resume = false, ): NodeJS.ProcessEnv { - if ( - appThread.lineage.relationshipToParent !== "subagent" || - subagentPromptCacheTtl === undefined || - environment.CLAUDE_CODE_PROMPT_CACHE_TTL !== undefined - ) { + const promptCacheTtl = + appThread.lineage.relationshipToParent === "subagent" && + subagentPromptCacheTtl !== undefined && + environment.CLAUDE_CODE_PROMPT_CACHE_TTL === undefined + ? subagentPromptCacheTtl + : undefined; + const blockingMcpConnect = resume && environment.MCP_CONNECTION_NONBLOCKING === undefined; + if (promptCacheTtl === undefined && !blockingMcpConnect) { return environment; } - return { ...environment, CLAUDE_CODE_PROMPT_CACHE_TTL: subagentPromptCacheTtl }; + return { + ...environment, + ...(promptCacheTtl === undefined ? {} : { CLAUDE_CODE_PROMPT_CACHE_TTL: promptCacheTtl }), + ...(blockingMcpConnect ? { MCP_CONNECTION_NONBLOCKING: "0" } : {}), + }; } // 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* ( turnInput.appThread.lineage.relationshipToParent === "subagent" ? yield* resolveSubagentPromptCacheTtl(turnInput.runtimePolicy.cwd) : undefined, + shouldResume, ), tools: queryPolicy.tools ?? CLAUDE_CODE_PRESET_TOOLS, ...(mcpOverrides.allowedTools === undefined