Skip to content

Commit 71866d8

Browse files
domfarolinoChromium LUCI CQ
authored andcommitted
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 [email protected], khushalsagar Bug: 489045948,494350656 Change-Id: I8f66b06b092bf94bab4e1c40b35f9897e34511d3 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7681371 Reviewed-by: Fr <[email protected]> Commit-Queue: Dominic Farolino <[email protected]> Cr-Commit-Position: refs/heads/main@{#1605522}
1 parent 17404f4 commit 71866d8

17 files changed

Lines changed: 119 additions & 42 deletions

‎android_webview/test/data/web_tests/webexposed/global-interface-listing-expected.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6358,7 +6358,6 @@ interface ModelContext
63586358
attribute @@toStringTag
63596359
method constructor
63606360
method registerTool
6361-
method unregisterTool
63626361
interface Mojo
63636362
static method bindInterface
63646363
static method createDataPipe

‎third_party/blink/renderer/bindings/generated_in_core.gni‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ generated_dictionary_sources_in_core = [
306306
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_memory_measurement.h",
307307
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_message_event_init.cc",
308308
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_message_event_init.h",
309+
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_model_context_register_tool_options.cc",
310+
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_model_context_register_tool_options.h",
309311
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_mojo_create_data_pipe_options.cc",
310312
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_mojo_create_data_pipe_options.h",
311313
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_mojo_create_data_pipe_result.cc",

‎third_party/blink/renderer/core/html/forms/html_form_element.cc‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,7 @@ void HTMLFormElement::UpdateMcpDefinitionsIfNeeded() {
345345
active_webmcp_tool_->CallDoneCallback(base::unexpected(ScriptToolError(
346346
ScriptToolErrorCode::kToolCancelled,
347347
"Tool execution cancelled, since tool definition was updated")));
348-
model_context->unregisterTool(active_webmcp_tool_->ToolName(),
349-
ASSERT_NO_EXCEPTION);
348+
model_context->UnregisterTool(active_webmcp_tool_->ToolName());
350349
active_webmcp_tool_ = nullptr;
351350
}
352351

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

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
1111
#include "third_party/blink/public/web/web_script_tool_types.h"
1212
#include "third_party/blink/renderer/bindings/core/v8/capture_source_location.h"
13+
#include "third_party/blink/renderer/bindings/core/v8/v8_model_context_register_tool_options.h"
1314
#include "third_party/blink/renderer/bindings/core/v8/v8_model_context_tool.h"
1415
#include "third_party/blink/renderer/bindings/core/v8/v8_script_runner.h"
1516
#include "third_party/blink/renderer/bindings/core/v8/v8_tool_annotations.h"
@@ -20,6 +21,7 @@
2021
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
2122
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
2223
#include "third_party/blink/renderer/core/html/html_script_element.h"
24+
#include "third_party/blink/renderer/core/inspector/console_message.h"
2325
#include "third_party/blink/renderer/core/probe/core_probes.h"
2426
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
2527
#include "third_party/blink/renderer/platform/bindings/source_location.h"
@@ -138,6 +140,25 @@ String ComputeScriptToolResult(const Document& document) {
138140

139141
} // namespace
140142

143+
class ModelContext::ToolUnregisterAbortAlgorithm final
144+
: public AbortSignal::Algorithm {
145+
public:
146+
ToolUnregisterAbortAlgorithm(ModelContext* model_context,
147+
const String& tool_name)
148+
: model_context_(model_context), tool_name_(tool_name) {}
149+
150+
void Run() override { model_context_->UnregisterTool(tool_name_); }
151+
152+
void Trace(Visitor* visitor) const override {
153+
visitor->Trace(model_context_);
154+
AbortSignal::Algorithm::Trace(visitor);
155+
}
156+
157+
private:
158+
Member<ModelContext> model_context_;
159+
String tool_name_;
160+
};
161+
141162
class ModelContext::ToolFunctionFinishedCallback
142163
: public ThenCallable<IDLAny, ToolFunctionFinishedCallback> {
143164
public:
@@ -208,6 +229,7 @@ void ModelContext::ForEachScriptTool(
208229

209230
void ModelContext::registerTool(ScriptState* script_state,
210231
ModelContextTool* tool,
232+
ModelContextRegisterToolOptions* options,
211233
ExceptionState& exception_state) {
212234
if (tool_map_.find(tool->name()) != tool_map_.end()) {
213235
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
@@ -237,6 +259,30 @@ void ModelContext::registerTool(ScriptState* script_state,
237259
}
238260
}
239261

262+
if (options && options->hasSignal()) {
263+
AbortSignal* signal = options->signal();
264+
if (signal->aborted()) {
265+
ExecutionContext::From(script_state)
266+
->AddConsoleMessage(MakeGarbageCollected<ConsoleMessage>(
267+
mojom::blink::ConsoleMessageSource::kJavaScript,
268+
mojom::blink::ConsoleMessageLevel::kWarning,
269+
"Tool '" + tool->name() +
270+
"' was not registered because its AbortSignal was already "
271+
"aborted."));
272+
return;
273+
}
274+
275+
// We intentionally do not use `ScopedAbortState` or otherwise explicitly
276+
// manage this algorithm handle. For imperative tools, the tool's
277+
// registration lifecycle is exactly bound to the given abort signal. Since
278+
// the signal automatically clears its internal algorithm list upon
279+
// aborting, no explicit cleanup is required here. If there were a way to
280+
// unregister an imperative tools otherwise, then during that process we
281+
// would remove the unregistration algorithm from the given signal.
282+
std::ignore = signal->AddAlgorithm(
283+
MakeGarbageCollected<ToolUnregisterAbortAlgorithm>(this, tool->name()));
284+
}
285+
240286
auto script_tool = mojom::blink::ScriptTool::New();
241287
script_tool->name = tool->name();
242288
script_tool->description = tool->description();
@@ -259,12 +305,9 @@ void ModelContext::registerTool(ScriptState* script_state,
259305
OnToolChange();
260306
}
261307

262-
void ModelContext::unregisterTool(const String& tool_name,
263-
ExceptionState& exception_state) {
264-
auto it = tool_map_.find(tool_name);
308+
void ModelContext::UnregisterTool(const String& name) {
309+
auto it = tool_map_.find(name);
265310
if (it == tool_map_.end()) {
266-
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
267-
"Invalid tool name");
268311
return;
269312
}
270313

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class AbortSignal;
2626
class Element;
2727
class SourceLocation;
2828
class ModelContextOptions;
29+
class ModelContextRegisterToolOptions;
2930
class ModelContextTool;
3031

3132
class DeclarativeWebMCPTool : public GarbageCollectedMixin {
@@ -108,8 +109,9 @@ class CORE_EXPORT ModelContext : public ScriptWrappable {
108109

109110
void registerTool(ScriptState* state,
110111
ModelContextTool* tool,
112+
ModelContextRegisterToolOptions* options,
111113
ExceptionState& exception_state);
112-
void unregisterTool(const String& name, ExceptionState& exception_state);
114+
void UnregisterTool(const String& name);
113115

114116
std::optional<ScriptToolDeclaration> GetScriptToolDeclaration(
115117
const String& name) const;
@@ -145,6 +147,7 @@ class CORE_EXPORT ModelContext : public ScriptWrappable {
145147

146148
private:
147149
class ToolFunctionFinishedCallback;
150+
class ToolUnregisterAbortAlgorithm;
148151

149152
bool ExecuteV8Tool(V8ToolExecuteCallback* tool_function,
150153
const base::UnguessableToken& execution_id,

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
// https://webmachinelearning.github.io/webmcp/#model-context-container
66

7+
dictionary ModelContextRegisterToolOptions {
8+
AbortSignal signal;
9+
};
10+
711
[
812
Exposed=Window,
913
SecureContext,
1014
RuntimeEnabled=WebMCP
1115
] interface ModelContext {
12-
[CallWith=ScriptState, RaisesException, MeasureAs=ModelContextRegisterTool] undefined registerTool(ModelContextTool tool);
13-
[RaisesException] undefined unregisterTool(DOMString name);
16+
[CallWith=ScriptState, RaisesException, MeasureAs=ModelContextRegisterTool] undefined registerTool(ModelContextTool tool, optional ModelContextRegisterToolOptions options = {});
1417
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ TEST_F(ModelContextTest, ForEachScriptToolGC) {
12441244
EXPECT_TRUE(found);
12451245

12461246
// Now unregister it.
1247-
model_context->unregisterTool("test_tool", ASSERT_NO_EXCEPTION);
1247+
model_context->UnregisterTool("test_tool");
12481248

12491249
// Trigger GC again. Now it should be reclaimed.
12501250
ThreadState::Current()->CollectAllGarbageForTesting();

‎third_party/blink/web_tests/external/wpt/webmcp/idlharness.https.window-expected.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
This is a testharness.js-based test.
2+
[FAIL] ModelContext interface: operation unregisterTool(DOMString)
3+
assert_own_property: interface prototype object missing non-static operation expected property "unregisterTool" missing
24
[FAIL] ModelContextClient interface: existence and properties of interface object
35
assert_own_property: self does not have own property "ModelContextClient" expected property "ModelContextClient" missing
46
[FAIL] ModelContextClient interface object length

‎third_party/blink/web_tests/external/wpt/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');

0 commit comments

Comments
 (0)