# `Playwright.WebMCP`
[🔗](https://github.com/ftes/playwright_ex/blob/v1.65.0-rc.3/lib/playwright/api/web_mcp.ex#L7)

[`WebMCP`](`Playwright.WebMCP`) exposes the tools that a frame registers through the experimental [`WebMCP`](`Playwright.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 {: .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.

```elixir
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}`).

# `call_tool_options`

```elixir
@type call_tool_options() ::
  [{:timeout, number() | :infinity}]
  | %{optional(:timeout) =&gt; number() | :infinity}
```

Options of `call_tool/4`, see its docs.

# `t`

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

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

# `tools_options`

```elixir
@type tools_options() ::
  [{:timeout, number() | :infinity}]
  | %{optional(:timeout) =&gt; number() | :infinity}
```

Options of `tools/2`, see its docs.

# `call_tool`

```elixir
@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.

```elixir
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

* `:timeout` (`t:number/0` | `:infinity`) - Maximum time in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. The default value can be changed by
  using the `Playwright.BrowserContext.set_default_timeout/2` or
  `Playwright.Page.set_default_timeout/2` methods.

  In Elixir, `:infinity` is the same as `0`: no timeout.

# `call_tool!`

```elixir
@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`

```elixir
@spec tools(t(), tools_options()) ::
  {:ok,
   [
     %{
       :name =&gt; String.t(),
       :description =&gt; String.t(),
       optional(:inputSchema) =&gt; term(),
       optional(:annotations) =&gt; %{
         optional(:readOnly) =&gt; boolean(),
         optional(:untrustedContent) =&gt; boolean(),
         optional(:consequential) =&gt; 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

* `:timeout` (`t:number/0` | `:infinity`) - Maximum time in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. The default value can be changed by
  using the `Playwright.BrowserContext.set_default_timeout/2` or
  `Playwright.Page.set_default_timeout/2` methods.

  In Elixir, `:infinity` is the same as `0`: no timeout.

# `tools!`

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

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
