The best way to control Claude Code, Codex and more from any computer or phone
fix(client-runtime): MCP App requests with undefined values reach the server (#33772)
Julius Marminge committed
Nov 11, 2026 at 15:51 UTC
4d52d321cbcc0909a3b2307c6474cceac5f56241
2 files changed
+39
-11
packages/client-runtime/src/mcpApps/host.test.ts
+25
-3
@@ -203,10 +203,12 @@ describe("makeMcpAppHost", () => {
203
});
204
205
it("proxies tools/call and reports a refusal as a JSON-RPC error", async () => {
206
+ const received: unknown[] = [];
207
const { host, sent } = setup({
208
callTool: async (input) => {
209
if (input.name === "delete_everything")
210
throw new McpAppHostRefusal({ message: "Declined by the user." });
211
+ received.push(input.arguments);
212
return { content: [], structuredContent: { ok: true } };
213
},
214
});
@@ -217,7 +219,18 @@ describe("makeMcpAppHost", () => {
219
method: "tools/call",
220
params: { name: "delete_everything", arguments: {} },
221
});
222
+ // postMessage's structured clone keeps undefined properties, which JSON cannot carry.
223
+ host.receive({
224
+ jsonrpc: "2.0",
225
+ id: 9,
226
+ method: "tools/call",
227
+ params: {
228
+ name: "poll",
229
+ arguments: { since: undefined, nested: { a: undefined, b: 1 }, list: [1, undefined] },
230
+ },
231
+ });
232
await flush();
233
+ expect(received).toStrictEqual([{}, { nested: { b: 1 }, list: [1, null] }]);
234
expect(sent).toContainEqual({
235
jsonrpc: "2.0",
236
id: 7,
@@ -240,8 +253,14 @@ describe("makeMcpAppHost", () => {
253
});
254
host.receive({ jsonrpc: "2.0", id: 3, method: "sampling/createMessage", params: {} });
255
host.receive({ jsonrpc: "2.0", id: 4, method: "ui/message", params: { role: "assistant" } });
256
+ host.receive({
257
+ jsonrpc: "2.0",
258
+ id: 5,
259
+ method: "tools/call",
260
+ params: { name: "poll", arguments: { since: 1n } },
261
+ });
262
expect(sent.map((message) => (message.error as { code: number }).code)).toEqual([
244
- -32602, -32601, -32602,
263
+ -32602, -32601, -32602, -32602,
264
]);
265
});
266
@@ -325,7 +344,10 @@ describe("makeMcpAppHost", () => {
344
jsonrpc: "2.0",
345
id: 1,
346
method: "ui/update-model-context",
328
- params: { content: [{ type: "text", text: "2 overdue" }], structuredContent: { overdue: 2 } },
347
+ params: {
348
+ content: [{ type: "text", text: "2 overdue" }],
349
+ structuredContent: { overdue: 2, since: undefined },
350
+ },
351
});
352
const embedded = {
353
type: "resource",
@@ -347,7 +369,7 @@ describe("makeMcpAppHost", () => {
369
});
370
host.receive({ jsonrpc: "2.0", id: 4, method: "ui/download-file", params: { contents: [{}] } });
371
await flush();
350
- expect(contexts).toEqual([
372
+ expect(contexts).toStrictEqual([
373
{ content: [{ type: "text", text: "2 overdue" }], structuredContent: { overdue: 2 } },
374
]);
375
expect(downloads[0]).toEqual([
packages/client-runtime/src/mcpApps/host.ts
+14
-8
@@ -119,11 +119,11 @@ export interface McpAppHost {
119
readonly dispose: () => void;
120
}
121
122
-function jsonBytes(value: unknown): number {
122
+function stringifyJson(value: unknown): string | undefined {
123
try {
124
- return encoder.encode(JSON.stringify(value) ?? "").byteLength;
124
+ return JSON.stringify(value);
125
} catch {
126
- return Number.POSITIVE_INFINITY;
126
+ return undefined;
127
}
128
}
129
@@ -395,10 +395,14 @@ export function makeMcpAppHost(options: McpAppHostOptions): McpAppHost {
395
const hostHandle: McpAppHost = {
396
receive: (data) => {
397
if (disposed || !Predicate.isObject(data) || data.jsonrpc !== "2.0") return;
398
- if (jsonBytes(data) > MAX_MESSAGE_BYTES) {
399
- if (isId(data.id) && typeof data.method === "string") {
400
- fail(data.id, -32600, "Message too large.");
401
- }
398
+ const requestId = isId(data.id) && typeof data.method === "string" ? data.id : undefined;
399
+ const json = stringifyJson(data);
400
+ if (json === undefined) {
401
+ if (requestId !== undefined) fail(requestId, -32602, "Params must be JSON.");
402
+ return;
403
+ }
404
+ if (encoder.encode(json).byteLength > MAX_MESSAGE_BYTES) {
405
+ if (requestId !== undefined) fail(requestId, -32600, "Message too large.");
406
return;
407
}
408
const method = data.method;
@@ -406,7 +410,9 @@ export function makeMcpAppHost(options: McpAppHostOptions): McpAppHost {
410
if (isId(data.id) && ("result" in data || "error" in data)) handleResponse(data.id);
411
return;
412
}
409
- const params = Predicate.isObject(data.params) ? data.params : {};
413
+ // postMessage keeps undefined, which the RPC's JSON codec rejects; pass on the JSON form.
414
+ const { params: jsonParams }: { params?: unknown } = JSON.parse(json);
415
+ const params = Predicate.isObject(jsonParams) ? jsonParams : {};
416
if (isId(data.id)) handleRequest(data.id, method, params);
417
else handleNotification(method, params);
418
},