Skip to content

Commit 747e7ca

Browse files
fix(server): Claude turns wait for MCP servers before the first request
The merge with main kept main's environment sanitizing and dropped this branch's blocking MCP connect default. Restore it on top of main's environment, keeping instance and host overrides, and cover it with a test. AI-assisted; verified with the tests and tools listed above.
1 parent 4501c04 commit 747e7ca

3 files changed

Lines changed: 43 additions & 9 deletions

File tree

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,7 @@ const captureSdkExecutablePaths = Effect.fn("captureSdkExecutablePaths")(functio
13521352
environment: ResolvedProviderInstanceEnvironment = [],
13531353
) {
13541354
const executablePaths: Array<string | undefined> = [];
1355-
const taskToolEnvironment: Array<string | undefined> = [];
1355+
const queryEnvironments: Array<NodeJS.ProcessEnv | undefined> = [];
13561356
const adapter = yield* ClaudeAdapterV2.createClaudeAdapterV2(
13571357
{
13581358
instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,
@@ -1373,7 +1373,7 @@ const captureSdkExecutablePaths = Effect.fn("captureSdkExecutablePaths")(functio
13731373
open: (input) =>
13741374
Effect.sync(() => {
13751375
executablePaths.push(input.options.pathToClaudeCodeExecutable);
1376-
taskToolEnvironment.push(input.options.env?.CLAUDE_CODE_ENABLE_TODO_TOOLS);
1376+
queryEnvironments.push(input.options.env);
13771377
return {
13781378
messages: Stream.never,
13791379
offer: () => Effect.void,
@@ -1417,7 +1417,7 @@ const captureSdkExecutablePaths = Effect.fn("captureSdkExecutablePaths")(functio
14171417
attachments: [],
14181418
}),
14191419
);
1420-
return { executablePaths, taskToolEnvironment };
1420+
return { executablePaths, queryEnvironments };
14211421
});
14221422

14231423
describe("ClaudeAdapterV2 executable path", () => {
@@ -1447,6 +1447,39 @@ describe("ClaudeAdapterV2 executable path", () => {
14471447
);
14481448
});
14491449

1450+
describe("ClaudeAdapterV2 turn environment", () => {
1451+
it.effect("connects MCP servers before the first request unless the environment opts out", () =>
1452+
Effect.gen(function* () {
1453+
const nonblocking = (environment: ResolvedProviderInstanceEnvironment) =>
1454+
captureSdkExecutablePaths("/usr/local/bin/claude", environment).pipe(
1455+
Effect.provideService(HostProcess.Platform, "linux"),
1456+
Effect.provideService(HostProcess.Environment, { PATH: "/usr/bin" }),
1457+
Effect.map(({ queryEnvironments }) => queryEnvironments[0]?.MCP_CONNECTION_NONBLOCKING),
1458+
Effect.scoped,
1459+
);
1460+
1461+
assert.deepEqual(
1462+
[
1463+
yield* nonblocking([]),
1464+
yield* nonblocking([
1465+
{ name: "MCP_CONNECTION_NONBLOCKING", value: "true", sensitive: false },
1466+
]),
1467+
],
1468+
["false", "true"],
1469+
);
1470+
}).pipe(
1471+
Effect.provide(
1472+
Layer.mergeAll(
1473+
IdAllocator.layer,
1474+
McpProviderSessions.layer,
1475+
NodeServices.layer,
1476+
NodeCrypto.layer,
1477+
),
1478+
),
1479+
),
1480+
);
1481+
});
1482+
14501483
describe("ClaudeAdapterV2 resumeThread", () => {
14511484
it.effect("fails for a transcript without messages so a fresh session replaces it", () =>
14521485
Effect.scoped(

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12447,7 +12447,12 @@ export const createClaudeAdapterV2 = Effect.fn("ClaudeAdapterV2Driver.create")(
1244712447
environment,
1244812448
hostEnvironment,
1244912449
);
12450-
const claudeEnvironment = yield* makeClaudeEnvironment(config, baseEnvironment);
12450+
const claudeEnvironment = yield* makeClaudeEnvironment(config, {
12451+
// Non-blocking connect sends whichever MCP tools are ready, so the first request's tool
12452+
// list varies per thread and a late server rewrites the whole prompt cache mid-turn.
12453+
MCP_CONNECTION_NONBLOCKING: "false",
12454+
...baseEnvironment,
12455+
});
1245112456
const path = yield* Path.Path;
1245212457
const random = yield* SecureRandom.SecureRandom;
1245312458
const binaryPath = yield* resolveClaudeSdkExecutablePath(

‎apps/server/src/provider/Drivers/ClaudeDriver.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,7 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv, Path.
111111
const modelCatalog = catalogService
112112
.current(DRIVER_KIND)
113113
.pipe(Effect.map(resolveClaudeModelCatalog));
114-
const processEnv = yield* mergeClaudeProcessEnvironment(
115-
config,
116-
environment,
117-
yield* HostProcess.Environment,
118-
);
114+
const processEnv = yield* mergeClaudeProcessEnvironment(config, environment, process.env);
119115
const fallbackContinuationIdentity = defaultProviderContinuationIdentity({
120116
driverKind: DRIVER_KIND,
121117
instanceId,

0 commit comments

Comments
 (0)