跳到正文
本页尚未翻译成中文,以下为英文原文。 查看英文版

WebMCP ​

Added in: v1.64

WebMCP exposes the tools that a frame registers through the experimental WebMCP browser API, navigator.modelContext. It lists the tools and calls them.

Instances are accessed through frame.webmcp. page.webmcp is the instance of the main frame.

INFO

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.

js
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 ​

webMCP.callTool() ​

Added in: v1.64 async

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.

js
const result = await page.webmcp.callTool('add', { a: 2, b: 40 });
console.log(result.content[0].text); // "42"

Arguments

  • name string

    Name of the tool, as reported by webMCP.tools().

  • input Serializable (optional)

    Input for the tool, matching its inputSchema. Defaults to an empty object.

  • options Object (optional)

Returns

  • Promise<Serializable>

webMCP.tools() ​

Added in: v1.64 async

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.

Arguments

Returns

  • Promise<Array<Object>>

基于 Apache 2.0 许可证发布。