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