Skip to content

Commit 157dbc0

Browse files
fix(server): refuse Claude MCP App calls that could route to another server
Claude Code routes `mcp__probe__server__tool` to server `probe` even when `probe__server` is connected, so an app's read-only check could approve a mutating tool on a different server. Capture keeps a plain row and `callTool` refuses whenever the app server's tool prefix overlaps another server's. A Claude app request with no live process now reports `session-stopped` instead of a generic request failure, through a new `session_stopped` provider failure reason. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8462d80 commit 157dbc0

6 files changed

Lines changed: 147 additions & 10 deletions

File tree

‎apps/server/src/mcpApps/McpAppRequests.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ProviderDriverKind,
23
ProviderSessionId,
34
ProviderThreadId,
45
ThreadId,
@@ -14,7 +15,7 @@ import * as Layer from "effect/Layer";
1415
import * as Option from "effect/Option";
1516

1617
import * as Orchestrator from "../orchestration-v2/Orchestrator.ts";
17-
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
18+
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
1819
import * as ProviderSessionManager from "../orchestration-v2/ProviderSessionManager.ts";
1920
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
2021
import * as McpAppModelContext from "./McpAppModelContext.ts";
@@ -189,6 +190,33 @@ describe("McpAppRequests", () => {
189190
}).pipe(Effect.provide(makeLayer({ item: appItem({ t3McpApp: app }), mcpApps }))),
190191
);
191192

193+
it.effect("reports a provider whose process has stopped as a stopped session", () =>
194+
Effect.gen(function* () {
195+
const requests = yield* McpAppRequests.McpAppRequests;
196+
assert.equal(
197+
yield* reason(requests.readResource({ threadId, itemId, uri: "ui://weather/app.html" })),
198+
"session-stopped",
199+
);
200+
}).pipe(
201+
Effect.provide(
202+
makeLayer({
203+
item: appItem({ t3McpApp: app }),
204+
mcpApps: {
205+
...mcpApps,
206+
readResource: () =>
207+
Effect.fail(
208+
new ProviderAdapter.ProviderAdapterProtocolError({
209+
driver: ProviderDriverKind.make("claudeAgent"),
210+
detail: "stopped",
211+
reason: "session_stopped",
212+
}),
213+
),
214+
},
215+
}),
216+
),
217+
),
218+
);
219+
192220
it.effect("keeps each app's latest model context, without a live session", () =>
193221
Effect.gen(function* () {
194222
const requests = yield* McpAppRequests.McpAppRequests;

‎apps/server/src/mcpApps/McpAppRequests.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ const make = Effect.gen(function* () {
6464

6565
const fail = (threadId: ThreadId, reason: McpAppRequestError["reason"], cause?: unknown) =>
6666
new McpAppRequestError({ threadId, reason, ...(cause === undefined ? {} : { cause }) });
67+
const adapterFailure = (threadId: ThreadId) => (cause: ProviderAdapter.ProviderAdapterV2Error) =>
68+
fail(
69+
threadId,
70+
cause._tag === "ProviderAdapterProtocolError" && cause.reason === "session_stopped"
71+
? "session-stopped"
72+
: "request-failed",
73+
cause,
74+
);
6775

6876
const resolveApp = Effect.fn("McpAppRequests.resolveApp")(function* (input: {
6977
readonly threadId: ThreadId;
@@ -112,7 +120,7 @@ const make = Effect.gen(function* () {
112120
) {
113121
const tools = yield* resolved.mcpApps
114122
.listTools({ providerThread: resolved.providerThread, server: resolved.app.server })
115-
.pipe(Effect.mapError((cause) => fail(threadId, "request-failed", cause)));
123+
.pipe(Effect.mapError(adapterFailure(threadId)));
116124
return tools.find((tool) => tool.name === name);
117125
});
118126

@@ -142,7 +150,7 @@ const make = Effect.gen(function* () {
142150
tool: input.name,
143151
arguments: input.arguments,
144152
})
145-
.pipe(Effect.mapError((cause) => fail(input.threadId, "request-failed", cause)));
153+
.pipe(Effect.mapError(adapterFailure(input.threadId)));
146154
});
147155

148156
const readResource = Effect.fn("McpAppRequests.readResource")(function* (
@@ -155,7 +163,7 @@ const make = Effect.gen(function* () {
155163
server: resolved.app.server,
156164
uri: input.uri,
157165
})
158-
.pipe(Effect.mapError((cause) => fail(input.threadId, "request-failed", cause)));
166+
.pipe(Effect.mapError(adapterFailure(input.threadId)));
159167
});
160168

161169
const descendsFrom = Effect.fn("McpAppRequests.descendsFrom")(function* (

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4948,6 +4948,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
49484948
overrides?: Partial<
49494949
Pick<ClaudeAdapterV2.ClaudeAgentSdkQuerySession, "mcpServerStatus" | "readMcpResource">
49504950
>,
4951+
toolName = "mcp__probe_server__show_weather",
49514952
) {
49524953
const mcpRequests: Array<unknown> = [];
49534954
const harness = yield* makeWakeHarnessWithOptions({
@@ -5012,7 +5013,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
50125013
{
50135014
type: "tool_use",
50145015
id,
5015-
name: "mcp__probe_server__show_weather",
5016+
name: toolName,
50165017
input: { city: "Oslo" },
50175018
},
50185019
],
@@ -5138,6 +5139,71 @@ describe("ClaudeAdapterV2 background wake turns", () => {
51385139
),
51395140
);
51405141

5142+
it.effect("refuses an app tool call Claude Code could route to an overlapping server", () =>
5143+
Effect.gen(function* () {
5144+
// Claude Code 2.1.303 runs `mcp__probe__server__refresh_weather` on `probe`.
5145+
const { harness, item, mcpRequests } = yield* runClaudeMcpAppToolTurn(
5146+
MCP_APP_CAPABILITIES,
5147+
{
5148+
mcpServerStatus: Effect.succeed([
5149+
{ ...PROBE_SERVER_STATUS, name: "probe__server" },
5150+
{
5151+
name: "probe",
5152+
status: "connected",
5153+
tools: [{ name: "refresh_weather", _meta: { ui: { visibility: ["app"] } } }],
5154+
},
5155+
]),
5156+
},
5157+
"mcp__probe__server__show_weather",
5158+
);
5159+
assert.isUndefined(mcpAppFromToolItem(item));
5160+
const exit = yield* Effect.exit(
5161+
harness.runtime.mcpApps!.callTool({
5162+
providerThread: harness.providerThread,
5163+
server: "probe__server",
5164+
tool: "refresh_weather",
5165+
arguments: {},
5166+
}),
5167+
);
5168+
assert.isTrue(Exit.isFailure(exit));
5169+
assert.deepEqual(mcpRequests, []);
5170+
}).pipe(
5171+
Effect.scoped,
5172+
Effect.provide(
5173+
Layer.mergeAll(
5174+
IdAllocator.layer,
5175+
McpProviderSessions.layer,
5176+
NodeServices.layer,
5177+
NodeCrypto.layer,
5178+
),
5179+
),
5180+
),
5181+
);
5182+
5183+
it.effect("keeps a plain MCP tool row when reading its ui:// resource times out", () =>
5184+
Effect.gen(function* () {
5185+
const reading = yield* Deferred.make<void>();
5186+
const turn = yield* runClaudeMcpAppToolTurn(MCP_APP_CAPABILITIES, {
5187+
readMcpResource: () =>
5188+
Deferred.succeed(reading, undefined).pipe(Effect.andThen(Effect.never)),
5189+
}).pipe(Effect.forkChild);
5190+
yield* Deferred.await(reading);
5191+
yield* TestClock.adjust("21 seconds");
5192+
const { item } = yield* Fiber.join(turn);
5193+
assert.isUndefined(mcpAppFromToolItem(item));
5194+
}).pipe(
5195+
Effect.scoped,
5196+
Effect.provide(
5197+
Layer.mergeAll(
5198+
IdAllocator.layer,
5199+
McpProviderSessions.layer,
5200+
NodeServices.layer,
5201+
NodeCrypto.layer,
5202+
),
5203+
),
5204+
),
5205+
);
5206+
51415207
it.effect("keeps a plain MCP tool row when the Claude CLI lacks MCP Apps support", () =>
51425208
Effect.gen(function* () {
51435209
const { harness, item, mcpRequests } = yield* runClaudeMcpAppToolTurn([]);

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

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,19 @@ function claudeSupportsMcpApps(capabilities: ReadonlySet<string>): boolean {
751751
return CLAUDE_MCP_APP_CAPABILITIES.every((capability) => capabilities.has(capability));
752752
}
753753

754+
// Claude Code routes `mcp__a__b__tool` to server `a` even when `a__b` is connected, so either prefix is ambiguous.
755+
function claudeMcpServerPrefixOverlaps(
756+
statuses: ReadonlyArray<ClaudeMcpServerStatus>,
757+
server: string,
758+
): boolean {
759+
const prefix = claudeMcpToolPrefix(server);
760+
return statuses.some((status) => {
761+
if (status.name === server) return false;
762+
const other = claudeMcpToolPrefix(status.name);
763+
return other.startsWith(prefix) || prefix.startsWith(other);
764+
});
765+
}
766+
754767
function findClaudeMcpTool(
755768
statuses: ReadonlyArray<ClaudeMcpServerStatus>,
756769
toolName: string,
@@ -765,8 +778,12 @@ function findClaudeMcpTool(
765778
.filter((tool) => `${prefix}${claudeMcpToolNamePart(tool.name)}` === toolName)
766779
.map((tool) => ({ server: status.name, tool }));
767780
});
768-
// Distinct servers can normalise to one name; the CLI may have run the other one.
769-
return matches.length === 1 ? matches[0] : undefined;
781+
const [match, ...others] = matches;
782+
return match === undefined ||
783+
others.length > 0 ||
784+
claudeMcpServerPrefixOverlaps(statuses, match.server)
785+
? undefined
786+
: match;
770787
}
771788

772789
/** Claude reports `annotations` as `readOnly`/`destructive`/`openWorld`; apps expect MCP's `*Hint` names. */
@@ -7266,12 +7283,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
72667283
if (
72677284
liveQuery === null ||
72687285
liveQuery.stopping ||
7269-
liveQuery.nativeThreadId !== providerThread.nativeThreadRef?.nativeId ||
7270-
!claudeSupportsMcpApps(liveQuery.capabilities)
7286+
liveQuery.nativeThreadId !== providerThread.nativeThreadRef?.nativeId
72717287
) {
72727288
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
72737289
driver: CLAUDE_PROVIDER,
7274-
detail: "No running Claude process with MCP Apps support owns this thread.",
7290+
detail: "No running Claude process owns this thread.",
7291+
reason: "session_stopped",
7292+
});
7293+
}
7294+
if (!claudeSupportsMcpApps(liveQuery.capabilities)) {
7295+
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
7296+
driver: CLAUDE_PROVIDER,
7297+
detail: "This Claude process does not support MCP Apps.",
72757298
});
72767299
}
72777300
return liveQuery;
@@ -11715,6 +11738,16 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1171511738
}),
1171611739
callTool: Effect.fn("ClaudeAdapterV2.callMcpTool")(function* (call) {
1171711740
const liveQuery = yield* mcpAppQuery(call.providerThread);
11741+
const statuses = yield* liveQuery.query.mcpServerStatus.pipe(
11742+
Effect.mapError(mcpAppError("Failed to list Claude MCP tools.")),
11743+
);
11744+
// Servers can connect after capture, so the routing check runs again per call.
11745+
if (claudeMcpServerPrefixOverlaps(statuses, call.server)) {
11746+
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
11747+
driver: CLAUDE_PROVIDER,
11748+
detail: "Another MCP server's tool names overlap this app's server.",
11749+
});
11750+
}
1171811751
return claudeMcpCallToolResult(
1171911752
yield* liveQuery.query
1172011753
.callMcpTool({

‎packages/provider-core/src/server/ProviderAdapter.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ export class ProviderAdapterEventStreamError extends Schema.TaggedError<Provider
388388
const ProviderAdapterFailureReason = Schema.Literals([
389389
"previous_turn_active",
390390
"attachment_unreadable",
391+
"session_stopped",
391392
]);
392393
export type ProviderAdapterFailureReason = typeof ProviderAdapterFailureReason.Type;
393394

‎packages/provider-core/src/server/failure.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const CODEX_GOAL_VALIDATION_MESSAGE =
6868
const PROVIDER_FAILURE_REASONS: Record<ProviderAdapterFailureReason, string> = {
6969
previous_turn_active: "the previous turn is still running",
7070
attachment_unreadable: "an attachment could not be read",
71+
session_stopped: "the provider session has stopped",
7172
};
7273

7374
/**

0 commit comments

Comments
 (0)