Skip to content

Commit 9676902

Browse files
oddharshchromium-wpt-export-bot
authored andcommitted
WebMCP: Reject tool results that can't be converted to a string
Imperative and declarative WebMCP tools both turn a non-object result into a string with `ScriptValue::ToString()`, which calls `ToString(context).ToLocalChecked()`. Converting a Symbol throws, so the `MaybeLocal` is empty and the renderer hits a fatal V8 error ("Empty MaybeLocal"): await document.modelContext.registerTool({ name: 'tool', description: 'desc', execute: () => Symbol(), }); const [tool] = await document.modelContext.getTools(); document.modelContext.executeTool(tool); // Crash. A declarative tool crashes the same way through `e.respondWith(Promise.resolve(Symbol()))`. This CL converts these results inside a `v8::TryCatch`, and fails the execution when conversion throws, the same way both paths already handle an object that can't be serialized to JSON. `executeTool()` then rejects with `OperationError`. For imperative tools this matches the spec: "invoke the execute callback" serializes the result to a JSON string, which throws for a Symbol, so the execution fails with "unserializable result". In `ModelContext`, the object and non-object branches now share one `TryCatch`. How other non-object results, such as strings and `undefined`, should serialize is a separate question, which I raised in webmachinelearning/webmcp#361. Adds unserializable-result cases (a Symbol, an object with a BigInt property, a throwing toJSON(), and a throwing getter) to the imperative executeTool-error-window-onerror test, and a declarative executeTool-respondWith-unserializable test covering the same cases. The Symbol cases crash without this CL. Also adds me to AUTHORS. [email protected], [email protected] Bug: None Change-Id: I6266226ae361e12e52df65b218b70f6a3102aba5 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8640683 Reviewed-by: Mason Freed <[email protected]> Commit-Queue: Aadharsh Pannirselvam <[email protected]> Commit-Queue: Khushal Sagar <[email protected]> Reviewed-by: Khushal Sagar <[email protected]> Cr-Commit-Position: refs/heads/main@{#1740992}
1 parent 91d0bab commit 9676902

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<title>WebMCP: executeTool with declarative tool returning an unserializable value via respondWith</title>
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
<script src="../resources/helpers.js"></script>
7+
<body>
8+
<form id="form" toolname="unserializable_tool" tooldescription="Declarative tool returning an unserializable value" toolautosubmit>
9+
</form>
10+
11+
<script>
12+
// Each case returns a value that can't become a tool result string: either
13+
// JSON serialization (objects) or string conversion (primitives) throws.
14+
const cases = [
15+
{
16+
description: 'a Symbol',
17+
makeResult: () => Symbol('result'),
18+
},
19+
{
20+
description: 'an object with a BigInt property',
21+
makeResult: () => ({value: 1n}),
22+
},
23+
{
24+
description: 'an object whose toJSON() throws',
25+
makeResult: () => ({toJSON() { throw new Error('toJSON'); }}),
26+
},
27+
{
28+
description: 'an object with a throwing getter',
29+
makeResult: () => ({get value() { throw new Error('getter'); }}),
30+
},
31+
];
32+
33+
let makeResult;
34+
const form = document.querySelector('#form');
35+
form.addEventListener('submit', (e) => {
36+
e.preventDefault();
37+
e.respondWith(Promise.resolve(makeResult()));
38+
});
39+
40+
for (const {description, makeResult: make} of cases) {
41+
promise_test(async t => {
42+
const tool = await waitForTool('unserializable_tool');
43+
makeResult = make;
44+
45+
await promise_rejects_dom(
46+
t,
47+
'OperationError',
48+
document.modelContext.executeTool(tool),
49+
`executeTool() should reject with OperationError when respondWith is passed ${description}`
50+
);
51+
}, `Declarative tool executeTool() rejects when respondWith() receives ${description}`);
52+
}
53+
</script>
54+
</body>

‎webmcp/imperative/executeTool-error-window-onerror.https.html‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,60 @@
7171

7272
assert_false(errorFired, 'window.onerror/error event should not be fired');
7373
}, 'Tool execution returning circular object rejects and does not trigger window.onerror');
74+
75+
// Each case returns a value that can't become a tool result string: either
76+
// JSON serialization (objects) or string conversion (primitives) throws.
77+
const unserializableCases = [
78+
{
79+
name: 'symbol_tool',
80+
description: 'a Symbol',
81+
execute: () => Symbol('result'),
82+
},
83+
{
84+
name: 'bigint_property_tool',
85+
description: 'an object with a BigInt property',
86+
execute: () => ({value: 1n}),
87+
},
88+
{
89+
name: 'throwing_tojson_tool',
90+
description: 'an object whose toJSON() throws',
91+
execute: () => ({toJSON() { throw new Error('toJSON'); }}),
92+
},
93+
{
94+
name: 'throwing_getter_tool',
95+
description: 'an object with a throwing getter',
96+
execute: () => ({get value() { throw new Error('getter'); }}),
97+
},
98+
];
99+
100+
for (const {name, description, execute} of unserializableCases) {
101+
promise_test(async t => {
102+
let errorFired = false;
103+
const errorHandler = () => {
104+
errorFired = true;
105+
};
106+
window.addEventListener('error', errorHandler);
107+
t.add_cleanup(() => {
108+
window.removeEventListener('error', errorHandler);
109+
});
110+
111+
await document.modelContext.registerTool({
112+
name,
113+
description: `A tool that returns ${description}`,
114+
execute
115+
});
116+
117+
const tools = await document.modelContext.getTools();
118+
const tool = tools.find(t => t.name === name);
119+
assert_true(!!tool, 'Should find the registered tool');
120+
121+
await promise_rejects_dom(t, 'OperationError',
122+
document.modelContext.executeTool(tool),
123+
`executeTool() rejects when the tool execution returns ${description}`);
124+
125+
assert_false(errorFired, 'window.onerror/error event should not be fired');
126+
}, `Tool execution returning ${description} rejects and does not trigger window.onerror`);
127+
}
74128
</script>
75129
</body>
76130
</html>

0 commit comments

Comments
 (0)