WebMCP
WebMCP 通过实验性的 WebMCP 浏览器 API navigator.modelContext,显示了一个框架注册的工具。它列出这些工具并调用它们。
可以通过 Frame.Webmcp 访问实例。Page.Webmcp 是主框架的实例。
🌐 Instances are accessed through Frame.Webmcp. Page.Webmcp is the instance of the main frame.
WebMCP 是一个实验性的浏览器功能。Chromium 使用 --enable-features=WebMCP 启动参数启用它,Firefox 使用 dom.modelcontext.enabled 和 dom.modelcontext.testing.enabled 偏好设置启用它。WebKit 没有实现它。
🌐 WebMCP is an experimental browser feature. Chromium enables it with the --enable-features=WebMCP launch argument, Firefox with the dom.modelcontext.enabled and dom.modelcontext.testing.enabled preferences. WebKit does not implement it.
工具名称、描述、输入格式和结果都是页面提供的,所以把它们当作不可靠的输入处理。
🌐 Tool names, descriptions, input schemas and results are provided by the page, so treat them as untrusted input.
方法
🌐 Methods
CallToolAsync
Added in: v1.64调用框架注册的工具并返回其结果。结果就是工具的 execute 函数解决的内容,通常是包含 content 数组的对象。带有 isError: true 的结果会原样返回。当工具未注册或其 execute 函数抛出异常时,该方法会抛出错误。
🌐 Calls a tool registered by the frame and returns its result. The result is whatever the tool's execute function resolved to, typically an object with a content array. A result with isError: true is returned as is. The method throws when the tool is not registered or its execute function throws.
用法
await WebMCP.CallToolAsync(name, input, options);
参数
-
工具的名称,如 WebMCP.ToolsAsync() 所报告的。
-
input[object]? (optional)#工具的输入,对应它的
inputSchema。默认是一个空对象。 -
optionsWebMCPCallToolOptions?(optional)-
Timeout[float]? (optional)#最长时间(以毫秒为单位)。默认值为
30000(30 秒)。传入0可禁用超时。默认值可以通过使用 BrowserContext.SetDefaultTimeout() 或 Page.SetDefaultTimeout() 方法进行更改。
-
返回
- [object]#
ToolsAsync
Added in: v1.64返回当前由框架注册的工具。如果浏览器是在没有 WebMCP 支持的情况下启动的,会抛出异常,详见上面的关于启用它的启动选项的说明。
🌐 Returns the tools currently registered by the frame. Throws if the browser was launched without WebMCP support, see the note above for the launch options that enable it.
Page.Webmcp covers the main frame only, child frames list their tools through their own Frame.Webmcp.
用法
await WebMCP.ToolsAsync(options);
参数
optionsWebMCPToolsOptions?(optional)-
Timeout[float]? (optional)#最长时间(以毫秒为单位)。默认值为
30000(30 秒)。传入0可禁用超时。默认值可以通过使用 BrowserContext.SetDefaultTimeout() 或 Page.SetDefaultTimeout() 方法进行更改。
-
返回
- IReadOnlyList<Tools>#