Skip to content

Commit dd7918e

Browse files
fix(web,mobile): MCP app calls keep their tool row and result beside the app card (pingdotgg#33347)
1 parent fb2e5a0 commit dd7918e

11 files changed

Lines changed: 265 additions & 62 deletions

File tree

‎apps/mobile/src/lib/threadActivity.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,36 @@ it("shows a captured MCP app for a failed deferred write", () => {
5454
input: {},
5555
output: { t3McpApp: mcpApp, result: { content: [], isError: true } },
5656
};
57-
expect(buildThreadFeed([projected(item, 0)])).toMatchObject([{ type: "mcp-app", app: mcpApp }]);
57+
expect(buildThreadFeed([projected(item, 0)])).toMatchObject([
58+
{ type: "activity-group" },
59+
{ type: "mcp-app", app: mcpApp },
60+
]);
61+
});
62+
63+
it("keeps an MCP app call's tool activity beside its app card", () => {
64+
const mcpApp = {
65+
attachmentId: "repro-app-html",
66+
server: "repro",
67+
tool: "aggregate_rum_events",
68+
resourceUri: "ui://repro/dataviz",
69+
};
70+
const item: OrchestrationV2TurnItem = {
71+
...base("repro-app", "2026-08-29T00:00:00.000Z", 1),
72+
type: "dynamic_tool",
73+
toolName: "repro.aggregate_rum_events",
74+
input: { query: "@type:view" },
75+
output: {
76+
t3McpApp: mcpApp,
77+
result: { content: [{ type: "text", text: "Notice\nview\tcount\n/home\t42" }] },
78+
},
79+
};
80+
const feed = buildThreadFeed([projected(item, 0)]);
81+
expect(feed).toContainEqual(expect.objectContaining({ type: "mcp-app", app: mcpApp }));
82+
const activities = feed.flatMap((entry) =>
83+
entry.type === "activity-group" ? entry.activities : [],
84+
);
85+
expect(activities).toHaveLength(1);
86+
expect(activities[0]?.getFullDetail()).toContain("@type:view");
5887
});
5988

6089
it("keeps historical plan detail accessible from its paged turn item", () => {

‎apps/mobile/src/lib/threadActivity.ts‎

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ const projectedEntriesCache = new WeakMap<
317317
OrchestrationV2ProjectedTurnItem,
318318
{
319319
readonly attemptId: RunAttemptId | null;
320-
readonly entry: RawThreadFeedEntry;
320+
readonly entries: ReadonlyArray<RawThreadFeedEntry>;
321321
}
322322
>();
323323
const localMessageEntriesCache = new WeakMap<
@@ -2409,8 +2409,10 @@ export function buildThreadFeed(
24092409
const attemptId = resolveAttemptId(item);
24102410
const cached = projectedEntriesCache.get(row);
24112411
if (cached?.attemptId === attemptId) {
2412-
entries.push(cached.entry);
2413-
entryRows.push(index);
2412+
for (const entry of cached.entries) {
2413+
entries.push(entry);
2414+
entryRows.push(index);
2415+
}
24142416
continue;
24152417
}
24162418
const createdAt = DateTime.formatIso(item.startedAt ?? item.updatedAt);
@@ -2426,24 +2428,7 @@ export function buildThreadFeed(
24262428
runId: item.runId,
24272429
render,
24282430
};
2429-
projectedEntriesCache.set(row, { attemptId, entry });
2430-
entries.push(entry);
2431-
entryRows.push(index);
2432-
continue;
2433-
}
2434-
const app = item.type === "dynamic_tool" ? mcpAppFromToolItem(item) : undefined;
2435-
if (app) {
2436-
const entry: RawThreadFeedEntry = {
2437-
type: "mcp-app",
2438-
id: `mcp-app:${row.visibility}:${row.sourceThreadId}:${row.sourceItemId}`,
2439-
createdAt,
2440-
runId: item.runId,
2441-
sourceThreadId: row.sourceThreadId,
2442-
itemId: row.sourceItemId,
2443-
revision: turnItemDetailRevision(item),
2444-
app,
2445-
};
2446-
projectedEntriesCache.set(row, { attemptId, entry });
2431+
projectedEntriesCache.set(row, { attemptId, entries: [entry] });
24472432
entries.push(entry);
24482433
entryRows.push(index);
24492434
continue;
@@ -2480,22 +2465,34 @@ export function buildThreadFeed(
24802465
projectedItem: row,
24812466
},
24822467
};
2483-
projectedEntriesCache.set(row, { attemptId, entry });
2468+
projectedEntriesCache.set(row, { attemptId, entries: [entry] });
24842469
entries.push(entry);
24852470
entryRows.push(index);
24862471
continue;
24872472
}
24882473
const activity = toFeedActivity(row, attemptId);
2489-
const entry: RawThreadFeedEntry = {
2490-
type: "activity",
2491-
id: activity.id,
2492-
createdAt,
2493-
runId: item.runId,
2494-
activity,
2495-
};
2496-
projectedEntriesCache.set(row, { attemptId, entry });
2497-
entries.push(entry);
2498-
entryRows.push(index);
2474+
const rowEntries: RawThreadFeedEntry[] = [
2475+
{ type: "activity", id: activity.id, createdAt, runId: item.runId, activity },
2476+
];
2477+
// The card follows its tool activity: a feed restart resumes after a row's last entry.
2478+
const app = item.type === "dynamic_tool" ? mcpAppFromToolItem(item) : undefined;
2479+
if (app) {
2480+
rowEntries.push({
2481+
type: "mcp-app",
2482+
id: `mcp-app:${row.visibility}:${row.sourceThreadId}:${row.sourceItemId}`,
2483+
createdAt,
2484+
runId: item.runId,
2485+
sourceThreadId: row.sourceThreadId,
2486+
itemId: row.sourceItemId,
2487+
revision: turnItemDetailRevision(item),
2488+
app,
2489+
});
2490+
}
2491+
projectedEntriesCache.set(row, { attemptId, entries: rowEntries });
2492+
for (const entry of rowEntries) {
2493+
entries.push(entry);
2494+
entryRows.push(index);
2495+
}
24992496
}
25002497
const retainedMessageIds = new Set([
25012498
...foldedAnswerRunIdByMessageId.keys(),

‎apps/server/src/orchestration-v2/WireProjection.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,45 @@ describe("orchestration V2 wire projection", () => {
228228
output: { content: [{ type: "image", mimeType: "image/png" }] },
229229
});
230230
expect(item.output.content[0]?.source.data).toBe(data);
231+
232+
const t3McpApp = {
233+
attachmentId: "app-html",
234+
server: "repro",
235+
tool: "chart",
236+
resourceUri: "ui://repro/chart",
237+
};
238+
const text = { type: "text", text: "Rendered chart" };
239+
const appItem = {
240+
...item,
241+
toolName: "repro.chart",
242+
output: {
243+
t3McpApp,
244+
result: { content: [text, { type: "image", mimeType: "image/png", data }] },
245+
},
246+
};
247+
expect(projectTurnItemForDetail(appItem)).toMatchObject({
248+
output: { t3McpApp, result: { content: [text, { type: "image", mimeType: "image/png" }] } },
249+
});
250+
const largeOutput = projectTurnItemForDetail(appItem);
251+
expect(
252+
JSON.stringify(largeOutput.type === "dynamic_tool" ? largeOutput.output : null),
253+
).not.toContain(data);
254+
255+
const smallApp = {
256+
t3McpApp,
257+
result: { content: [text, { type: "image", mimeType: "image/png", data: "iVBORw0KGgo=" }] },
258+
};
259+
expect(projectTurnItemForDetail({ ...appItem, output: smallApp })).toMatchObject({
260+
output: smallApp,
261+
});
262+
263+
const forged = projectTurnItemForDetail({
264+
...appItem,
265+
output: { t3McpApp: { ...t3McpApp, resourceUri: data }, result: { content: [text] } },
266+
});
267+
expect(
268+
JSON.stringify(forged.type === "dynamic_tool" ? forged.output : null).length,
269+
).toBeLessThan(270_000);
231270
});
232271

233272
it("keeps image markers when the rest of a tool output is too large to send", () => {

‎apps/server/src/orchestration-v2/WireProjection.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
dedupeSubagentBodies,
1616
dedupeTurnItemBodies,
1717
} from "@t3tools/shared/orchestrationV2DedupedBodies";
18+
import { MCP_APP_OUTPUT_KEY, readMcpAppReference } from "@t3tools/shared/mcpApp";
1819
import {
1920
compactDynamicToolOutput,
2021
omitToolOutputImageData,
@@ -228,7 +229,7 @@ function boundDynamicValue(value: unknown): unknown {
228229
}
229230

230231
/** Preserve ordered image markers in truncated output so clients can load images by index. */
231-
function boundToolOutput(output: unknown): unknown {
232+
function boundToolResult(output: unknown): unknown {
232233
const omitted = omitToolOutputImageData(output);
233234
const bounded = boundDynamicValue(omitted);
234235
if (typeof bounded !== "string" || typeof omitted === "string") return bounded;
@@ -241,6 +242,21 @@ function boundToolOutput(output: unknown): unknown {
241242
];
242243
}
243244

245+
function boundToolOutput(output: unknown): unknown {
246+
if (typeof output !== "object" || output === null || !("result" in output)) {
247+
return boundToolResult(output);
248+
}
249+
const app =
250+
MCP_APP_OUTPUT_KEY in output ? readMcpAppReference(output[MCP_APP_OUTPUT_KEY]) : undefined;
251+
if (app === undefined) return boundToolResult(output);
252+
// The app receives this result, so its image data stays unless it is too large to send.
253+
const fits = boundDynamicValue(output.result) === output.result;
254+
return {
255+
[MCP_APP_OUTPUT_KEY]: app,
256+
result: fits ? output.result : boundToolResult(output.result),
257+
};
258+
}
259+
244260
export function projectTurnItemForDetail(item: OrchestrationV2TurnItem): OrchestrationV2TurnItem {
245261
switch (item.type) {
246262
case "command_execution":

‎apps/web/src/session-logic.test.ts‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ describe("HTML renders in the timeline", () => {
13911391
]);
13921392
});
13931393

1394-
it("hosts a captured MCP app in place, from any provider's tool", () => {
1394+
it("hosts a captured MCP app after its tool row, from any provider's tool", () => {
13951395
const mcpApp = {
13961396
attachmentId: "render-thread-app-html",
13971397
server: "weather",
@@ -1403,13 +1403,58 @@ describe("HTML renders in the timeline", () => {
14031403
call.type === "dynamic_tool" ? { ...call, toolName: "weather.get_weather" } : call,
14041404
);
14051405
expect(entries.find((entry) => entry.kind === "mcp-app")).toMatchObject({
1406-
id: "render",
1406+
id: "mcp-app:render",
14071407
itemId: "render",
14081408
mcpApp,
14091409
});
14101410
expect(rowsFor(entries, false)).toEqual(["message", "turn-fold", "mcp-app", "message"]);
14111411
});
14121412

1413+
it("keeps an MCP app call's tool row and output beside its app card", () => {
1414+
const mcpApp = {
1415+
attachmentId: "render-thread-app-html",
1416+
server: "repro",
1417+
tool: "aggregate_rum_events",
1418+
resourceUri: "ui://repro/dataviz",
1419+
};
1420+
const result = { content: [{ type: "text", text: "Notice\nview\tcount\n/home\t42" }] };
1421+
const call = renderCall("completed", { t3McpApp: mcpApp, result });
1422+
const entries = turn(
1423+
call.type === "dynamic_tool" ? { ...call, toolName: "repro.aggregate_rum_events" } : call,
1424+
);
1425+
expect(entries.find((entry) => entry.kind === "mcp-app")).toMatchObject({ mcpApp });
1426+
const toolRows = entries.flatMap((entry) =>
1427+
entry.kind === "work" && entry.entry.toolTitle === "repro.aggregate_rum_events"
1428+
? [entry.entry]
1429+
: [],
1430+
);
1431+
expect(toolRows).toMatchObject([{ toolData: { output: { result } } }]);
1432+
});
1433+
1434+
it("drops an MCP app card when an update removes the call's app", () => {
1435+
const mcpApp = {
1436+
attachmentId: "render-thread-app-html",
1437+
server: "repro",
1438+
tool: "aggregate_rum_events",
1439+
resourceUri: "ui://repro/dataviz",
1440+
};
1441+
const call = (output: unknown) => ({
1442+
...renderCall("completed", output),
1443+
toolName: "repro.aggregate_rum_events",
1444+
});
1445+
const result = { content: [{ type: "text", text: "rows" }] };
1446+
const input = (output: unknown) => ({
1447+
visibleTurnItems: [visible(command("before", 1)), visible(call(output))],
1448+
optimisticMessages: [],
1449+
});
1450+
const previous = deriveTimelineEntriesFromVisibleTurnItemsWithState(
1451+
input({ t3McpApp: mcpApp, result }),
1452+
);
1453+
const nextInput = input({ result });
1454+
const next = deriveTimelineEntriesFromVisibleTurnItemsWithState(nextInput, previous);
1455+
expect(next.entries).toEqual(deriveTimelineEntriesFromVisibleTurnItems(nextInput));
1456+
});
1457+
14131458
it("hosts an MCP app whose call failed, as a form-deferring write does", () => {
14141459
const mcpApp = {
14151460
attachmentId: "render-thread-app-html",

‎apps/web/src/session-logic.ts‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -652,22 +652,6 @@ export function deriveTimelineEntriesFromVisibleTurnItems(
652652
continue;
653653
}
654654

655-
const mcpApp = item.type === "dynamic_tool" ? mcpAppFromToolItem(item) : undefined;
656-
if (mcpApp !== undefined) {
657-
entries.push({
658-
id: item.id,
659-
kind: "mcp-app",
660-
createdAt,
661-
runId: item.runId,
662-
sourceThreadId: row.sourceThreadId,
663-
itemId: row.sourceItemId,
664-
revision: turnItemDetailRevision(item),
665-
mcpApp,
666-
...attemptMetadata,
667-
});
668-
continue;
669-
}
670-
671655
if (STANDALONE_V2_ITEM_TYPES.has(item.type)) {
672656
entries.push({
673657
id: item.id,
@@ -686,6 +670,21 @@ export function deriveTimelineEntriesFromVisibleTurnItems(
686670
entry: projectedWorkEntry(row),
687671
...attemptMetadata,
688672
});
673+
674+
const mcpApp = item.type === "dynamic_tool" ? mcpAppFromToolItem(item) : undefined;
675+
if (mcpApp !== undefined) {
676+
entries.push({
677+
id: `mcp-app:${item.id}`,
678+
kind: "mcp-app",
679+
createdAt,
680+
runId: item.runId,
681+
sourceThreadId: row.sourceThreadId,
682+
itemId: row.sourceItemId,
683+
revision: turnItemDetailRevision(item),
684+
mcpApp,
685+
...attemptMetadata,
686+
});
687+
}
689688
}
690689

691690
const retainedMessageIds = new Set([
@@ -870,6 +869,9 @@ function canRederiveInPlace(
870869
item.item.type !== "user_message" &&
871870
item.item.type !== "assistant_message" &&
872871
item.item.type !== "user_input_request" &&
872+
// An app call projects two entries; in-place reuse replaces one.
873+
(previousItem.item.type !== "dynamic_tool" ||
874+
mcpAppFromToolItem(previousItem.item) === undefined) &&
873875
((input.anchoredMessages?.length ?? 0) === 0 ||
874876
projectedItemCreatedAt(item) === projectedItemCreatedAt(previousItem))
875877
);

‎docs/user/html-renders.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Previews can load public resources such as CDN scripts, but nothing on your loca
1212

1313
## MCP apps
1414

15-
Some MCP servers return an interactive app with their tool results, following the [MCP Apps](https://github.com/modelcontextprotocol/ext-apps) standard. When an agent calls one of those tools, the app appears in the thread in place of the tool call, on web, desktop, and mobile. Codex and Claude support this today, for any MCP server you have configured for them. Claude needs Claude Code 2.1.301 or newer; older versions and other providers show these calls as ordinary tool calls.
15+
Some MCP servers return an interactive app with their tool results, following the [MCP Apps](https://github.com/modelcontextprotocol/ext-apps) standard. When an agent calls one of those tools, the app appears in the thread below the tool call, on web, desktop, and mobile. The tool call keeps its input and result, so you can still read them when an app shows nothing. Codex and Claude support this today, for any MCP server you have configured for them. Claude needs Claude Code 2.1.301 or newer; older versions and other providers show these calls as ordinary tool calls.
1616

1717
Apps follow your theme. An app can call tools on its own server and post a message to the thread; T3 Code asks first unless the server marks the tool as read-only, and it asks before every message. On mobile, an app also asks before opening a link, and T3 Code asks before saving a file an app offers. An app stays viewable after its agent stops, but using it needs the agent of the thread that created it running, so send a message in that thread first if the app says it is unavailable. An app that navigates away from its own page is stopped.
1818

0 commit comments

Comments
 (0)