Skip to content

Commit 374d3ed

Browse files
piero-dev25claude
andcommitted
fix(claude): pingdotgg#155 — isolate in-app agents from ambient MCP so harness tools reach them
Root cause (evidence-grade, captured from the real `claude` CLI invocation): the per-turn ClaudeAdapter query loaded settingSources [user,project,local] with no strictMcpConfig and no explicit mcpServers base, so the SDK ALSO merged the operator's ambient MCP config — filesystem (~/.claude.json + project .mcp.json) AND connected claude.ai servers (Gmail/Drive/Strava/...) — alongside the injected devgame /mcp entry. Those ambient servers surfaced to in-app agents (a privacy leak) AND crowded out the harness's own generate_3d / preview tools, so no harness tool reached the agent (pingdotgg#155). The capability probe already isolates itself this way (ClaudeProvider.buildClaudeCapabilitiesProbeQueryOptions); commit aa5ec80 (pingdotgg#4015) wired that isolation only to the probe, never to the real per-turn path. Fix — mirror the probe's isolation on the real per-turn query: - strictMcpConfig: true + env ENABLE_CLAUDEAI_MCP_SERVERS=false, UNCONDITIONAL for every in-app Claude turn (privacy must not depend on whether devgame is injected). - keep mcpServers:{devgame} additive when a harness session exists; add alwaysLoad:true so its tools land in the turn-1 prompt instead of being deferred behind tool-search. - settingSources unchanged, so workspace commands / CLAUDE.md still work. This is also a real privacy/security fix: the operator's personal MCP servers no longer leak into in-app agents. Guard (ClaudeAdapter.test.ts): two red-green tests — (1) with an mcpSession, the query isolates + injects only devgame with alwaysLoad; (2) with NO mcpSession, isolation still holds and no server is injected (the privacy property is independent of harness injection). Both proven to go red when their lever is removed. tsgo clean; ClaudeAdapter suite 70/70. Verified structurally live: the real claude CLI invocation now carries --strict-mcp-config (absent before the fix). The in-app generate_3d E2E on the real desktop app remains the closing gate (standalone dev rig is auth-blocked by OAuth contention, not fix-blocked). Reviewed by an independent Opus pass (correct + mergeable); its two actionable findings (unconditional privacy levers; alwaysLoad) are folded in. Sibling adapters (Grok/Cursor/Codex/OpenCode) inject devgame without this isolation but use array-style mcpServers — unverified, tracked separately. Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent bae21a1 commit 374d3ed

2 files changed

Lines changed: 93 additions & 1 deletion

File tree

‎apps/server/src/provider/Layers/ClaudeAdapter.test.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
import {
1515
ApprovalRequestId,
1616
ClaudeSettings,
17+
EnvironmentId,
1718
ProviderDriverKind,
1819
ProviderItemId,
1920
ProviderRuntimeEvent,
@@ -34,6 +35,7 @@ import * as TestClock from "effect/testing/TestClock";
3435

3536
import { attachmentRelativePath } from "../../attachmentStore.ts";
3637
import { ServerConfig } from "../../config.ts";
38+
import * as McpProviderSession from "../../mcp/McpProviderSession.ts";
3739
import { ServerSettingsService } from "../../serverSettings.ts";
3840
import { ProviderAdapterProcessError, ProviderAdapterValidationError } from "../Errors.ts";
3941
import type { ClaudeAdapterShape } from "../Services/ClaudeAdapter.ts";
@@ -271,6 +273,7 @@ async function readFirstPromptMessage(
271273

272274
const THREAD_ID = ThreadId.make("thread-claude-1");
273275
const RESUME_THREAD_ID = ThreadId.make("thread-claude-resume");
276+
const MCP_ISOLATION_THREAD_ID = ThreadId.make("thread-claude-mcp-isolation");
274277

275278
describe("ClaudeAdapterLive", () => {
276279
it.effect("returns validation error for non-claude provider on startSession", () => {
@@ -400,6 +403,74 @@ describe("ClaudeAdapterLive", () => {
400403
);
401404
});
402405

406+
it.effect(
407+
"isolates in-app agent turns from the operator's ambient MCP config when an mcpSession is present (#155)",
408+
() => {
409+
const harness = makeHarness();
410+
const endpoint = "http://127.0.0.1:0/mcp";
411+
const authorizationHeader = "Bearer test-mcp-session-token";
412+
McpProviderSession.setMcpProviderSession({
413+
environmentId: EnvironmentId.make("env-mcp-isolation"),
414+
threadId: MCP_ISOLATION_THREAD_ID,
415+
providerSessionId: "provider-session-mcp-isolation",
416+
providerInstanceId: ProviderInstanceId.make("claudeAgent"),
417+
endpoint,
418+
authorizationHeader,
419+
});
420+
421+
return Effect.gen(function* () {
422+
const adapter = yield* ClaudeAdapter;
423+
yield* adapter.startSession({
424+
threadId: MCP_ISOLATION_THREAD_ID,
425+
provider: ProviderDriverKind.make("claudeAgent"),
426+
runtimeMode: "approval-required",
427+
});
428+
429+
const createInput = harness.getLastCreateQueryInput();
430+
assert.equal(createInput?.options.strictMcpConfig, true);
431+
assert.equal(createInput?.options.env?.["ENABLE_CLAUDEAI_MCP_SERVERS"], "false");
432+
assert.deepEqual(Object.keys(createInput?.options.mcpServers ?? {}), ["devgame"]);
433+
const devgameServer = createInput?.options.mcpServers?.["devgame"];
434+
assert.deepEqual(devgameServer, {
435+
type: "http",
436+
url: endpoint,
437+
headers: { Authorization: authorizationHeader },
438+
alwaysLoad: true,
439+
});
440+
assert.equal(devgameServer?.type === "http" ? devgameServer.alwaysLoad : undefined, true);
441+
}).pipe(
442+
Effect.provideService(Random.Random, makeDeterministicRandomService()),
443+
Effect.provide(harness.layer),
444+
Effect.ensuring(
445+
Effect.sync(() => McpProviderSession.clearMcpProviderSession(MCP_ISOLATION_THREAD_ID)),
446+
),
447+
);
448+
},
449+
);
450+
451+
it.effect(
452+
"isolates in-app turns from ambient MCP even with NO mcpSession — privacy independent of harness injection (#155)",
453+
() => {
454+
const harness = makeHarness();
455+
return Effect.gen(function* () {
456+
const adapter = yield* ClaudeAdapter;
457+
yield* adapter.startSession({
458+
threadId: THREAD_ID,
459+
provider: ProviderDriverKind.make("claudeAgent"),
460+
runtimeMode: "approval-required",
461+
});
462+
463+
const createInput = harness.getLastCreateQueryInput();
464+
assert.equal(createInput?.options.strictMcpConfig, true);
465+
assert.equal(createInput?.options.env?.["ENABLE_CLAUDEAI_MCP_SERVERS"], "false");
466+
assert.equal(createInput?.options.mcpServers, undefined);
467+
}).pipe(
468+
Effect.provideService(Random.Random, makeDeterministicRandomService()),
469+
Effect.provide(harness.layer),
470+
);
471+
},
472+
);
473+
403474
it.effect("uses bypass permissions for full-access claude sessions", () => {
404475
const harness = makeHarness();
405476
return Effect.gen(function* () {

‎apps/server/src/provider/Layers/ClaudeAdapter.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4195,18 +4195,39 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
41954195
...(newSessionId ? { sessionId: newSessionId } : {}),
41964196
includePartialMessages: true,
41974197
canUseTool,
4198-
env: claudeEnvironment,
4198+
// #155: isolate EVERY in-app Claude turn from the operator's ambient MCP
4199+
// config, independent of whether the devgame harness is injected this turn.
4200+
// settingSources stays [user,project,local] (needed for workspace commands /
4201+
// CLAUDE.md) but that ALSO merges the machine's ~/.claude.json + project
4202+
// .mcp.json MCP servers, and connected claude.ai servers merge in separately.
4203+
// Without this the operator's personal MCP servers (Gmail/Drive/Strava/etc.)
4204+
// surface to in-app agents AND crowd out the harness's injected `devgame`
4205+
// tools. Mirror the capability probe's isolation
4206+
// (ClaudeProvider.ts:buildClaudeCapabilitiesProbeQueryOptions; commit
4207+
// aa5ec8036/#4015 only wired it to the probe, never this real per-turn path):
4208+
// strictMcpConfig makes the SDK use ONLY the explicit `mcpServers` map (empty
4209+
// here unless devgame is injected below), and ENABLE_CLAUDEAI_MCP_SERVERS=false
4210+
// drops connected claude.ai servers. Kept UNCONDITIONAL (not gated on
4211+
// mcpSession) so the privacy boundary can't silently reopen on a turn that
4212+
// happens not to inject a harness server.
4213+
strictMcpConfig: true,
4214+
env: { ...claudeEnvironment, ENABLE_CLAUDEAI_MCP_SERVERS: "false" },
41994215
...(input.cwd ? { additionalDirectories: [input.cwd] } : {}),
42004216
...(Object.keys(extraArgs).length > 0 ? { extraArgs } : {}),
42014217
...(mcpSession
42024218
? {
4219+
// The injected harness /mcp server is the SOLE MCP source (strictMcpConfig
4220+
// above). `alwaysLoad: true` forces its tools into the turn-1 prompt
4221+
// (blocks until connected, capped ~5s) rather than deferring them behind
4222+
// tool-search, so preview + generation tools reliably reach the agent.
42034223
mcpServers: {
42044224
devgame: {
42054225
type: "http",
42064226
url: mcpSession.endpoint,
42074227
headers: {
42084228
Authorization: mcpSession.authorizationHeader,
42094229
},
4230+
alwaysLoad: true,
42104231
},
42114232
},
42124233
}

0 commit comments

Comments
 (0)