Skip to content

Commit 0524618

Browse files
fix: MCP apps work end to end against a real Codex session
Found running the official ext-apps example server through Codex in the web client: - The compact wire output renamed the app reference, so clients never saw it; it now keeps its stored key. - Codex reports absent result fields as null (`_meta: null`), which the MCP Apps SDK rejects, silently dropping the tool result and every tools/call response. The host now passes results in the spec's shape. - The SDK sends ui/message content as an array of blocks; both forms are accepted. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 362e514 commit 0524618

4 files changed

Lines changed: 101 additions & 12 deletions

File tree

‎packages/client-runtime/src/mcpApps/host.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,55 @@ describe("makeMcpAppHost", () => {
100100
]);
101101
});
102102

103+
it("drops null fields providers report, which the MCP Apps SDK rejects", async () => {
104+
const { host, sent } = setup({
105+
callTool: async () => ({ content: [], structuredContent: null, _meta: null }),
106+
});
107+
host.receive({ jsonrpc: "2.0", id: 1, method: "ui/initialize", params: {} });
108+
host.receive({ jsonrpc: "2.0", method: "ui/notifications/initialized" });
109+
host.setToolCall({
110+
arguments: {},
111+
result: {
112+
content: [{ type: "text", text: "t" }],
113+
structuredContent: { time: "t" },
114+
_meta: null,
115+
},
116+
});
117+
host.receive({ jsonrpc: "2.0", id: 2, method: "tools/call", params: { name: "get-time" } });
118+
await flush();
119+
expect(sent.find((m) => m.method === "ui/notifications/tool-result")?.params).toEqual({
120+
content: [{ type: "text", text: "t" }],
121+
structuredContent: { time: "t" },
122+
});
123+
expect(sent.find((m) => m.id === 2)?.result).toEqual({ content: [] });
124+
});
125+
126+
it("accepts ui/message as one text block or the SDK's block array", async () => {
127+
const messages: Array<string> = [];
128+
const { host, sent } = setup({ sendMessage: async (text) => void messages.push(text) });
129+
host.receive({
130+
jsonrpc: "2.0",
131+
id: 1,
132+
method: "ui/message",
133+
params: { role: "user", content: { type: "text", text: "one" } },
134+
});
135+
host.receive({
136+
jsonrpc: "2.0",
137+
id: 2,
138+
method: "ui/message",
139+
params: { role: "user", content: [{ type: "text", text: "two" }] },
140+
});
141+
host.receive({
142+
jsonrpc: "2.0",
143+
id: 3,
144+
method: "ui/message",
145+
params: { role: "user", content: [{ type: "image", data: "x" }] },
146+
});
147+
await flush();
148+
expect(messages).toEqual(["one", "two"]);
149+
expect(sent.find((m) => m.id === 3)?.error).toMatchObject({ code: -32602 });
150+
});
151+
103152
it("sends only changed context fields, and nothing before initialization", () => {
104153
const { host, sent, setTheme } = setup();
105154
setTheme("light");

‎packages/client-runtime/src/mcpApps/host.ts‎

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ function isId(value: unknown): value is JsonRpcId {
8181
return typeof value === "string" || (typeof value === "number" && Number.isFinite(value));
8282
}
8383

84+
/**
85+
* A `CallToolResult` as the spec's schema accepts it. Providers can report
86+
* absent fields as null (Codex sends `_meta: null`), which the MCP Apps SDK
87+
* rejects, dropping the whole notification or response.
88+
*/
89+
export function normalizeMcpAppToolResult(result: McpAppCallToolResult): McpAppCallToolResult {
90+
return {
91+
content: Array.isArray(result.content) ? result.content : [],
92+
...(Predicate.isObject(result.structuredContent)
93+
? { structuredContent: result.structuredContent }
94+
: {}),
95+
...(result.isError === true ? { isError: true } : {}),
96+
...(Predicate.isObject(result._meta) ? { _meta: result._meta } : {}),
97+
};
98+
}
99+
84100
const errorMessage = (error: unknown) =>
85101
error instanceof McpAppHostRefusal
86102
? error.message
@@ -112,7 +128,9 @@ export function makeMcpAppHost(options: McpAppHostOptions): McpAppHost {
112128
notify("ui/notifications/tool-input", {
113129
arguments: Predicate.isObject(toolCall.arguments) ? toolCall.arguments : {},
114130
});
115-
if (toolCall.result !== undefined) notify("ui/notifications/tool-result", toolCall.result);
131+
if (toolCall.result !== undefined) {
132+
notify("ui/notifications/tool-result", normalizeMcpAppToolResult(toolCall.result));
133+
}
116134
};
117135

118136
const answer = (id: JsonRpcId, run: () => Promise<unknown>) => {
@@ -169,7 +187,11 @@ export function makeMcpAppHost(options: McpAppHostOptions): McpAppHost {
169187
fail(id, -32602, "tools/call needs a tool name and object arguments.");
170188
return;
171189
}
172-
answer(id, () => options.callTool({ name, arguments: args as Record<string, unknown> }));
190+
answer(id, () =>
191+
options
192+
.callTool({ name, arguments: args as Record<string, unknown> })
193+
.then(normalizeMcpAppToolResult),
194+
);
173195
return;
174196
}
175197
case "resources/read": {
@@ -191,16 +213,18 @@ export function makeMcpAppHost(options: McpAppHostOptions): McpAppHost {
191213
return;
192214
}
193215
case "ui/message": {
194-
const content = Predicate.isObject(params.content) ? params.content : undefined;
195-
if (
196-
params.role !== "user" ||
197-
content?.type !== "text" ||
198-
typeof content.text !== "string"
199-
) {
216+
// The SDK sends an array of content blocks; the spec text shows one block.
217+
const blocks = Array.isArray(params.content) ? params.content : [params.content];
218+
const texts = blocks.map((block) =>
219+
Predicate.isObject(block) && block.type === "text" && typeof block.text === "string"
220+
? block.text
221+
: undefined,
222+
);
223+
if (params.role !== "user" || texts.length === 0 || texts.includes(undefined)) {
200224
fail(id, -32602, "Only text messages from the user role are supported.");
201225
return;
202226
}
203-
const text = content.text;
227+
const text = texts.join("\n").trim();
204228
answer(id, () => options.sendMessage(text).then(() => ({})));
205229
return;
206230
}

‎packages/shared/src/mcpApp.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,18 @@ describe("tool metadata", () => {
8484
expect(mcpAppToolCallableByApp({ ui: { visibility: ["model", "app"] } })).toBe(true);
8585
});
8686
});
87+
88+
describe("mcpAppFromToolItem", () => {
89+
it("reads the app from both the stored output and its compact wire form", async () => {
90+
const { compactDynamicToolOutput, mcpAppFromToolItem } = await import("./toolOutput.ts");
91+
const app = {
92+
attachmentId: "thread-1-abc-html",
93+
server: "weather",
94+
tool: "get_weather",
95+
resourceUri: "ui://weather/dashboard",
96+
};
97+
const stored = { t3McpApp: app, result: { content: [] } };
98+
expect(mcpAppFromToolItem({ output: stored })).toEqual(app);
99+
expect(mcpAppFromToolItem({ output: compactDynamicToolOutput(stored) })).toEqual(app);
100+
});
101+
});

‎packages/shared/src/toolOutput.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ interface CompactToolOutput {
3737
scheduledTaskId?: string;
3838
status?: "rolled_back";
3939
htmlRender?: HtmlRenderReference;
40-
mcpApp?: McpAppReference;
40+
[MCP_APP_OUTPUT_KEY]?: McpAppReference;
4141
thread?: { threadId: string };
4242
threads?: Array<{ threadId?: string; status?: "rolled_back" }>;
4343
}
@@ -124,7 +124,8 @@ export function compactDynamicToolOutput(value: unknown): CompactToolOutput | un
124124
const htmlRender = readHtmlRenderReference(data.htmlRender);
125125
if (htmlRender !== undefined) output.htmlRender = htmlRender;
126126
const mcpApp = readMcpAppReference(data[MCP_APP_OUTPUT_KEY]);
127-
if (mcpApp !== undefined) output.mcpApp = mcpApp;
127+
// Kept under its stored key, so the compact wire output reads back the same way.
128+
if (mcpApp !== undefined) output[MCP_APP_OUTPUT_KEY] = mcpApp;
128129
const nestedThreadId = Predicate.isObject(data.thread)
129130
? boundedId(data.thread.threadId)
130131
: undefined;
@@ -186,7 +187,7 @@ export function htmlRenderFromToolItem(item: {
186187
export function mcpAppFromToolItem(item: {
187188
readonly output?: unknown;
188189
}): McpAppReference | undefined {
189-
return compactDynamicToolOutput(item.output)?.mcpApp;
190+
return compactDynamicToolOutput(item.output)?.[MCP_APP_OUTPUT_KEY];
190191
}
191192

192193
/** Some providers report completion even when command output describes a failure. */

0 commit comments

Comments
 (0)