Skip to content

Commit ec20db4

Browse files
juliusmarmingeBil0000claude
authored
fix(server): share MCP tool presentation across providers (pingdotgg#15475)
Co-authored-by: Bil0000 <[email protected]> Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 745c225 commit ec20db4

19 files changed

Lines changed: 958 additions & 107 deletions

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

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,9 +1008,12 @@ describe("AcpAdapterV2", () => {
10081008
);
10091009
type Runtime = AcpSessionRuntime.AcpSessionRuntime["Service"];
10101010
let handler: Parameters<Runtime["handleSessionUpdate"]>[0] | undefined;
1011+
let createTerminal: Parameters<Runtime["handleCreateTerminal"]>[0] | undefined;
10111012
const instanceId = ProviderInstanceId.make("devin-replay");
10121013
const adapter = makeAcpAdapterV2({
10131014
instanceId,
1015+
// Production Devin runs commands through client terminals.
1016+
clientTerminals: { childProcessSpawner, shellCommands: true },
10141017
crypto: yield* Crypto.Crypto,
10151018
fileSystem: yield* FileSystem.FileSystem,
10161019
idAllocator,
@@ -1031,9 +1034,18 @@ describe("AcpAdapterV2", () => {
10311034
Effect.sync(() => {
10321035
handler = next;
10331036
}).pipe(Effect.andThen(runtime.handleSessionUpdate(next))),
1037+
handleCreateTerminal: (next) =>
1038+
Effect.sync(() => {
1039+
createTerminal = next;
1040+
}).pipe(Effect.andThen(runtime.handleCreateTerminal(next))),
10341041
prompt: () =>
10351042
Effect.gen(function* () {
10361043
assert.isDefined(handler);
1044+
assert.isDefined(createTerminal);
1045+
const fallbackTerminal = yield* createTerminal(
1046+
{ sessionId: "mock-session-1", command: "true acp-mcp-call task_status {}" },
1047+
{ requestId: "child-mcp-terminal", method: "terminal/create" },
1048+
);
10371049
// Production thread 54aeb6d7 split after "(command". Metadata shapes
10381050
// below were captured from live Devin sessions showy-mile/fragrant-chamomile.
10391051
const updates = [
@@ -1075,6 +1087,48 @@ describe("AcpAdapterV2", () => {
10751087
"cognition.ai/subagent_context": { parentAgentId: "child-a" },
10761088
},
10771089
},
1090+
{
1091+
sessionUpdate: "tool_call",
1092+
toolCallId: "child-weather",
1093+
title: "Check weather",
1094+
status: "completed",
1095+
rawInput: { server: "weather", tool: "get_weather", city: "Berlin" },
1096+
rawOutput: {
1097+
result: {
1098+
_meta: {
1099+
source: { name: "Weather", logoUrl: "https://example.com/weather.png" },
1100+
},
1101+
content: [{ type: "text", text: "Sunny" }],
1102+
},
1103+
},
1104+
_meta: {
1105+
is_mcp_tool_call: true,
1106+
"cognition.ai/subagent_context": { parentAgentId: "child-a" },
1107+
},
1108+
},
1109+
{
1110+
sessionUpdate: "tool_call",
1111+
toolCallId: "child-mcp-fallback",
1112+
title: "Ran command",
1113+
kind: "execute",
1114+
status: "completed",
1115+
content: [{ type: "terminal", terminalId: fallbackTerminal.terminalId }],
1116+
_meta: { "cognition.ai/subagent_context": { parentAgentId: "child-a" } },
1117+
},
1118+
{
1119+
sessionUpdate: "tool_call",
1120+
toolCallId: "parent-weather",
1121+
title: "Check weather",
1122+
status: "completed",
1123+
rawInput: { server: "weather", tool: "get_weather", city: "Berlin" },
1124+
rawOutput: {
1125+
_meta: {
1126+
source: { name: "Weather", logoUrl: "https://example.com/weather.png" },
1127+
},
1128+
content: [{ type: "text", text: "Sunny" }],
1129+
},
1130+
_meta: { is_mcp_tool_call: true },
1131+
},
10781132
{
10791133
sessionUpdate: "agent_message_chunk",
10801134
content: { type: "text", text: " → decider → event)." },
@@ -1192,6 +1246,37 @@ describe("AcpAdapterV2", () => {
11921246
);
11931247
assert.deepEqual([...childMessages.values()], ["Checking the code.", "ONE"]);
11941248
assert.equal(task?.prompt, "Run pwd, then reply ONE.");
1249+
// Terminal-fallback MCP calls in a child session keep their T3 identity.
1250+
assert.isTrue(
1251+
items.some(
1252+
(item) =>
1253+
item.threadId === task?.childThreadId &&
1254+
item.type === "dynamic_tool" &&
1255+
item.toolName === "t3-code.task_status",
1256+
),
1257+
);
1258+
const childMcp = items.find(
1259+
(item) =>
1260+
item.threadId === task?.childThreadId &&
1261+
item.type === "dynamic_tool" &&
1262+
item.toolName === "weather.get_weather",
1263+
);
1264+
const parentMcp = items.find(
1265+
(item) =>
1266+
item.threadId === threadId &&
1267+
item.type === "dynamic_tool" &&
1268+
item.toolName === "weather.get_weather",
1269+
);
1270+
for (const item of [parentMcp, childMcp]) {
1271+
assert.equal(item?.title, "get weather");
1272+
assert.deepEqual(item?.toolSource, {
1273+
key: "mcp:weather",
1274+
name: "Weather",
1275+
kind: "integration",
1276+
icon: { _tag: "themed-logo", logoUrl: "https://example.com/weather.png" },
1277+
});
1278+
assert.deepEqual(item?.toolIcon, item?.toolSource?.icon);
1279+
}
11951280
assert.isTrue(
11961281
items.some(
11971282
(item) =>
@@ -1216,7 +1301,7 @@ describe("AcpAdapterV2", () => {
12161301
const parentTools = items.filter(
12171302
(item) => item.threadId === threadId && item.type === "dynamic_tool",
12181303
);
1219-
assert.equal(parentTools.length, 1);
1304+
assert.equal(parentTools.length, 2);
12201305
assert.equal(parentTools[0]?.title, "Parent tool finished");
12211306
}).pipe(Effect.provide(testLayer), Effect.scoped),
12221307
);

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

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import {
6363
makeAcpMcpOverAcpBridge,
6464
type AcpMcpOverAcpBridge,
6565
} from "../../mcp/AcpMcpOverAcpBridge.ts";
66+
import { mcpToolPresentation } from "../../provider/McpToolPresentation.ts";
6667
import * as McpProviderSession from "../../mcp/McpProviderSession.ts";
6768
import {
6869
applyAcpAgentTerminalUpdate,
@@ -1632,6 +1633,21 @@ export function makeAcpAdapterV2(
16321633
embeddedTerminalsByToolCallId.delete(oldest);
16331634
}
16341635
};
1636+
// Command lines of the terminals embedded in a tool call, so MCP calls
1637+
// made through the acp-mcp-call terminal fallback keep their identity.
1638+
const embeddedTerminalCommands = (
1639+
sessionId: string,
1640+
toolCallId: string,
1641+
): ReadonlyArray<string> =>
1642+
(
1643+
embeddedTerminalsByToolCallId.get(sessionScopedId(sessionId, toolCallId))
1644+
?.terminalIds ?? []
1645+
).flatMap((terminalId) => {
1646+
const command =
1647+
clientTerminals?.readCommandLine(terminalId) ??
1648+
agentTerminalsById.get(sessionScopedId(sessionId, terminalId))?.command;
1649+
return command === undefined ? [] : [command];
1650+
});
16351651
// Client terminals (Devin) run with the T3 server's privileges, so they
16361652
// are policy-checked against the active turn policy; a command the user
16371653
// already approved satisfies an "ask" disposition.
@@ -3267,17 +3283,10 @@ export function makeAcpAdapterV2(
32673283
// agent-specific shape and project the same branded dynamic_tool
32683284
// item native providers produce (e.g. the T3 orchestration tools).
32693285
const mcpIdentity = extractMcpToolCallIdentity(toolCall, {
3270-
embeddedTerminalCommands: (
3271-
embeddedTerminalsByToolCallId.get(
3272-
sessionScopedId(context.nativeThreadId, toolCall.toolCallId),
3273-
)?.terminalIds ?? []
3274-
).flatMap((terminalId) => {
3275-
const command =
3276-
clientTerminals?.readCommandLine(terminalId) ??
3277-
agentTerminalsById.get(sessionScopedId(context.nativeThreadId, terminalId))
3278-
?.command;
3279-
return command === undefined ? [] : [command];
3280-
}),
3286+
embeddedTerminalCommands: embeddedTerminalCommands(
3287+
context.nativeThreadId,
3288+
toolCall.toolCallId,
3289+
),
32813290
});
32823291
let turnItem: OrchestrationV2TurnItem;
32833292
if (toolCall.toolCallId.startsWith("acp-compaction:")) {
@@ -3302,9 +3311,14 @@ export function makeAcpAdapterV2(
33023311
} else if (mcpIdentity !== undefined) {
33033312
turnItem = {
33043313
...base,
3305-
// Identity lives in toolName, like native Codex MCP items; the
3306-
// agent's own title (e.g. "Ran command") would shadow it.
33073314
title: null,
3315+
...mcpToolPresentation({
3316+
serverName: mcpIdentity.server,
3317+
toolName: mcpIdentity.tool,
3318+
source: unknownRecord(
3319+
(unknownRecord(rawOutputRecord?.result) ?? rawOutputRecord)?._meta,
3320+
)?.source,
3321+
}),
33083322
type: "dynamic_tool",
33093323
toolName: `${mcpIdentity.server}.${mcpIdentity.tool}`,
33103324
input:
@@ -4328,7 +4342,8 @@ export function makeAcpAdapterV2(
43284342
return;
43294343
}
43304344
if (context.finalized) return;
4331-
if (notification.sessionId !== (yield* Ref.get(activeSessionId))) {
4345+
const rootSessionId = yield* Ref.get(activeSessionId);
4346+
if (notification.sessionId !== rootSessionId) {
43324347
// Finalize may have completed during the activeSessionId yield.
43334348
if (context.finalized) return;
43344349
if (flavor.extractSubagentUpdate === undefined) return;
@@ -4356,6 +4371,17 @@ export function makeAcpAdapterV2(
43564371
const key = `${nativeTaskId}:tool:${toolCall.toolCallId}`;
43574372
const merged = mergeToolCallState(context.tools.get(key), toolCall);
43584373
context.tools.set(key, merged);
4374+
// Terminals are remembered under the raw session id: the child's
4375+
// own session, or the root one when the flavor routes child
4376+
// updates out of it (Devin).
4377+
const mcpIdentity = extractMcpToolCallIdentity(merged, {
4378+
embeddedTerminalCommands: [
4379+
...embeddedTerminalCommands(notification.sessionId, toolCall.toolCallId),
4380+
...(rootSessionId === null
4381+
? []
4382+
: embeddedTerminalCommands(rootSessionId, toolCall.toolCallId)),
4383+
],
4384+
});
43594385
const now = yield* DateTime.now;
43604386
const status = toolStatus(merged.status);
43614387
const startedAt = context.toolStartedAt.get(key) ?? now;
@@ -4380,7 +4406,22 @@ export function makeAcpAdapterV2(
43804406
completedAt: completedAtForStatus(status, now),
43814407
updatedAt: now,
43824408
type: "dynamic_tool",
4383-
toolName: merged.title ?? merged.kind ?? "Tool",
4409+
...(mcpIdentity === undefined
4410+
? {}
4411+
: mcpToolPresentation({
4412+
serverName: mcpIdentity.server,
4413+
toolName: mcpIdentity.tool,
4414+
source: unknownRecord(
4415+
(
4416+
unknownRecord(unknownRecord(merged.data.rawOutput)?.result) ??
4417+
unknownRecord(merged.data.rawOutput)
4418+
)?._meta,
4419+
)?.source,
4420+
})),
4421+
toolName:
4422+
mcpIdentity === undefined
4423+
? (merged.title ?? merged.kind ?? "Tool")
4424+
: `${mcpIdentity.server}.${mcpIdentity.tool}`,
43844425
input: merged.data.rawInput ?? null,
43854426
output: merged.data.rawOutput ?? merged.data.content ?? null,
43864427
},

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

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,111 @@ describe("ClaudeAdapterV2 background wake turns", () => {
21952195
});
21962196
const makeWakeHarness = makeWakeHarnessWithOptions();
21972197

2198+
it.effect.each([
2199+
{ isError: false, title: "Check weather" },
2200+
{ isError: true, title: "Check weather" },
2201+
{ isError: false, title: undefined },
2202+
])("keeps late MCP display metadata with %j", ({ isError, title }) =>
2203+
Effect.gen(function* () {
2204+
const harness = yield* makeWakeHarness;
2205+
yield* harness.runtime.startTurn(
2206+
makeClaudeTestTurnInput({
2207+
threadId: harness.threadId,
2208+
providerThread: harness.providerThread,
2209+
now: yield* DateTime.now,
2210+
attemptId: RunAttemptId.make("mcp-presentation"),
2211+
text: "Check the weather",
2212+
attachments: [],
2213+
}),
2214+
);
2215+
const toolName = "mcp__weather__get_weather";
2216+
const id = "weather-call";
2217+
yield* Effect.promise(() =>
2218+
harness.getOpenedOptions()!.canUseTool!(
2219+
toolName,
2220+
{ city: "Berlin" },
2221+
{
2222+
signal: new AbortController().signal,
2223+
toolUseID: id,
2224+
requestId: "weather-request",
2225+
},
2226+
),
2227+
);
2228+
yield* harness.offerAndWait(
2229+
claudeSdkFrame({
2230+
type: "assistant",
2231+
uuid: "weather-assistant",
2232+
session_id: WAKE_NATIVE_SESSION,
2233+
parent_tool_use_id: null,
2234+
message: {
2235+
id: "weather-message",
2236+
type: "message",
2237+
role: "assistant",
2238+
model: "claude-sonnet-4-6",
2239+
content: [{ type: "tool_use", id, name: toolName, input: { city: "Berlin" } }],
2240+
},
2241+
tool_use_meta: [
2242+
{
2243+
id,
2244+
display_name: title,
2245+
server_display_name: "Weather",
2246+
icon_url: "https://example.com/weather.png",
2247+
},
2248+
],
2249+
}),
2250+
);
2251+
yield* harness.offerAndWait(
2252+
claudeSdkFrame({
2253+
type: "user",
2254+
uuid: "weather-result",
2255+
session_id: WAKE_NATIVE_SESSION,
2256+
parent_tool_use_id: null,
2257+
message: {
2258+
role: "user",
2259+
content: [
2260+
{
2261+
type: "tool_result",
2262+
tool_use_id: id,
2263+
content: "Weather result",
2264+
is_error: isError,
2265+
},
2266+
],
2267+
},
2268+
}),
2269+
);
2270+
yield* Queue.offer(
2271+
harness.sdkMessages,
2272+
makeResultFrame({ uuid: "weather-terminal", result: "Weather checked" }),
2273+
);
2274+
yield* Queue.take(harness.terminalReceipts);
2275+
const items = harness.events.flatMap((event) =>
2276+
event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool"
2277+
? [event.turnItem]
2278+
: [],
2279+
);
2280+
assert.deepEqual(
2281+
items.map((item) => item.status),
2282+
["running", "running", isError ? "failed" : "completed"],
2283+
);
2284+
assert.isNull(items[0]?.title);
2285+
for (const item of items.slice(1)) {
2286+
assert.equal(item.title, title ?? "get weather");
2287+
assert.deepEqual(item.toolIcon, {
2288+
_tag: "themed-logo",
2289+
logoUrl: "https://example.com/weather.png",
2290+
});
2291+
assert.deepEqual(item.toolSource, {
2292+
key: "mcp:weather",
2293+
name: "Weather",
2294+
kind: "integration",
2295+
icon: { _tag: "themed-logo", logoUrl: "https://example.com/weather.png" },
2296+
});
2297+
assert.deepEqual(item.input, { city: "Berlin" });
2298+
}
2299+
assert.equal(new Set(items.map((item) => item.id)).size, 1);
2300+
}).pipe(Effect.scoped, Effect.provide(Layer.merge(IdAllocator.layer, NodeServices.layer))),
2301+
);
2302+
21982303
it.effect(
21992304
"reuses a background shell's query for omitted and explicit Normal, but blocks Fast",
22002305
() =>

0 commit comments

Comments
 (0)