Skip to content

Commit f4b89af

Browse files
WebMCP: ModelContext::registerTool takes an unregistration AbortSignal
This CL removes `ModelContext#unregisterTool()` in favor of a tool registration design where `registerTool()` takes an `AbortSignal` that controls the tool's registration lifetime. This matches the final proposal in [1], which is an improvement over the original proposal of supplying an identical `ModelContextTool` to `unregisterTool()`, and performing some kind of value/reference equality matching with the tool that is already registered. [1]: webmachinelearning/webmcp#130 R=beaufort.francois@gmail.com, khushalsagar Bug: 489045948,494350656 Change-Id: I8f66b06b092bf94bab4e1c40b35f9897e34511d3 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7681371 Reviewed-by: Fr <beaufort.francois@gmail.com> Commit-Queue: Dominic Farolino <dom@chromium.org> Cr-Commit-Position: refs/heads/main@{#1605522}
1 parent 94d3448 commit f4b89af

5 files changed

Lines changed: 43 additions & 20 deletions

‎webmcp/imperative/register_tool_invalid_json_schema.https.window.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,31 @@ test_register_tool_schema_error(
3838
TypeError,
3939
'registerTool throws when inputSchema contains non-serializable types (BigInt)',
4040
);
41+
42+
test(() => {
43+
// Register with invalid schema AND aborted signal. This asserts that input
44+
// schema validation happens before checking the aborted signal.
45+
const circularSchema = {};
46+
circularSchema.self = circularSchema;
47+
48+
assert_throws_js(
49+
TypeError,
50+
() => {
51+
navigator.modelContext.registerTool(
52+
{
53+
name: 'aborted_invalid_schema',
54+
description: 'empty',
55+
inputSchema: {
56+
type: "object",
57+
properties: {
58+
prop1: circularSchema,
59+
}
60+
},
61+
execute: () => {},
62+
},
63+
{ signal: AbortSignal.abort('aborted') }
64+
);
65+
},
66+
'Should throw TypeError for circular input schema, before checking if the signal is aborted'
67+
);
68+
}, 'registerTool throws on invalid schema even if an aborted signal was provided');
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict';
22

33
test(() => {
4-
navigator.modelContext.registerTool({
4+
const tool = {
55
name: 'empty',
66
description: 'echo empty',
77
execute: () => {},
8-
});
8+
};
99

10-
navigator.modelContext.unregisterTool('empty');
10+
const controller = new AbortController();
11+
navigator.modelContext.registerTool(tool, { signal: controller.signal });
12+
controller.abort();
1113
}, 'register tool with only required params');
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
'use strict';
22

33
test(() => {
4-
navigator.modelContext.registerTool({
4+
const tool = {
55
name: 'echo',
66
description: 'echo input',
77
execute: (obj) => obj.text,
88
annotations: {
99
// No `readOnlyHint` member.
1010
},
11-
});
11+
};
1212

13-
navigator.modelContext.unregisterTool('echo');
13+
const controller = new AbortController();
14+
navigator.modelContext.registerTool(tool, { signal: controller.signal });
15+
controller.abort();
1416
}, 'register tool with empty annotations');

‎webmcp/imperative/register_tool_with_schema.https.window.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
test(() => {
4-
navigator.modelContext.registerTool({
4+
const tool = {
55
name: 'echo',
66
description: 'echo input',
77
inputSchema: {
@@ -18,9 +18,11 @@ test(() => {
1818
annotations: {
1919
readOnlyHint: 'true',
2020
},
21-
});
21+
};
2222

23-
navigator.modelContext.unregisterTool('echo');
23+
const controller = new AbortController();
24+
navigator.modelContext.registerTool(tool, { signal: controller.signal });
25+
controller.abort();
2426
}, 'register and unregister script tool');
2527

2628
test(() => {

‎webmcp/imperative/unregister_invalid_tool.https.window.js‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)