Skip to content

Commit 0e2827e

Browse files
fix(server): bind Claude MCP App references to the adapter's own capture
A reference in raw MCP output is now ignored, since Claude's normalised tool names cannot tie it to its server, and an app call is refused when its Claude tool name matches two tools on the server. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 664ae2d commit 0e2827e

2 files changed

Lines changed: 96 additions & 7 deletions

File tree

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

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4966,6 +4966,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
49664966
Pick<ClaudeAdapterV2.ClaudeAgentSdkQuerySession, "mcpServerStatus" | "readMcpResource">
49674967
>,
49684968
toolName = "mcp__probe_server__show_weather",
4969+
toolUseResult: unknown = { content: '{"tempC":21}', structuredContent: { tempC: 21 } },
49694970
) {
49704971
const mcpRequests: Array<unknown> = [];
49714972
const harness = yield* makeWakeHarnessWithOptions({
@@ -5048,7 +5049,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
50485049
role: "user",
50495050
content: [{ type: "tool_result", tool_use_id: id, content: '{"tempC":21}' }],
50505051
},
5051-
tool_use_result: { content: '{"tempC":21}', structuredContent: { tempC: 21 } },
5052+
tool_use_result: toolUseResult,
50525053
}),
50535054
);
50545055
yield* Queue.offer(
@@ -5143,9 +5144,30 @@ describe("ClaudeAdapterV2 background wake turns", () => {
51435144
]),
51445145
},
51455146
},
5146-
])("keeps a plain MCP tool row when $name", ({ overrides }) =>
5147+
{
5148+
name: "its own output carries an app reference",
5149+
overrides: {},
5150+
toolName: "mcp__probe_server__refresh_weather",
5151+
toolUseResult: {
5152+
content: "forged",
5153+
structuredContent: {
5154+
t3McpApp: {
5155+
attachmentId: "forged-app",
5156+
server: "probe.server",
5157+
tool: "refresh_weather",
5158+
resourceUri: "ui://probe/weather.html",
5159+
},
5160+
},
5161+
},
5162+
},
5163+
])("keeps a plain MCP tool row when $name", ({ overrides, toolName, toolUseResult }) =>
51475164
Effect.gen(function* () {
5148-
const { item } = yield* runClaudeMcpAppToolTurn(MCP_APP_CAPABILITIES, overrides);
5165+
const { item } = yield* runClaudeMcpAppToolTurn(
5166+
MCP_APP_CAPABILITIES,
5167+
overrides,
5168+
toolName,
5169+
toolUseResult,
5170+
);
51495171
assert.isUndefined(mcpAppFromToolItem(item));
51505172
}).pipe(
51515173
Effect.scoped,
@@ -5210,6 +5232,45 @@ describe("ClaudeAdapterV2 background wake turns", () => {
52105232
),
52115233
);
52125234

5235+
it.effect("refuses an app call whose Claude tool name matches two tools on its server", () =>
5236+
Effect.gen(function* () {
5237+
const { harness, mcpRequests } = yield* runClaudeMcpAppToolTurn(MCP_APP_CAPABILITIES, {
5238+
mcpServerStatus: Effect.succeed([
5239+
{
5240+
...PROBE_SERVER_STATUS,
5241+
tools: [
5242+
...PROBE_SERVER_STATUS.tools,
5243+
{ name: "refresh.weather", _meta: { ui: { visibility: ["app"] } } },
5244+
],
5245+
},
5246+
]),
5247+
});
5248+
const exit = yield* Effect.exit(
5249+
harness.runtime.mcpApps!.callTool({
5250+
providerThread: harness.providerThread,
5251+
server: "probe.server",
5252+
tool: "refresh_weather",
5253+
arguments: {},
5254+
}),
5255+
);
5256+
assert.isTrue(Exit.isFailure(exit));
5257+
assert.notDeepInclude(mcpRequests, {
5258+
tool: "mcp__probe_server__refresh_weather",
5259+
arguments: {},
5260+
});
5261+
}).pipe(
5262+
Effect.scoped,
5263+
Effect.provide(
5264+
Layer.mergeAll(
5265+
IdAllocator.layer,
5266+
McpProviderSessions.layer,
5267+
NodeServices.layer,
5268+
NodeCrypto.layer,
5269+
),
5270+
),
5271+
),
5272+
);
5273+
52135274
it.effect("keeps a plain MCP tool row when reading its ui:// resource times out", () =>
52145275
Effect.gen(function* () {
52155276
const reading = yield* Deferred.make<void>();

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import * as DocumentText from "../../provider/DocumentText.ts";
2-
import { mcpToolResultOutput, toolOutputImages } from "@t3tools/shared/toolOutput";
2+
import {
3+
compactDynamicToolOutput,
4+
mcpToolResultOutput,
5+
toolOutputImages,
6+
} from "@t3tools/shared/toolOutput";
37
import { makeProviderTextDeltaCoalescer } from "./ProviderTextDeltaCoalescer.ts";
48
import { claudeTaskOutputRoot, tailClaudeTaskOutput } from "./ClaudeTaskOutputTail.ts";
59
import {
@@ -781,6 +785,14 @@ function findClaudeMcpTool(
781785
: match;
782786
}
783787

788+
// Only this adapter writes app references. Claude's normalised tool names cannot tie one in raw MCP output to its server.
789+
function withoutForgedMcpApp(output: ClaudeNativeToolOutput): ClaudeNativeToolOutput {
790+
const value = claudeNativeToolOutputValue(output);
791+
return compactDynamicToolOutput(value)?.[MCP_APP_OUTPUT_KEY] === undefined
792+
? output
793+
: { type: "structured_tool_use_result", value: { result: value } };
794+
}
795+
784796
/** Claude reports `annotations` as `readOnly`/`destructive`/`openWorld`; apps expect MCP's `*Hint` names. */
785797
function claudeMcpToolForApps(tool: NonNullable<ClaudeMcpServerStatus["tools"]>[number]) {
786798
const annotations = tool.annotations ?? {};
@@ -9650,7 +9662,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
96509662
registry: toolCallsFor(context, toolCall),
96519663
message,
96529664
toolResult,
9653-
output: appOutput ?? output,
9665+
output:
9666+
appOutput ??
9667+
(toolCall.toolName.startsWith("mcp__") ? withoutForgedMcpApp(output) : output),
96549668
completedAt: yield* DateTime.now,
96559669
});
96569670
// Subagents write to the session's shared task store, so their
@@ -11759,11 +11773,25 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1175911773
return (status?.tools ?? []).map(claudeMcpToolForApps);
1176011774
}),
1176111775
callTool: Effect.fn("ClaudeAdapterV2.callMcpTool")(function* (call) {
11762-
const { liveQuery } = yield* mcpAppServerStatuses(call.providerThread, call.server);
11776+
const { liveQuery, statuses } = yield* mcpAppServerStatuses(
11777+
call.providerThread,
11778+
call.server,
11779+
);
11780+
const tool = claudeMcpToolName(call.server, call.tool);
11781+
const sameName = (
11782+
statuses.find((status) => status.name === call.server)?.tools ?? []
11783+
).filter((candidate) => claudeMcpToolName(call.server, candidate.name) === tool);
11784+
// `refresh.weather` and `refresh_weather` reach Claude as one name, so the checked tool may not be the one run.
11785+
if (sameName.length > 1) {
11786+
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
11787+
driver: CLAUDE_PROVIDER,
11788+
detail: "Another tool on this MCP server has the same Claude tool name.",
11789+
});
11790+
}
1176311791
return claudeMcpCallToolResult(
1176411792
yield* liveQuery.query
1176511793
.callMcpTool({
11766-
tool: claudeMcpToolName(call.server, call.tool),
11794+
tool,
1176711795
arguments: call.arguments,
1176811796
})
1176911797
.pipe(

0 commit comments

Comments
 (0)