diff --git a/README.md b/README.md index ae92828..abab03b 100644 --- a/README.md +++ b/README.md @@ -347,13 +347,15 @@ await document.modelContext.registerTool({ Any document in the tree matching these origins (and allowed to use `tools` permission) will: - Receive the `toolchange` event on its `document.modelContext` when the tool is registered or unregistered. -- Be able to discover and run the tools +- Be able to discover the tools (for cross-origin discovery, the consumer's `fromOrigins` must include the provider's origin or `"*"`), and run them subject to execution checks. #### Discovering and running tools: `getTools()` and `executeTool()` Once tools are registered, in-page agents can discover and invoke them using `getTools()` and `executeTool()`. -Calling `document.modelContext.getTools()` returns a promise that resolves with an array of [`Tool`](https://webmachinelearning.github.io/webmcp/#tool) objects. Each `Tool` exposes the tool's `name`, `title`, `description`, `inputSchema`, `outputSchema`, `annotations`, `origin`, and `source` (the owner window). By default, `getTools()` only returns tools registered by documents same-origin with the caller in the frame tree. To retrieve cross-origin tools, you must explicitly list their origins in the `fromOrigins` option. This array only supports secure origins. +Calling `document.modelContext.getTools()` returns a promise that resolves with an array of [`Tool`](https://webmachinelearning.github.io/webmcp/#tool) objects. Each `Tool` exposes the tool's `name`, `title`, `description`, `inputSchema`, `outputSchema`, `annotations`, `origin`, and `source` (the owner window). By default, `getTools()` only returns tools registered by documents same-origin with the caller in the frame tree. To retrieve cross-origin tools, you must explicitly list their origins in the `fromOrigins` option, or use `{ fromOrigins: ["*"] }` to query all origins in that tree, including opaque origins. The API's secure-context restriction still applies. + +In `fromOrigins`, only the exact `"*"` string is special. It does not bypass Permissions Policy or execution checks. Discovery still requires the provider's explicit `exposedTo` origin list to allow the caller. A wildcard opts into any eligible origin, not just origins trusted by the author; wildcard consumers may discover tools with untrusted descriptions. Prefer explicit origin lists when possible. Other entries are still validated even when `"*"` is present. ```js // Discover tools exposed by same-origin frames in the tree (default) @@ -371,9 +373,14 @@ for (const tool of tools) { const crossOriginTools = await document.modelContext.getTools({ fromOrigins: ["https://trusted-partner.example"] }); + +// Discover tools exposed to the caller by frames of any origin: +const allOriginTools = await document.modelContext.getTools({ + fromOrigins: ["*"] +}); ``` -An agent executes a discovered `Tool` by passing it and input arguments along to `document.modelContext.executeTool()`. The browser securely mediates the execution, ensuring the [`exposedTo`](https://webmachinelearning.github.io/webmcp/#dom-modelcontextregistertooloptions-exposedto) and [`fromOrigins`](https://webmachinelearning.github.io/webmcp/#dom-modelcontextgettooloptions-fromorigins) agree, and the tool runs in the tool owner's execution context. `Tool` objects can only be obtained from `getTools()`, and each one refers to the specific tool registration it was returned for: if that tool is unregistered, executing the `Tool` fails, even if a new tool is later registered under the same name. +An agent executes a discovered `Tool` by passing it and input arguments along to `document.modelContext.executeTool()`. The browser securely mediates the execution, checking [`exposedTo`](https://webmachinelearning.github.io/webmcp/#dom-modelcontextregistertooloptions-exposedto) again, and the tool runs in the tool owner's execution context. [`fromOrigins`](https://webmachinelearning.github.io/webmcp/#dom-modelcontextgettooloptions-fromorigins) filters discovery, not execution. `Tool` objects can only be obtained from `getTools()`, and each one refers to the specific tool registration it was returned for: if that tool is unregistered, executing the `Tool` fails, even if a new tool is later registered under the same name. ```js const tools = await document.modelContext.getTools(); diff --git a/index.bs b/index.bs index 95d6872..4e14754 100644 --- a/index.bs +++ b/index.bs @@ -175,6 +175,15 @@ An AI platform is a provider of agentic assistants such as OpenAI’s

Supporting concepts

+An origin wildcard is the [=string=] "`*`", used in +{{ModelContextGetToolOptions/fromOrigins}} to request discovery from all origins. + +In {{ModelContextGetToolOptions/fromOrigins}}, +only the exact string "`*`" is an [=origin wildcard=]. All other entries are parsed and validated +as origins, even when the array also contains an [=origin wildcard=]. + +Issue(#317): Consider supporting {{URLPattern}} objects for more selective origin matching. + A model context is a [=struct=] with the following [=struct/items=]:
@@ -1066,10 +1075,18 @@ The getTools(options) method steps 1. Let |from origins| be an empty [=list=] of [=origins=]. +1. Let |includeAllOrigins| be false. + 1. If |options|'s {{ModelContextGetToolOptions/fromOrigins}} [=map/exists=], then: 1. [=list/For each=] |origin| of |options|'s {{ModelContextGetToolOptions/fromOrigins}}: + 1. If |origin| is the [=origin wildcard=], then: + + 1. Set |includeAllOrigins| to true. + + 1. [=iteration/continue=]. + 1. Let |parsedURL| be the result of running the [=URL parser=] on |origin|. 1. If |parsedURL| is failure or its [=url/origin=] is not [$is origin potentially @@ -1101,7 +1118,8 @@ The getTools(options) method steps 1. Let |callerOrigin| be |toolRequestor|'s [=Document/origin=]. 1. Let |toolOwnerIsRequested| be true if |targetOrigin| is [=same origin=] with - |callerOrigin|, or if |from origins| [=list/contains=] |targetOrigin|; otherwise, false. + |callerOrigin|, |from origins| [=list/contains=] |targetOrigin|, or |includeAllOrigins| + is true; otherwise, false. 1. If |toolOwnerIsRequested| is false, then [=iteration/continue=]. @@ -1472,7 +1490,9 @@ dictionary ModelContextRegisterToolOptions {
: options["{{ModelContextRegisterToolOptions/exposedTo}}"] - :: An array of origins that control which documents this tool is exposed to, in the current document's tree. + :: An array of origin strings that control which documents this tool is exposed to, in the + current document's tree. + An omitted or empty array only permits same-origin access by documents. : options["{{ModelContextRegisterToolOptions/signal}}"] :: An {{AbortSignal}} that unregisters the tool when aborted. @@ -1493,9 +1513,22 @@ dictionary ModelContextGetToolOptions { : options["{{ModelContextGetToolOptions/fromOrigins}}"] :: An array of origins from which to query tools. Documents whose origin appears in this list, or are same-origin with the caller, have their tools queried. An empty list only includes - same-origin documents. + same-origin documents. The [=origin wildcard=] queries documents regardless of their origin, + including [=opaque origins=], in the current document's tree. + Tools must still be exposed to the caller, and documents must be [=allowed to use=] the + "{{tools}}" feature.
+
+ Use the [=origin wildcard=] to discover tools exposed to the caller by documents of any origin + in the current document's tree: + + ```js + const tools = await document.modelContext.getTools({ fromOrigins: ["*"] }); + ``` + +
+

ModelContextExecuteToolOptions Dictionary

The {{ModelContextExecuteToolOptions}} dictionary allows web applications to pass options to @@ -2271,9 +2304,9 @@ operations or extract sensitive state from another [=origin=]. By default, WebMCP enforces strict [=same origin=] isolation: a tool registered via document.{{Document/modelContext}}.{{ModelContext/registerTool()}} is only visible to and executable by {{Document}}s that are [=same origin=] with the registering -{{Document}} (tool owner). Cross-origin {{Document}}s cannot discover, observe -{{ModelContext/toolchange}} events for, or execute a tool unless three independent access-control -gates are satisfied: +{{Document}} (tool owner). Cross-origin tool discovery requires three independent access-control +gates. Tool invocation and {{ModelContext/toolchange}} notifications require the first two gates; +{{ModelContextGetToolOptions/fromOrigins}} filters discovery, not invocation or notifications:
  1. @@ -2289,9 +2322,10 @@ gates are satisfied:
  2. Tool Provider Opt-In ({{ModelContextRegisterToolOptions/exposedTo}}): A {{Document}} registering a tool must explicitly opt in to cross-origin exposure by specifying - the permitted caller [=origins=] in {{ModelContextRegisterToolOptions/exposedTo}}. Each listed - [=origin=] must be a valid, [$is origin potentially trustworthy?|potentially trustworthy$] - [=origin=]. During both discovery ({{ModelContext/getTools()}}) and invocation + the permitted caller [=origins=] in + {{ModelContextRegisterToolOptions/exposedTo}}. Each entry + must be a valid, [$is origin potentially trustworthy?|potentially trustworthy$] [=origin=]. + During both discovery ({{ModelContext/getTools()}}) and invocation ({{ModelContext/executeTool()}}), the [=user agent=] evaluates the [=tool is exposed to an origin=] algorithm, ensuring that only callers whose [=Document/origin=] is [=same origin=] with the tool owner or explicitly listed in {{ModelContextRegisterToolOptions/exposedTo}} can access @@ -2303,14 +2337,15 @@ gates are satisfied: a tool to its parent [=origin=] via {{ModelContextRegisterToolOptions/exposedTo}}, {{ModelContext/getTools()}} only returns tools from [=same origin=] {{Document}}s by default. To discover tools from a cross-origin descendant [=navigable=], the calling {{Document}} must also - explicitly list the target [=origin=] in {{ModelContextGetToolOptions/fromOrigins}}. + explicitly list the target [=origin=] or the [=origin wildcard=] in + {{ModelContextGetToolOptions/fromOrigins}}.
-**Why Two-Sided Opt-In Matters**: Requiring mutual consent from both the tool provider +**Why Two-Sided Opt-In Matters**: Requiring opt-in from both the tool provider ({{ModelContextRegisterToolOptions/exposedTo}}) and the tool consumer -({{ModelContextGetToolOptions/fromOrigins}}) prevents both unauthorized cross-origin actuation and -unsolicited tool-list pollution. Without provider opt-in +({{ModelContextGetToolOptions/fromOrigins}}) lets each restrict the origins involved in discovery. +Provider opt-in also protects against unauthorized cross-origin actuation. Without provider opt-in ({{ModelContextRegisterToolOptions/exposedTo}}), a malicious parent page could invoke internal tools inside an embedded third-party widget that were intended only for [=same origin=] use. Conversely, without consumer opt-in ({{ModelContextGetToolOptions/fromOrigins}}), a compromised or untrusted @@ -2341,6 +2376,12 @@ await document.modelContext.registerTool( ); ``` +The [=origin wildcard=] opts into all origins in the document's tree, not only origins trusted by +the author. Authors should prefer explicit origin lists when possible. In particular, a consumer +using the [=origin wildcard=] accepts tools exposed to it by any eligible document in that tree. +The provider's {{ModelContextRegisterToolOptions/exposedTo}} origin list and the "{{tools}}" +Permissions Policy still apply. +
Residual Risks
Even when cross-origin access is governed by the mechanisms above, two categories of residual risk @@ -2348,7 +2389,7 @@ remain: