# WebMCP

> Expose your frontend tools to any compatible browser agent on your site, one tool at a time or all of them with one setting.

## Overview [#overview]

Browser agents that visit your site have to guess which buttons to click. WebMCP lets your site tell them what they can do. The agent discovers a named tool with a description, a typed input schema, and a JavaScript handler.

With CopilotKit, a [frontend tool](/angular/mastra/frontend-tools) you define can also be a WebMCP tool. Your own agent can call it. Any compatible browser agent that visits your site can call it too.

CopilotKit publishes the tool to [`document.modelContext`](https://webmachinelearning.github.io/webmcp/). The same handler runs for your agent and for browser agents. You write the tool once.

<Callout type="warn" title="WebMCP is experimental">
  WebMCP is a draft Community Group report, not a W3C Standard. Chrome offers
  it through an origin trial beginning in Chrome 149, or through the
  `chrome://flags/#enable-webmcp-testing` flag for local development. CopilotKit
  safely does nothing when `document.modelContext` is unavailable. Check the
  [current Chrome setup instructions](https://developer.chrome.com/docs/ai/webmcp)
  before testing.
</Callout>

## Start with your coding agent [#start-with-your-coding-agent]

Use this pre-built prompt to add WebMCP to your project. Your coding agent will adapt the implementation to your app and verify that a compatible browser can discover and call the tool.

<WebMCPSetupPrompt />

## Choose what to expose [#choose-what-to-expose]

WebMCP is off until you turn it on. You have two choices:

- **One tool at a time:** add `webmcp: true` to each tool. See [Expose one tool](#expose-one-tool).
- **Every tool:** set `defaultWebMCPTools` on the provider. Every tool you define is then available to all agents that visit your site, not only to your own agent. See [Expose every tool](#expose-every-tool).

A tool's own `webmcp` value always wins over `defaultWebMCPTools`.

### What `defaultWebMCPTools` exposes [#what-defaultwebmcptools-exposes]

When `defaultWebMCPTools` is on, CopilotKit registers a frontend tool with WebMCP when all of these are true:

- The tool has a `handler`.
- The tool has a non-empty `description`. WebMCP rejects tools without one.
- The tool is available. A tool with `available: false` is removed from WebMCP until it is available again.
- The tool does not set `webmcp: false`.

These tools are not included:

- **Human-in-the-loop tools.** Their handler waits for a response from UI in your chat, and a browser agent never sees that UI.
- **Render-only tools.** A tool with no `handler` has nothing for a browser agent to run.
- **CopilotKit's built-in tools**, such as the tool for Open Generative UI.

Do not set `webmcp: true` on a human-in-the-loop tool. The browser agent waits for a response that never comes. CopilotKit's built-in tools always stay private.

## Do I need an agent? [#do-i-need-an-agent]

You do not need a CopilotKit backend agent to handle a WebMCP call. A compatible browser agent calls the frontend tool handler directly on the page. In that path, the handler context has no `agent`.

Choose the smallest path that fits your app:

| What your app has today | What to do |
| --- | --- |
| A CopilotKit frontend tool for the action | Add `webmcp: true` to it, or turn on `defaultWebMCPTools`. |
| CopilotKit, but no frontend tool for the action | Wrap the smallest suitable browser-side action in a frontend tool. |
| A backend agent | Keep it. The tool stays available to that agent, but WebMCP calls do not pass through it. |
| No backend agent | Use `CopilotKitCore` directly in browser code, or add the appropriate frontend integration. Do not create an agent only for WebMCP. |

React Native does not expose `document.modelContext`, so WebMCP registration is a no-op there. CopilotKit supports WebMCP in its browser integrations for React, Vue, Angular, and direct core usage.

## How CopilotKit connects WebMCP [#how-copilotkit-connects-webmcp]

1. Your app registers a CopilotKit frontend tool that sets `webmcp: true` or `webmcp: { annotations }`, or `defaultWebMCPTools` is on.
2. CopilotKit mirrors the tool's name, description, JSON Schema, annotations, and handler onto `document.modelContext`.
3. A compatible browser agent discovers the tool and calls the same JavaScript handler while the page is open.
4. CopilotKit unregisters the WebMCP tool when the frontend tool is removed or becomes unavailable, and unregisters all tools when the provider unmounts.

The WebMCP call stays in the browser unless your handler calls an API. CopilotKit does not route that call through a Runtime or backend agent.

## Expose one tool [#expose-one-tool]

Add `webmcp: true` to the frontend tool. To tell browser agents how to treat the tool, pass annotations instead. For example, `readOnlyHint: true` says that the tool does not change anything.

### Angular frontend tool [#angular-frontend-tool]

Register the tool in an Angular injection context.

```ts title="src/app/order-search.component.ts"
import { Component } from "@angular/core";
import { registerFrontendTool } from "@copilotkit/angular";
import { z } from "zod";

@Component({
  selector: "app-order-search",
  standalone: true,
  template: "",
})
export class OrderSearchComponent {
  constructor() {
    registerFrontendTool({
      name: "searchOrders",
      description: "Search the signed-in user's orders by status",
      parameters: z.object({
        status: z.enum(["open", "shipped", "delivered"]),
      }),
      handler: async ({ status }) => {
        const orders = await searchOrders(status);
        return JSON.stringify(orders);
      },
      webmcp: {
        annotations: {
          readOnlyHint: true,
        },
      },
    });
  }
}
```

See the Angular [`registerFrontendTool` reference](/reference/angular/functions/registerFrontendTool) for the complete API.

### WebMCP only, with no agent [#webmcp-only-with-no-agent]

For browser code that does not use a framework provider, register the frontend tool directly with the core. Keep the core instance alive for as long as the tool must stay available.

```ts title="webmcp.ts"
import { CopilotKitCore } from "@copilotkit/core";
import { z } from "zod";

export const copilotkit = new CopilotKitCore({
  tools: [
    {
      name: "searchOrders",
      description: "Search the signed-in user's orders by status",
      parameters: z.object({
        status: z.enum(["open", "shipped", "delivered"]),
      }),
      handler: async ({ status }) => {
        const orders = await searchOrders(status);
        return JSON.stringify(orders);
      },
      webmcp: {
        annotations: {
          readOnlyHint: true,
        },
      },
    },
  ],
});
```

This path does not configure a Runtime or an agent. It only registers browser-side tools through the core. Call `copilotkit.stopWebMCP()` to unregister all of them, and `copilotkit.startWebMCP()` to register them again.

## Expose every tool [#expose-every-tool]

Set `defaultWebMCPTools` on the provider. Every tool that has a handler and a description becomes a WebMCP tool. See [What `defaultWebMCPTools` exposes](#what-defaultwebmcptools-exposes).

Before you turn it on, make sure that every handler checks its own authorization. Any browser agent on the page can then call every exposed tool.

```ts title="src/app/app.config.ts"
import { ApplicationConfig } from "@angular/core";
import { provideCopilotKit } from "@copilotkit/angular";

export const appConfig: ApplicationConfig = {
  providers: [
    provideCopilotKit({
      runtimeUrl: "/api/copilotkit",
      defaultWebMCPTools: true,
    }),
  ],
};
```

For direct core usage, pass `defaultWebMCPTools: true` to `new CopilotKitCore()`. To change it later, call `copilotkit.setDefaultWebMCPTools(false)` or `copilotkit.setDefaultWebMCPTools(true)`.

### Keep one tool private [#keep-one-tool-private]

Some tools must stay with your own agent. For example, a tool that deletes data can rely on your chat to ask the user first. A browser agent calls the handler directly and skips that step.

Add `webmcp: false` to the tool. The tool stays available to your own agent.

```tsx title="DeleteOrder.tsx"
import { useFrontendTool } from "@copilotkit/react-core/v2";
import { z } from "zod";

export function DeleteOrder() {
  useFrontendTool({
    name: "deleteOrder",
    description: "Delete one of the signed-in user's orders",
    parameters: z.object({ orderId: z.string() }),
    handler: async ({ orderId }) => {
      await deleteOrder(orderId);
      return "Deleted";
    },
    webmcp: false,
  });

  return null;
}
```

The option is the same for Vue `useFrontendTool`, Angular `registerFrontendTool`, and core tools.

## Design tools agents can use reliably [#design-tools-agents-can-use-reliably]

Required:

- Give every tool a non-empty, specific `description`. CopilotKit does not register a tool with WebMCP without one.
- Use `readOnlyHint: true` only when the handler cannot change state.
- Set the frontend tool to unavailable when the action cannot run now. CopilotKit removes it from WebMCP until it is available again.

For better results:

- Keep each tool focused on one action, and give every parameter a useful description.
- Use `untrustedContentHint: true` when results can contain user-generated or external content.
- Return concise, structured results that tell the caller what happened.

For more, see Chrome's [WebMCP best practices](https://developer.chrome.com/docs/ai/webmcp/best-practices).

<Callout type="warn" title="Protect every handler">
  Any compatible browser agent on your page can call an exposed tool.
  Annotations are hints, not security controls. Enforce authentication,
  authorization, validation, rate limits, and user confirmation inside the
  handler or the API that it calls. See Chrome's [WebMCP tool security
  guidance](https://developer.chrome.com/docs/ai/webmcp/secure-tools).
</Callout>

<Callout type="warn" title="Agent scope does not scope WebMCP">
  `agentId` limits which CopilotKit agent receives a frontend tool. WebMCP tools
  are page-level, so `agentId` does not restrict browser-agent access. If
  multiple tools share a name across agent IDs, CopilotKit exposes only one.
  A tool that opts in with `webmcp: true` or `webmcp: { annotations }` wins
  over one that only follows `defaultWebMCPTools`. Otherwise, the first one wins.
  `webmcp: false` keeps only that tool private. Another eligible tool with the
  same name is still exposed.
</Callout>

## Test the complete path [#test-the-complete-path]

Set up the browser:

1. Open Chrome 149 or newer and enable the WebMCP origin trial or local testing flag.
2. Load the app in an [origin-isolated document](https://developer.chrome.com/docs/ai/webmcp#origin-isolation). The `tools` permissions policy defaults to `self`. Cross-origin iframes also need `allow="tools"`.
3. Make sure that `document.modelContext` exists.

Test the tool:

1. Open Chrome's [Model Context Tool Inspector](https://developer.chrome.com/docs/ai/webmcp#imitate-agent-chat-with-the-inspector-extension).
2. Make sure that your tool names and schemas appear.
3. Call a tool with representative inputs and read its result.
4. Ask the inspector's agent to complete the user task in natural language.
5. Make sure that the agent chooses the right tool, and that authentication and confirmation boundaries still hold.

If a tool does not appear, make sure that:

1. The browser setup is complete.
2. The tool has a `description`.
3. The tool is available.
4. The tool sets `webmcp: true`, or `defaultWebMCPTools` is on and the tool is in [its set](#what-defaultwebmcptools-exposes).

When your tools appear in the inspector and its agent can call them, your site works with any WebMCP-aware browser agent.
