# Tools for the AI in the browser (WebMCP)

Browsers are getting their own agents: you ask the browser to do something on a page and it does it. Without help, an agent reads the page (a screenshot or the accessibility tree), clicks and types, and reads the page again after every step. WebMCP lets the page hand the agent tools instead: `create_note`, `edit_note`, `board_move_card`. The agent calls them directly, with the right input, and gets a short answer back.

Hatch gives tools from three places:

- **The app.** Search, read and list notes, the open note and what's selected, open, create and edit notes, list and run commands, and every plugin's agent tools.
- **The shadcn components.** The board, chat, search and editor each give their own while they're on screen.
- **Your components.** `useWebMcpTools` from `@hatchmd/react`, or `registerTools` from `@hatchmd/webmcp` outside React.

## Turning it on

WebMCP is in Chrome behind a flag and in an origin trial. To try it, open `chrome://flags/#enable-webmcp-testing`, set it to Enabled and restart Chrome. In a browser without WebMCP, nothing changes: no tools, no switch, no extra code loaded.

People can turn Hatch's tools off in Settings, Privacy ("Let your browser's AI use Hatch"). The switch shows only in a browser that has WebMCP, and other open tabs follow it. It's on by default: the tools only run when the person asks their browser's agent to do something, and every change can be undone.

## The app's tools

| Tool | What it does | Notes |
|---|---|---|
| `search_notes`, `read_note`, `list_notes`, `backlinks` | Read the vault | The chat's own tools, the same names as the MCP server's |
| `get_open_note` | The note on screen, its Markdown and the selected text | For "this note" |
| `open_note` | Show a note | By path or name |
| `create_note` | Make a note and open it | Fails if it's already there |
| `edit_note` | Find and replace, append, or rewrite a note | Goes in like an accepted chat change: undo takes it back, History keeps the version before |
| `list_commands`, `run_command` | The command palette's commands, by id | Only commands on a list in `webmcp.ts`: ones that show something, make something new, or change a note in a way undo takes back. Nothing that sends, publishes, exports, imports, unlocks or switches vaults, and no plugin commands (plugins give tools of their own). A new command stays off until it's added |
| `<plugin>__<tool>` | Each plugin's agent tools | The same ones the AI chat gets |

Reading tools say `readOnlyHint` and `untrustedContentHint` (notes are text people wrote, which the agent should treat as data). Nothing Hatch gives is `consequentialHint`, because everything can be undone. The code is `packages/app/src/ai/webmcp.ts`, loaded only in a browser with WebMCP.

## Your own tools

```tsx
import { useBoard, useWebMcpTools } from '@hatchmd/react'

function Board() {
  const { columns, moveTask } = useBoard('Website')
  useWebMcpTools([
    {
      name: 'board_move_card',
      description: 'Move a card to another column',
      inputSchema: { type: 'object', properties: { path: { type: 'string' }, status: { type: 'string', enum: columns.map((c) => c.status) } }, required: ['path', 'status'] },
      execute: ({ path, status }) => moveTask(path, status),
    },
  ])
  // ...
}
```

Tools are registered while the component is on screen and registered again only when a name, description or schema changes. Each call runs the `execute` from the latest render, so it sees current state.

Outside React:

```ts
import { registerTools } from '@hatchmd/webmcp'

const stop = await registerTools(tools, {
  // Before every call. Return false to refuse it
  allow: (call) => !call.consequential || confirm(`Let the browser's AI ${call.name}?`),
})
stop() // takes them back
```

Good tools, from what we found writing these:

- **One tool per thing a person would do**, named with a verb (`board_add_card`), described by what it does and when to use it.
- **Throw with the next step in the message.** "No note called "Cabn". Use search_notes to find it." `registerTools` turns a throw into an answer starting `Error: `, because Chrome gives the agent only "Tool execution failed" for a thrown error.
- **Answer with what the agent needs next**, like the new card's path, not a whole page of state.
- **Annotate honestly.** `readOnlyHint` only if nothing changes, `untrustedContentHint` on anything people wrote, `consequentialHint` on anything that can't be undone.
- **All or nothing.** `registerTools` registers a batch at once and takes it all back if one fails, so the agent never sees half a workflow.

## The benchmark

`bench/webmcp` runs the same task in Hatch with Claude Code three ways: the usual browser tools (Playwright MCP) in a browser without WebMCP, the same browser tools in a browser with it (Playwright MCP then lists the page's tools too, as `webmcp_<name>`), and only Hatch's WebMCP tools. The task: make a note with a checklist in a folder, add a link to it at the end of another note, and switch to the dark theme. Every run starts from a fresh vault and is checked against the files afterwards.

Results on 2026-12-04, Claude Sonnet through Claude Code, medians of three runs each. Every run finished the task correctly. Tokens count everything the model read and wrote, cached reads included. The raw runs are in `bench/webmcp/results.json`.

| | Browser tools, no WebMCP | Browser tools + WebMCP | WebMCP only |
|---|---|---|---|
| Tool calls | 34 | 9 | 6 |
| Text the tools returned | 37,166 characters | 13,932 | 714 |
| Time | 51 s | 18 s | 7.5 s |
| Tokens | 458,689 | 126,208 | 29,475 |
| Cost | $0.152 | $0.059 | $0.014 |

With only WebMCP, the agent made 82% fewer tool calls, used 94% fewer tokens, cost 91% less and finished 85% sooner than with browser tools alone. Most of the browser runs went on reading the page again after each click (8 snapshots and 10 clicks a run). With both kinds of tools, the agent still took one page snapshot and a couple of clicks before it found the page's own tools, which is most of the gap between the last two columns.

One task on one app, and a task Hatch's tools cover well (each step is one tool), so read it as a direction, not a law. A task the tools don't cover would leave the agent clicking for that part. Medians count only runs that did the task right; failed ones show in the first row of the script's table. The same script runs any task: change `TASK` and `verify` in `run.mjs`.

To run it, build and serve the web app, then:

```sh
(cd apps/web && npx vite build --outDir /tmp/hatch-web && npx vite preview --outDir /tmp/hatch-web --port 4390)
node bench/webmcp/run.mjs http://localhost:4390 --runs 3 --out bench/webmcp/results.json
```

It needs Claude Code signed in and Playwright's Chromium. `--model` picks the model (Sonnet unless you say), `--modes browser,mixed,webmcp` picks the ways.

## Checking it

- `npx vitest run packages/webmcp`
- `python3 scripts/e2e-webmcp.py <url>`: the app's tools in Chromium with WebMCP on, the Privacy switch, and a browser without it
- `python3 scripts/e2e-shadcn.py`: the components' tools
