Introduce the `Tool` interface

Merged
DO
12d ago
+267-136

This PR introduces the Tool interface which encapsulates a tool's new UUID. Tools are now keyed and identified by this ID. This replaces the RegisteredTool dictionary with the new Tool interface, fixes a number of possible race conditions (such as https://webmachinelearning.github.io/webmcp/#unregistration-execution-race), and also unblocks us from being able to execute registered by opaque origin documents (see #349).

Note that there are some compatibility changes with this:

  • RegisteredTool#inputSchema and RegisteredTool#annotations could be undefined before this PR, where as Tool#inputSchema and Tool#annotations are not object? which makes more sense for an interface object. This means these two members can be null when their respective values are not provided. They are never purely undefined now.
  • JSON.stringify(tool) returns {}, and the spread operator copies nothing.
  • structuredClone(tool) throws, since this type is not yet [Serializable].

This makes some progress towards the discussion in #236 (/cc @jakearchibald), and helps us out with future worker integration.

/cc @nmccormack-oai from who was interested in this fix from an agent's perspective. Note that the spec still doesn't define a native-agent-initiated tool execution path (equivalent to CDP-initiated tool execution in Chromium, or Gemini-in-Chrome/"actor stack"-initiated tool execution in branded Chrome), but that's coming soon, and the changes here are required to support the correctness / race fixes in those areas too.

Closes https://github.com/webmachinelearning/webmcp/issues/385.


Preview | Diff