Playwright.WebMCP (Playwright v1.65.0-rc.3)

Copy Markdown View Source

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 Playwright.Frame.webmcp/1. Playwright.Page.webmcp/1 is the instance of the main frame.

Note

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.

browser = Playwright.chromium(playwright) |> BrowserType.launch!(args: ["--enable-features=WebMCP"])
page = Browser.new_page!(browser)
Page.goto!(page, "https://example.com")

for tool <- Page.webmcp(page) |> WebMCP.tools!() do
  IO.puts("#{tool.name} #{tool.description}")
end

result = Page.webmcp(page) |> WebMCP.call_tool!("add", %{a: 2, b: 40})

Since Playwright 1.64.

The examples alias the modules they use (alias Playwright.{Locator, Page}).

Summary

Types

Options of call_tool/4, see its docs.

t()

A WebMCP handle. Compare handles with ==, their fields are internal.

Options of tools/2, see its docs.

Functions

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 fails when the tool is not registered or its execute function throws.

Like call_tool/4, but returns the value and raises on error.

Returns the tools currently registered by the frame. Fails if the browser was launched without WebMCP support, see the note above for the launch options that enable it.

Like tools/2, but returns the value and raises on error.

Types

call_tool_options()

@type call_tool_options() ::
  [{:timeout, number() | :infinity}]
  | %{optional(:timeout) => number() | :infinity}

Options of call_tool/4, see its docs.

t()

@type t() :: %Playwright.WebMCP{_frame: term()}

A WebMCP handle. Compare handles with ==, their fields are internal.

tools_options()

@type tools_options() ::
  [{:timeout, number() | :infinity}]
  | %{optional(:timeout) => number() | :infinity}

Options of tools/2, see its docs.

Functions

call_tool(web_mcp, name, input \\ nil, options \\ [])

@spec call_tool(t(), String.t(), term() | nil, call_tool_options()) ::
  {:ok, term()} | {:error, Playwright.error()}

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 fails when the tool is not registered or its execute function throws.

result = Page.webmcp(page) |> WebMCP.call_tool!("add", %{a: 2, b: 40})
# prints 42
IO.puts(hd(result["content"])["text"])

Since Playwright 1.64.

Arguments

  • name - Name of the tool, as reported by Playwright.WebMCP.tools/2.
  • input - Input for the tool, matching its inputSchema. Defaults to an empty object.

Options

call_tool!(web_mcp, name, input \\ nil, options \\ [])

@spec call_tool!(t(), String.t(), term() | nil, call_tool_options()) :: term()

Like call_tool/4, but returns the value and raises on error.

tools(web_mcp, options \\ [])

@spec tools(t(), tools_options()) ::
  {:ok,
   [
     %{
       :name => String.t(),
       :description => String.t(),
       optional(:inputSchema) => term(),
       optional(:annotations) => %{
         optional(:readOnly) => boolean(),
         optional(:untrustedContent) => boolean(),
         optional(:consequential) => boolean()
       }
     }
   ]}
  | {:error, Playwright.error()}

Returns the tools currently registered by the frame. Fails if the browser was launched without WebMCP support, see the note above for the launch options that enable it.

Playwright.Page.webmcp/1 covers the main frame only, child frames list their tools through their own Playwright.Frame.webmcp/1.

Since Playwright 1.64.

Options

tools!(web_mcp, options \\ [])

@spec tools!(t(), tools_options()) :: [
  %{
    :name => String.t(),
    :description => String.t(),
    optional(:inputSchema) => term(),
    optional(:annotations) => %{
      optional(:readOnly) => boolean(),
      optional(:untrustedContent) => boolean(),
      optional(:consequential) => boolean()
    }
  }
]

Like tools/2, but returns the value and raises on error.