Skip to content
TypeScript

fix(server): show MCP App forms from failed tool results (#25225)

mariaa1621a81af72
diff --git a/apps/mobile/src/lib/threadActivity.test.ts b/apps/mobile/src/lib/threadActivity.test.ts
index f2ced2d6897..96626a5caef 100644
--- a/apps/mobile/src/lib/threadActivity.test.ts
+++ b/apps/mobile/src/lib/threadActivity.test.ts
@@ -38,6 +38,24 @@ const threadId = ThreadId.make("thread-1");
const sourceThreadId = ThreadId.make("thread-source");
const runId = RunId.make("run-1");
+it("shows a captured MCP app for a failed deferred write", () => {
+ const mcpApp = {
+ attachmentId: "deferred-app-html",
+ server: "github",
+ tool: "create_pull_request",
+ resourceUri: "ui://github-mcp-server/pr-create",
+ };
+ const item: OrchestrationV2TurnItem = {
+ ...base("deferred-app", "2026-08-29T00:00:00.000Z", 1),
+ type: "dynamic_tool",
+ toolName: "github.create_pull_request",
+ status: "failed",
+ input: {},
+ output: { t3McpApp: mcpApp, result: { content: [], isError: true } },
+ };
+ expect(buildThreadFeed([projected(item, 0)])).toMatchObject([{ type: "mcp-app", app: mcpApp }]);
+});
+
it("keeps historical plan detail accessible from its paged turn item", () => {
const item = {
...base("historical-plan", "2026-08-29T00:00:00.000Z", 1),
diff --git a/apps/mobile/src/lib/threadActivity.ts b/apps/mobile/src/lib/threadActivity.ts
index 4cc5a842c21..bd7af44b2fa 100644
--- a/apps/mobile/src/lib/threadActivity.ts
+++ b/apps/mobile/src/lib/threadActivity.ts
@@ -2102,10 +2102,7 @@ export function buildThreadFeed(
entryRows.push(index);
continue;
}
- const app =
- item.type === "dynamic_tool" && item.status === "completed"
- ? mcpAppFromToolItem(item)
- : undefined;
+ const app = item.type === "dynamic_tool" ? mcpAppFromToolItem(item) : undefined;
if (app) {
const entry: RawThreadFeedEntry = {
type: "mcp-app",
diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
index 19eb2a1e239..c20994423f3 100644
--- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
@@ -5138,6 +5138,73 @@ describe("CodexAdapterV2 post-settle continuation", () => {
),
);
+ const MCP_APP_DEFERRED_RESULT = {
+ content: [{ type: "text", text: "An interactive form is being shown to the user." }],
+ structuredContent: { status: "awaiting_user_submission" },
+ };
+ const mcpAppDeferredTranscript = makeCodexReplayTranscript({
+ scenario: MCP_APP_SCENARIO,
+ entries: mcpAppTranscript.entries.map((entry) =>
+ entry.type === "emit_inbound" && entry.label === "item/completed/app-tool"
+ ? {
+ ...entry,
+ frame: {
+ method: "item/completed",
+ params: {
+ item: {
+ ...mcpAppToolItem("inProgress"),
+ status: "failed",
+ result: MCP_APP_DEFERRED_RESULT,
+ },
+ threadId: MCP_APP_NATIVE_THREAD,
+ turnId: MCP_APP_NATIVE_TURN,
+ completedAtMs: 1782622441500,
+ },
+ },
+ }
+ : entry,
+ ),
+ });
+
+ it.effect("captures the app of a tool call that defers to its form", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const appCaptured = yield* Deferred.make<OrchestrationV2TurnItem>();
+ const harness = yield* makeCodexReplayHarness(mcpAppDeferredTranscript, (event) =>
+ event.type === "turn_item.updated" &&
+ event.turnItem.type === "dynamic_tool" &&
+ event.turnItem.status === "failed"
+ ? Deferred.succeed(appCaptured, event.turnItem)
+ : Effect.void,
+ );
+ const now = yield* DateTime.now;
+
+ yield* harness.runtime.startTurn(
+ makeCodexTestTurnInput({
+ threadId: harness.threadId,
+ providerThread: harness.providerThread,
+ now,
+ attemptId: RunAttemptId.make("attempt-codex-mcp-app-deferred"),
+ text: MCP_APP_PROMPT,
+ }),
+ );
+ const item = yield* Deferred.await(appCaptured);
+
+ const output = item.type === "dynamic_tool" ? (item.output as Record<string, unknown>) : {};
+ assert.equal(
+ readMcpAppReference(output[MCP_APP_OUTPUT_KEY])?.resourceUri,
+ MCP_APP_RESOURCE,
+ );
+ assert.deepEqual(output.result, { ...MCP_APP_DEFERRED_RESULT, isError: true });
+ assert.isFalse(yield* harness.hasPendingBackgroundWork);
+ }).pipe(
+ Effect.provide(
+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
+ ),
+ ),
+ ),
+ );
+
const ORPHAN_WAIT_SCENARIO = "codex-orphaned-dynamic-tool";
const ORPHAN_WAIT_NATIVE_THREAD = "native-codex-orphan-wait-thread";
const ORPHAN_WAIT_NATIVE_TURN = "native-codex-orphan-wait-turn";
diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
index 51796da166e..51a686e2703 100644
--- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
@@ -3744,7 +3744,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
Effect.uninterruptibleMask((restore) =>
Effect.gen(function* () {
const reference =
- item.status === "completed" && item.result != null && item.error == null
+ item.result != null && item.error == null
? yield* restore(
getNativeThreadId(context.providerThread).pipe(
Effect.flatMap((threadId) =>
@@ -3798,7 +3798,13 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
? artifacts.turnItem
: {
...artifacts.turnItem,
- output: { [MCP_APP_OUTPUT_KEY]: reference, result: item.result },
+ output: {
+ [MCP_APP_OUTPUT_KEY]: reference,
+ result:
+ item.status === "failed"
+ ? { ...item.result, isError: true }
+ : item.result,
+ },
};
yield* emitProviderEvent({
type: "node.updated",
diff --git a/apps/web/src/session-logic.test.ts b/apps/web/src/session-logic.test.ts
index 9c4e38c30e5..df3eb85c3db 100644
--- a/apps/web/src/session-logic.test.ts
+++ b/apps/web/src/session-logic.test.ts
@@ -1307,6 +1307,23 @@ describe("HTML renders in the timeline", () => {
expect(rowsFor(entries, false)).toEqual(["message", "turn-fold", "mcp-app", "message"]);
});
+ it("hosts an MCP app whose call failed, as a form-deferring write does", () => {
+ const mcpApp = {
+ attachmentId: "render-thread-app-html",
+ server: "github",
+ tool: "create_pull_request",
+ resourceUri: "ui://github-mcp-server/pr-create",
+ };
+ const call = renderCall("failed", {
+ t3McpApp: mcpApp,
+ result: { content: [], isError: true },
+ });
+ const entries = turn(
+ call.type === "dynamic_tool" ? { ...call, toolName: "github.create_pull_request" } : call,
+ );
+ expect(entries.find((entry) => entry.kind === "mcp-app")).toMatchObject({ mcpApp });
+ });
+
it("keeps a render visible when its superseded attempt folds", () => {
const attempt: OrchestrationV2RunAttempt = {
id: RunAttemptId.make("attempt-superseded"),
diff --git a/apps/web/src/session-logic.ts b/apps/web/src/session-logic.ts
index 7b79dea6f74..6cf2761d3d6 100644
--- a/apps/web/src/session-logic.ts
+++ b/apps/web/src/session-logic.ts
@@ -711,10 +711,7 @@ export function deriveTimelineEntriesFromVisibleTurnItems(
continue;
}
- const mcpApp =
- item.type === "dynamic_tool" && item.status === "completed"
- ? mcpAppFromToolItem(item)
- : undefined;
+ const mcpApp = item.type === "dynamic_tool" ? mcpAppFromToolItem(item) : undefined;
if (mcpApp !== undefined) {
entries.push({
id: item.id,