Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ export default extendConfig(
{ text: 'Head Management', link: '/guide/pages-routing/head' },
{ text: 'Islands', link: '/guide/pages-routing/islands' },
{ text: 'Markdown', link: '/guide/pages-routing/markdown' },
{ text: 'WebMCP', link: '/guide/pages-routing/webmcp' },
],
},
{
Expand Down
235 changes: 235 additions & 0 deletions docs/guide/pages-routing/webmcp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
---
description: 'Let AI agents in the browser use your pages through WebMCP tools: submit forms, read page data, and navigate.'
outline: deep
---

# WebMCP

[WebMCP](https://webmachinelearning.github.io/webmcp/) lets a page offer tools to AI agents running in the browser, such as a browser assistant. A tool has a name, a description, and a JSON Schema for its input. The agent calls it instead of clicking through the page. Tools run in the page, with the user's session, and change the same UI the user sees.

The `@void/webmcp` package offers three kinds of tools for Void Pages:

- **Form tools**: `useFormTool()` lets an agent fill in a `useForm()` form and submit it through the page action.
- **Component tools**: `useAgentTool()` registers a tool while a component is mounted.
- **A `navigate` tool**: opens any page in your app.

::: warning Experimental
WebMCP is an early web standard. Chrome supports it behind a flag and in an origin trial; other browsers do not support it yet. Where WebMCP is unavailable, these APIs do nothing, so you can ship them now.
:::

## Setup

Install the package:

```sh
npm install @void/webmcp
```

Add `voidWebMcp()` to your Vite plugins:

```ts
// vite.config.ts
import { voidReact } from '@void/react/plugin'; // or voidVue, voidSvelte, voidSolid
import { voidWebMcp } from '@void/webmcp/plugin';
import { defineConfig } from 'vite-plus';
import { voidPlugin } from 'void';

export default defineConfig({
plugins: [voidPlugin(), voidReact(), voidWebMcp()],
});
```

The plugin offers the [`navigate` tool](#navigation-tool). Form and component tools come from the framework entries: `@void/webmcp/react`, `@void/webmcp/vue`, `@void/webmcp/svelte`, and `@void/webmcp/solid`.

## Trying it locally

In Chrome, enable `chrome://flags/#enable-webmcp-testing`, or start Chrome with `--enable-features=WebMCP`. Then use an agent that supports WebMCP, such as Chrome's Model Context Tool Inspector extension, which lists the page's tools and lets you call them.

To offer tools to visitors before WebMCP ships, register your origin for the [WebMCP origin trial](https://developer.chrome.com/origintrials/) and serve the token it gives you.

## Form tools

Pass a `useForm()` form to `useFormTool()` to let agents submit it:

```ts
import { useForm } from '@void/react'; // or @void/vue, @void/svelte, @void/solid
import { useFormTool } from '@void/webmcp/react'; // or /vue, /svelte, /solid

const form = useForm('/users', { name: '', email: '' });
useFormTool(form, {
name: 'add_user',
description: 'Add a person to the user directory. Use when the user asks to add someone.',
});
```

When an agent calls `add_user`:

1. The form is filled in with the agent's values, so the user sees them.
2. The form is submitted through the page action, like a user submit. `pending`, `onSuccess`, and `onError` behave the same way.
3. The agent receives the result. Validation errors from the action appear in the form and are sent to the agent, so it can correct its input and try again.

The tool's input schema comes from the form's defaults. Strings, numbers, booleans, arrays, and objects are described; file fields are left out, so agents cannot fill them in. Agents can send only some fields; the others keep their current values.

| Option | Description |
| ------------- | ------------------------------------------------------------------------------------------------------------ |
| `name` | Tool name: 1 to 128 ASCII letters, digits, `_`, `-`, or `.`. Unique on the page. |
| `description` | What submitting the form does, and when an agent should do it. |
| `title` | A human-readable name for the tool. |
| `method` | The method the form submits with. Defaults to `"post"`. |
| `confirm` | Fill in the form without submitting it. The agent is told that the user must review and submit it. |
| `input` | A [Standard Schema](https://standardschema.dev) that validates the agent's values before the form is filled. |
| `inputSchema` | A JSON Schema for the fields, when the defaults do not describe them well enough. |
| `annotations` | Hints for the agent; see [annotations](#annotations). |
| `enabled` | `false`, or a function returning `false`, makes the tool reject calls, for example while a form is locked. |

Use `confirm: true` for forms whose effects the user should approve, such as payments or deleting data:

```ts
const form = useForm('/checkout', { address: '', quantity: 1 });
useFormTool(form, {
name: 'prepare_order',
description: 'Fill in the checkout form for the user to review.',
confirm: true,
});
```

::: tip
An agent acts with the signed-in user's permissions, through the same action as the user. Keep validating and authorizing in the action; `input` only gives the agent earlier feedback.
:::

## Component tools

`useAgentTool()` offers a tool while the component is mounted. It is removed when the component unmounts, for example when the user navigates to another page.

::: code-group

```tsx [React]
import { useAgentTool } from '@void/webmcp/react';
import type { Props } from './index.server.ts';

export default function UsersPage({ users }: Props) {
useAgentTool({
name: 'list_users',
description: 'List the people in the user directory with their ids.',
annotations: { readOnlyHint: true },
execute: () => users,
});
// ...
}
```

```vue [Vue]
<script setup lang="ts">
import { useAgentTool } from '@void/webmcp/vue';
import type { Props } from './index.server.ts';

const props = defineProps<Props>();

useAgentTool({
name: 'list_users',
description: 'List the people in the user directory with their ids.',
annotations: { readOnlyHint: true },
execute: () => props.users,
});
</script>
```

```svelte [Svelte]
<script lang="ts">
import { useAgentTool } from '@void/webmcp/svelte';
import type { Props } from './index.server.ts';

let { users }: Props = $props();

useAgentTool({
name: 'list_users',
description: 'List the people in the user directory with their ids.',
annotations: { readOnlyHint: true },
execute: () => users,
});
</script>
```

```tsx [Solid]
import { useAgentTool } from '@void/webmcp/solid';
import type { Props } from './index.server.ts';

export default function UsersPage(props: Props) {
useAgentTool({
name: 'list_users',
description: 'List the people in the user directory with their ids.',
annotations: { readOnlyHint: true },
execute: () => props.users,
});
// ...
}
```

:::

`execute` reads the current props and state each time it is called.

### Tool input

Pass a Standard Schema as `input` to validate calls. Schemas that support [Standard JSON Schema](https://standardschema.dev/json-schema), such as Zod and ArkType, also describe the tool's parameters to the agent. Otherwise, set `inputSchema` to a JSON Schema yourself.

```ts
import { useRouter } from '@void/react'; // or @void/vue, @void/svelte, @void/solid
import { useAgentTool } from '@void/webmcp/react'; // or /vue, /svelte, /solid
import { z } from 'zod';

const router = useRouter();

useAgentTool({
name: 'search_products',
description: 'Search the product catalog and show the results to the user.',
input: z.object({ query: z.string().describe('Words to search for') }),
async execute({ query }) {
await router.visit(`/products?q=${encodeURIComponent(query)}`);
return `Showing results for "${query}".`;
},
});
```

Invalid calls are rejected with the schema's messages before `execute` runs.

### Results

What `execute` returns is sent to the agent:

| Return value | Agent receives |
| ----------------------- | --------------------------------------------------------------------------------------------------------------- |
| Nothing | `Done.` |
| A string | The string. |
| Another value | The value as JSON. |
| `{ content, isError? }` | The [MCP tool result](https://modelcontextprotocol.io/specification/2025-06-18/server/tools#tool-result) as is. |
| A thrown error | An error result with the error's message. |

Use `defineAgentTool()` to declare a tool outside a component and pass it to `useAgentTool()` later. It checks the name and infers the input type from `input`. To offer a tool outside components, call `registerAgentTool()` from `@void/webmcp`; it returns a function that removes the tool.

## Navigation tool

`voidWebMcp()` offers a `navigate` tool that opens a page in your app. Pass `navigate: false` to leave it out:

```ts
voidWebMcp({ navigate: false });
```

The tool takes a `path`. Its description lists your pages, such as `/users/[id]`, with their Markdown titles. It navigates on the client like a `Link` and returns the new URL and title. Paths that match no page, and other origins, are rejected.

## Annotations

`annotations` tell the agent how a tool behaves:

| Annotation | Meaning |
| ---------------------- | -------------------------------------------------------------------------- |
| `readOnlyHint` | The tool only reads data, so an agent can call it without asking the user. |
| `consequentialHint` | The tool has effects that are hard to undo; the agent may ask first. |
| `untrustedContentHint` | The result can contain content from sources the app does not control. |

## Writing good tools

- Describe what the tool does for the user and when to use it. Agents choose tools by their descriptions.
- Name the tool after the task, such as `add_user`, not after the UI, such as `submit_form`.
- Return data the agent can act on, such as ids and URLs, rather than a plain `Done.`.
- Offer a tool only on pages where it works, so the agent sees what it can do right now.
2 changes: 1 addition & 1 deletion docs/reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Use this reference to find public imports, signatures, and return types. For set
| [Deployment artifacts](./api/deploy.md) | Package built apps for managed deployment. |
| [Types](./api/types.md) | Typed URLs, route types, handler context, and head metadata. |
| [Environment variables](./api/env.md) | Validated environment values and binding types. |
| [Pages adapters](./api/pages.md) | React, Vue, Svelte, Solid, and Markdown. |
| [Pages adapters](./api/pages.md) | React, Vue, Svelte, Solid, Markdown, and WebMCP. |

## User-Facing Imports {#user-facing-imports}

Expand Down
Loading