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.
const browser = await chromium.launch({ args: ['--enable-features=WebMCP'] });
const page = await browser.newPage();
await page.goto('https://example.com');
for (const tool of await page.webmcp.tools())
console.log(tool.name, tool.description);
const result = await page.webmcp.callTool('add', { a: 2, b: 40 });
方法
🌐 Methods
callTool
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.
const result = await page.webmcp.callTool('add', { a: 2, b: 40 });
console.log(result.content[0].text); // "42"
用法
await webMCP.callTool(name);
await webMCP.callTool(name, input, options);
参数
-
工具的名称,如 webMCP.tools() 报告的那样。
-
inputSerializable (optional)#工具的输入,对应它的
inputSchema。默认是一个空对象。 -
optionsObject (optional)-
最大时间(毫秒)。默认为
0- 无超时。默认值可以通过配置中的actionTimeout选项更改,或者使用 browserContext.setDefaultTimeout() 或 page.setDefaultTimeout() 方法更改。
-
返回
tools
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.tools();
await webMCP.tools(options);
参数
optionsObject (optional)-
最大时间(毫秒)。默认为
0- 无超时。默认值可以通过配置中的actionTimeout选项更改,或者使用 browserContext.setDefaultTimeout() 或 page.setDefaultTimeout() 方法更改。
-
返回
- Promise<Array<Object>>#
-
namestring工具名称,在框架内唯一。
-
descriptionstring工具说明。
-
inputSchemaSerializable (optional)当页面提供时,工具输入的 JSON 架构。
-
annotationsObject (optional)-
readOnlyboolean (optional)这个工具不会修改任何状态。
-
untrustedContentboolean (optional)工具输出可能包含第三方内容。
-
consequentialboolean (optional)这个工具会采取一个后续动作,比如下订单。
这个页面提供的关于工具的小提示。
-
-