Skip to content

Commit a1621a8

Browse files
fix(server): show MCP App forms from failed tool results (#25225)
Co-authored-by: shivam <[email protected]>
1 parent 3bd64cf commit a1621a8

6 files changed

Lines changed: 112 additions & 10 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ const threadId = ThreadId.make("thread-1");
3838
const sourceThreadId = ThreadId.make("thread-source");
3939
const runId = RunId.make("run-1");
4040

41+
it("shows a captured MCP app for a failed deferred write", () => {
42+
const mcpApp = {
43+
attachmentId: "deferred-app-html",
44+
server: "github",
45+
tool: "create_pull_request",
46+
resourceUri: "ui://github-mcp-server/pr-create",
47+
};
48+
const item: OrchestrationV2TurnItem = {
49+
...base("deferred-app", "2026-08-29T00:00:00.000Z", 1),
50+
type: "dynamic_tool",
51+
toolName: "github.create_pull_request",
52+
status: "failed",
53+
input: {},
54+
output: { t3McpApp: mcpApp, result: { content: [], isError: true } },
55+
};
56+
expect(buildThreadFeed([projected(item, 0)])).toMatchObject([{ type: "mcp-app", app: mcpApp }]);
57+
});
58+
4159
it("keeps historical plan detail accessible from its paged turn item", () => {
4260
const item = {
4361
...base("historical-plan", "2026-08-29T00:00:00.000Z", 1),

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,10 +2102,7 @@ export function buildThreadFeed(
21022102
entryRows.push(index);
21032103
continue;
21042104
}
2105-
const app =
2106-
item.type === "dynamic_tool" && item.status === "completed"
2107-
? mcpAppFromToolItem(item)
2108-
: undefined;
2105+
const app = item.type === "dynamic_tool" ? mcpAppFromToolItem(item) : undefined;
21092106
if (app) {
21102107
const entry: RawThreadFeedEntry = {
21112108
type: "mcp-app",

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5138,6 +5138,73 @@ describe("CodexAdapterV2 post-settle continuation", () => {
51385138
),
51395139
);
51405140

5141+
const MCP_APP_DEFERRED_RESULT = {
5142+
content: [{ type: "text", text: "An interactive form is being shown to the user." }],
5143+
structuredContent: { status: "awaiting_user_submission" },
5144+
};
5145+
const mcpAppDeferredTranscript = makeCodexReplayTranscript({
5146+
scenario: MCP_APP_SCENARIO,
5147+
entries: mcpAppTranscript.entries.map((entry) =>
5148+
entry.type === "emit_inbound" && entry.label === "item/completed/app-tool"
5149+
? {
5150+
...entry,
5151+
frame: {
5152+
method: "item/completed",
5153+
params: {
5154+
item: {
5155+
...mcpAppToolItem("inProgress"),
5156+
status: "failed",
5157+
result: MCP_APP_DEFERRED_RESULT,
5158+
},
5159+
threadId: MCP_APP_NATIVE_THREAD,
5160+
turnId: MCP_APP_NATIVE_TURN,
5161+
completedAtMs: 1782622441500,
5162+
},
5163+
},
5164+
}
5165+
: entry,
5166+
),
5167+
});
5168+
5169+
it.effect("captures the app of a tool call that defers to its form", () =>
5170+
Effect.scoped(
5171+
Effect.gen(function* () {
5172+
const appCaptured = yield* Deferred.make<OrchestrationV2TurnItem>();
5173+
const harness = yield* makeCodexReplayHarness(mcpAppDeferredTranscript, (event) =>
5174+
event.type === "turn_item.updated" &&
5175+
event.turnItem.type === "dynamic_tool" &&
5176+
event.turnItem.status === "failed"
5177+
? Deferred.succeed(appCaptured, event.turnItem)
5178+
: Effect.void,
5179+
);
5180+
const now = yield* DateTime.now;
5181+
5182+
yield* harness.runtime.startTurn(
5183+
makeCodexTestTurnInput({
5184+
threadId: harness.threadId,
5185+
providerThread: harness.providerThread,
5186+
now,
5187+
attemptId: RunAttemptId.make("attempt-codex-mcp-app-deferred"),
5188+
text: MCP_APP_PROMPT,
5189+
}),
5190+
);
5191+
const item = yield* Deferred.await(appCaptured);
5192+
5193+
const output = item.type === "dynamic_tool" ? (item.output as Record<string, unknown>) : {};
5194+
assert.equal(
5195+
readMcpAppReference(output[MCP_APP_OUTPUT_KEY])?.resourceUri,
5196+
MCP_APP_RESOURCE,
5197+
);
5198+
assert.deepEqual(output.result, { ...MCP_APP_DEFERRED_RESULT, isError: true });
5199+
assert.isFalse(yield* harness.hasPendingBackgroundWork);
5200+
}).pipe(
5201+
Effect.provide(
5202+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
5203+
),
5204+
),
5205+
),
5206+
);
5207+
51415208
const ORPHAN_WAIT_SCENARIO = "codex-orphaned-dynamic-tool";
51425209
const ORPHAN_WAIT_NATIVE_THREAD = "native-codex-orphan-wait-thread";
51435210
const ORPHAN_WAIT_NATIVE_TURN = "native-codex-orphan-wait-turn";

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3744,7 +3744,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
37443744
Effect.uninterruptibleMask((restore) =>
37453745
Effect.gen(function* () {
37463746
const reference =
3747-
item.status === "completed" && item.result != null && item.error == null
3747+
item.result != null && item.error == null
37483748
? yield* restore(
37493749
getNativeThreadId(context.providerThread).pipe(
37503750
Effect.flatMap((threadId) =>
@@ -3798,7 +3798,13 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
37983798
? artifacts.turnItem
37993799
: {
38003800
...artifacts.turnItem,
3801-
output: { [MCP_APP_OUTPUT_KEY]: reference, result: item.result },
3801+
output: {
3802+
[MCP_APP_OUTPUT_KEY]: reference,
3803+
result:
3804+
item.status === "failed"
3805+
? { ...item.result, isError: true }
3806+
: item.result,
3807+
},
38023808
};
38033809
yield* emitProviderEvent({
38043810
type: "node.updated",

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,23 @@ describe("HTML renders in the timeline", () => {
13071307
expect(rowsFor(entries, false)).toEqual(["message", "turn-fold", "mcp-app", "message"]);
13081308
});
13091309

1310+
it("hosts an MCP app whose call failed, as a form-deferring write does", () => {
1311+
const mcpApp = {
1312+
attachmentId: "render-thread-app-html",
1313+
server: "github",
1314+
tool: "create_pull_request",
1315+
resourceUri: "ui://github-mcp-server/pr-create",
1316+
};
1317+
const call = renderCall("failed", {
1318+
t3McpApp: mcpApp,
1319+
result: { content: [], isError: true },
1320+
});
1321+
const entries = turn(
1322+
call.type === "dynamic_tool" ? { ...call, toolName: "github.create_pull_request" } : call,
1323+
);
1324+
expect(entries.find((entry) => entry.kind === "mcp-app")).toMatchObject({ mcpApp });
1325+
});
1326+
13101327
it("keeps a render visible when its superseded attempt folds", () => {
13111328
const attempt: OrchestrationV2RunAttempt = {
13121329
id: RunAttemptId.make("attempt-superseded"),

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -711,10 +711,7 @@ export function deriveTimelineEntriesFromVisibleTurnItems(
711711
continue;
712712
}
713713

714-
const mcpApp =
715-
item.type === "dynamic_tool" && item.status === "completed"
716-
? mcpAppFromToolItem(item)
717-
: undefined;
714+
const mcpApp = item.type === "dynamic_tool" ? mcpAppFromToolItem(item) : undefined;
718715
if (mcpApp !== undefined) {
719716
entries.push({
720717
id: item.id,

0 commit comments

Comments
 (0)