Skip to content

Commit ec5ace9

Browse files
beaufortfrancoisChromium LUCI CQ
authored andcommitted
[WebMCP] Expose RegisteredTool.inputSchema as an object
This CL updates the WebIDL definition for RegisteredTool.inputSchema from DOMString to object to align with the WebMCP specification: webmachinelearning/webmcp#241 In Blink, JSONStringToScriptObject() is used in ModelContext::OnGetScriptToolsCompleted() to parse the mojo string into a ScriptObject when constructing the RegisteredTool results. Web platform tests in external/wpt/webmcp/imperative/ are updated to expect inputSchema as a JavaScript object rather than a serialized JSON string. Bug: 489045948 Change-Id: I7a28b36d725c85e24726708aecee4cd9f3cbb476 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8248081 Reviewed-by: Dominic Farolino <[email protected]> Commit-Queue: Fr <[email protected]> Cr-Commit-Position: refs/heads/main@{#1682945}
1 parent ca9c463 commit ec5ace9

12 files changed

Lines changed: 49 additions & 49 deletions

‎third_party/blink/renderer/core/script_tools/model_context.cc‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,16 @@ void ModelContext::OnGetScriptToolsCompleted(
10431043
result->setTitle(t->title);
10441044
result->setDescription(t->description);
10451045
if (!t->input_schema.IsNull()) {
1046-
result->setInputSchema(t->input_schema);
1046+
ScriptState* script_state = resolver->GetScriptState();
1047+
ScriptState::Scope scope(script_state);
1048+
v8::TryCatch try_catch(script_state->GetIsolate());
1049+
auto script_object =
1050+
JSONStringToScriptObject(script_state, t->input_schema);
1051+
ScriptValue script_value = script_object;
1052+
if (try_catch.HasCaught() || script_value.IsEmpty()) {
1053+
continue;
1054+
}
1055+
result->setInputSchema(script_object);
10471056
}
10481057
if (t->annotations) {
10491058
auto* annotations = ToolAnnotations::Create();

‎third_party/blink/renderer/core/script_tools/model_context.idl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ dictionary ExecuteToolOptions {
2828
dictionary RegisteredTool {
2929
required DOMString name;
3030
required DOMString description;
31-
DOMString inputSchema;
31+
object inputSchema;
3232
// `title` can be exposed as a `DOMString` since it was taken in by a
3333
// `USVString`, meaning all unmatched surrogate processing has already been
3434
// done, and there's no need to do it again on tool exposure.

‎third_party/blink/web_tests/external/wpt/webmcp/declarative/getTools-declarative-schema.https.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
required: ["query"]
3636
};
3737

38-
const tool = await waitForFormToolSchemaToMatch(JSON.stringify(expected_schema_obj));
38+
const tool = await waitForFormToolSchemaToMatch(expected_schema_obj);
3939

4040
assert_equals(tool.name, "search_tool", "Tool name matches");
4141
assert_equals(tool.description, "Search the web", "Tool description matches");

‎third_party/blink/web_tests/external/wpt/webmcp/declarative/toolchange-on-control-add-remove.https.html‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@
1717
const form = document.getElementById('f1');
1818

1919
// Wait for the baseline form schema.
20-
await waitForFormToolSchemaToMatch(`{"type":"object","properties":{"input_name":{"type":"string"}},"required":[]}`);
20+
await waitForFormToolSchemaToMatch({"type":"object","properties":{"input_name":{"type":"string"}},"required":[]});
2121

2222
// Test adding an input
2323
const i = document.createElement('input');
2424
i.type = 'text';
2525
i.name = 'new_input';
2626
i.id = 'new_input';
2727
form.appendChild(i);
28-
await waitForFormToolSchemaToMatch(`{"type":"object","properties":{"input_name":{"type":"string"},"new_input":{"type":"string"}},"required":[]}`);
28+
await waitForFormToolSchemaToMatch({"type":"object","properties":{"input_name":{"type":"string"},"new_input":{"type":"string"}},"required":[]});
2929

3030
// Test removing an input
3131
const newInput = document.getElementById('new_input');
3232
form.removeChild(newInput);
33-
await waitForFormToolSchemaToMatch(`{"type":"object","properties":{"input_name":{"type":"string"}},"required":[]}`);
33+
await waitForFormToolSchemaToMatch({"type":"object","properties":{"input_name":{"type":"string"}},"required":[]});
3434
}, "Test that toolchange event fires when controls are added or removed");
3535
</script>
3636
</body>

‎third_party/blink/web_tests/external/wpt/webmcp/imperative/exposedTo-cross-origin-child.https.html‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
assert_true(toolsAreEqual(tool, {
4545
name: 'parent_tool_exposed',
4646
description: 'Parent tool exposed to child',
47-
inputSchema: JSON.stringify({ type: 'object', properties: { query: { type: 'string' } } }),
47+
inputSchema: { type: 'object', properties: { query: { type: 'string' } } },
4848
origin: self.origin
4949
}), 'Tool details should match');
5050

@@ -104,7 +104,7 @@
104104
assert_true(toolsAreEqual(tool, {
105105
name: 'iframe_tool_exposed',
106106
description: 'Iframe tool exposed to parent',
107-
inputSchema: JSON.stringify({ type: 'object', properties: { query: { type: 'string' } } }),
107+
inputSchema: { type: 'object', properties: { query: { type: 'string' } } },
108108
origin: host_info.HTTPS_REMOTE_ORIGIN
109109
}), 'Tool details should match');
110110

‎third_party/blink/web_tests/external/wpt/webmcp/imperative/exposedTo-defaults-same-origin.https.html‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
assert_true(toolsAreEqual(tool, {
4242
name: 'parent_tool_default',
4343
description: 'Parent tool with default exposure',
44-
inputSchema: JSON.stringify({ type: 'object', properties: { query: { type: 'string' } } }),
44+
inputSchema: { type: 'object', properties: { query: { type: 'string' } } },
4545
annotations: { readOnlyHint: true, untrustedContentHint: false },
4646
origin: self.origin
4747
}), 'Tool details should match');
@@ -90,7 +90,7 @@
9090
assert_true(toolsAreEqual(tool, {
9191
name: 'parent_tool_empty',
9292
description: 'Parent tool with empty exposedTo',
93-
inputSchema: JSON.stringify({ type: 'object', properties: { query: { type: 'string' } } }),
93+
inputSchema: { type: 'object', properties: { query: { type: 'string' } } },
9494
origin: self.origin
9595
}), 'Tool details should match');
9696

‎third_party/blink/web_tests/external/wpt/webmcp/imperative/exposedTo-multiple-children.https.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
assert_true(toolsAreEqual(tool, {
7070
name: 'tool_b_to_c',
7171
description: 'Tool B exposed to C',
72-
inputSchema: JSON.stringify({ type: 'object', properties: { query: { type: 'string' } } }),
72+
inputSchema: { type: 'object', properties: { query: { type: 'string' } } },
7373
origin: origin_b
7474
}), 'Tool details should match');
7575

‎third_party/blink/web_tests/external/wpt/webmcp/imperative/getTools-imperative-schema.https.html‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,23 @@
4141
assert_equals(registeredTool.description, "Search the web", "Tool description matches registered tool");
4242

4343
assert_true(!!registeredTool.inputSchema, "inputSchema should be non-empty");
44-
const schema = JSON.parse(registeredTool.inputSchema);
45-
46-
assert_equals(schema.type, "object", "Schema should be an object");
44+
assert_equals(registeredTool.inputSchema.type, "object", "Schema should be an object");
4745

4846
// 'query' property validation (required string)
49-
assert_true("query" in schema.properties, "Schema should contain 'query' property");
50-
assert_equals(schema.properties.query.type, "string", "Query property type should be string");
51-
assert_equals(schema.properties.query.description, "The search query", "Query property description is correct");
52-
assert_array_equals(schema.required, ["query"], "Query should be a required property");
47+
assert_true("query" in registeredTool.inputSchema.properties, "Schema should contain 'query' property");
48+
assert_equals(registeredTool.inputSchema.properties.query.type, "string", "Query property type should be string");
49+
assert_equals(registeredTool.inputSchema.properties.query.description, "The search query", "Query property description is correct");
50+
assert_array_equals(registeredTool.inputSchema.required, ["query"], "Query should be a required property");
5351

5452
// 'limit' property validation (optional number)
55-
assert_true("limit" in schema.properties, "Schema should contain 'limit' property");
56-
assert_equals(schema.properties.limit.type, "number", "Limit property type should be number");
57-
assert_equals(schema.properties.limit.description, "Max results count", "Limit property description is correct");
53+
assert_true("limit" in registeredTool.inputSchema.properties, "Schema should contain 'limit' property");
54+
assert_equals(registeredTool.inputSchema.properties.limit.type, "number", "Limit property type should be number");
55+
assert_equals(registeredTool.inputSchema.properties.limit.description, "Max results count", "Limit property description is correct");
5856

5957
// 'safe_search' property validation (optional boolean)
60-
assert_true("safe_search" in schema.properties, "Schema should contain 'safe_search' property");
61-
assert_equals(schema.properties.safe_search.type, "boolean", "safe_search property type should be boolean");
62-
assert_equals(schema.properties.safe_search.description, "Enable safe search filtering", "safe_search property description is correct");
58+
assert_true("safe_search" in registeredTool.inputSchema.properties, "Schema should contain 'safe_search' property");
59+
assert_equals(registeredTool.inputSchema.properties.safe_search.type, "boolean", "safe_search property type should be boolean");
60+
assert_equals(registeredTool.inputSchema.properties.safe_search.description, "Enable safe search filtering", "safe_search property description is correct");
6361
}, "Test that getTools() returns the correct inputSchema for an imperative tool");
6462
</script>
6563
</body>

‎third_party/blink/web_tests/external/wpt/webmcp/imperative/resources/helpers.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ function toolsAreEqual(actual, expected) {
2020
if (actual.description !== expected.description) {
2121
return `descriptions are unequal: ${actual.description} !== ${expected.description}`;
2222
}
23-
if (actual.inputSchema !== expected.inputSchema) {
24-
return `inputSchemas are unequal: ${actual.inputSchemas} !== ${expected.inputSchemas}`;
23+
if (JSON.stringify(actual.inputSchema) !==
24+
JSON.stringify(expected.inputSchema)) {
25+
return `inputSchemas are unequal: ${
26+
JSON.stringify(
27+
actual.inputSchema)} !== ${JSON.stringify(expected.inputSchema)}`;
2528
}
2629
if (actual.origin !== expected.origin) {
2730
return `origins are unequal: ${actual.origin} !== ${expected.origin}`;

‎third_party/blink/web_tests/external/wpt/webmcp/resources/helpers.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,23 @@ async function waitForTool(name) {
1818
});
1919
}
2020

21-
// Wait for the active WebMCP tool's input schema to match the expected schema string.
21+
// Wait for the active WebMCP tool's input schema to match the expected schema.
2222
async function waitForFormToolSchemaToMatch(expected_schema) {
23+
const isMatch = (tool) => {
24+
if (!tool)
25+
return false;
26+
return JSON.stringify(tool.inputSchema) === JSON.stringify(expected_schema);
27+
};
28+
2329
const [tool] = await document.modelContext.getTools();
24-
if (tool && tool.inputSchema === expected_schema) {
30+
if (isMatch(tool)) {
2531
return tool;
2632
}
2733
return new Promise(resolve => {
2834
const ac = new AbortController();
2935
document.modelContext.addEventListener('toolchange', async e => {
3036
const [tool] = await document.modelContext.getTools();
31-
if (tool && tool.inputSchema === expected_schema) {
37+
if (isMatch(tool)) {
3238
resolve(tool);
3339
ac.abort();
3440
}

0 commit comments

Comments
 (0)