From ca8ed1c3a03ddc767e8cd9db6c8e3d6e49be65db Mon Sep 17 00:00:00 2001 From: Alexander Lichter Date: Sat, 5 Dec 2026 00:42:37 +0100 Subject: [PATCH 1/5] Add experimental WebMCP tools to Pages adapters --- docs/.vitepress/config.ts | 1 + docs/guide/pages-routing/webmcp.md | 247 +++++++++ docs/reference/api/pages.md | 26 + packages/react/src/plugin-pages.ts | 15 +- packages/react/src/plugin.ts | 3 + packages/react/src/runtime/index.ts | 8 + packages/react/src/runtime/use-agent-tool.ts | 31 ++ packages/react/src/runtime/use-form.ts | 212 +++++--- packages/react/test/unit/use-form.test.ts | 85 +++ packages/solid/src/plugin-pages.ts | 21 +- packages/solid/src/plugin.ts | 3 + packages/solid/src/runtime/index.ts | 8 + packages/solid/src/runtime/use-agent-tool.ts | 10 + packages/solid/src/runtime/use-form.ts | 48 +- packages/svelte/src/plugin-pages.ts | 21 +- packages/svelte/src/plugin.ts | 3 + packages/svelte/src/runtime/index.ts | 8 + packages/svelte/src/runtime/use-agent-tool.ts | 8 + .../svelte/src/runtime/use-form.svelte.ts | 44 +- packages/void/skills/void/SKILL.md | 1 + packages/void/src/codegen/route-types.ts | 1 + packages/void/src/pages/client.ts | 17 + packages/void/src/pages/index.ts | 2 + packages/void/src/pages/plugin.ts | 3 + packages/void/src/pages/webmcp-codegen.ts | 57 ++ packages/void/src/pages/webmcp.ts | 486 ++++++++++++++++++ packages/void/src/routes-stub.ts | 3 + packages/void/test/unit/pages-webmcp.test.ts | 470 +++++++++++++++++ packages/vue/src/plugin-pages.ts | 21 +- packages/vue/src/plugin.ts | 9 +- packages/vue/src/runtime/index.ts | 8 + packages/vue/src/runtime/use-agent-tool.ts | 12 + packages/vue/src/runtime/use-form.ts | 56 +- playground/react-pages/pages/users/index.tsx | 21 +- playground/react-pages/vite.config.ts | 2 +- playground/solid-pages/pages/users/index.tsx | 21 +- playground/solid-pages/vite.config.ts | 2 +- .../svelte-pages/pages/users/index.svelte | 21 +- playground/svelte-pages/vite.config.ts | 2 +- playground/vue-pages/pages/users/index.vue | 21 +- playground/vue-pages/vite.config.ts | 2 +- test/pages-browser.ts | 5 +- test/pages-client-contract.ts | 3 + test/pages-webmcp-contract.ts | 143 ++++++ 44 files changed, 2059 insertions(+), 132 deletions(-) create mode 100644 docs/guide/pages-routing/webmcp.md create mode 100644 packages/react/src/runtime/use-agent-tool.ts create mode 100644 packages/solid/src/runtime/use-agent-tool.ts create mode 100644 packages/svelte/src/runtime/use-agent-tool.ts create mode 100644 packages/void/src/pages/webmcp-codegen.ts create mode 100644 packages/void/src/pages/webmcp.ts create mode 100644 packages/void/test/unit/pages-webmcp.test.ts create mode 100644 packages/vue/src/runtime/use-agent-tool.ts create mode 100644 test/pages-webmcp-contract.ts diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index af2f1a1aaf..092af1fb47 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -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' }, ], }, { diff --git a/docs/guide/pages-routing/webmcp.md b/docs/guide/pages-routing/webmcp.md new file mode 100644 index 0000000000..751115e7ff --- /dev/null +++ b/docs/guide/pages-routing/webmcp.md @@ -0,0 +1,247 @@ +--- +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. + +Void Pages can offer three kinds of tools: + +- **Form tools**: add `tool` to `useForm()`. An agent fills in the form and submits 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. +::: + +## 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 `tool` to `useForm()` to let agents submit the form: + +```ts +import { useForm } from '@void/react'; // or @void/vue, @void/svelte, @void/solid + +const form = useForm( + '/users', + { name: '', email: '' }, + { + tool: { + 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. Void fills in the form with the agent's values, so the user sees them. +2. Void submits the form 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 }, + { + tool: { + 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/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] + +``` + +```svelte [Svelte] + +``` + +```tsx [Solid] +import { useAgentTool } from '@void/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 { useAgentTool, useRouter } from '@void/react'; // or @void/vue, @void/svelte, @void/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`. + +## Navigation tool + +Set `webmcp.navigate` in the adapter plugin to offer a `navigate` tool that opens a page in your app: + +::: code-group + +```ts [React] +// vite.config.ts +export default defineConfig({ + plugins: [voidPlugin(), voidReact({ webmcp: { navigate: true } })], +}); +``` + +```ts [Vue] +// vite.config.ts +export default defineConfig({ + plugins: [voidPlugin(), voidVue({ webmcp: { navigate: true } })], +}); +``` + +```ts [Svelte] +// vite.config.ts +export default defineConfig({ + plugins: [voidPlugin(), voidSvelte({ webmcp: { navigate: true } })], +}); +``` + +```ts [Solid] +// vite.config.ts +export default defineConfig({ + plugins: [voidPlugin(), voidSolid({ webmcp: { navigate: true } })], +}); +``` + +::: + +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. diff --git a/docs/reference/api/pages.md b/docs/reference/api/pages.md index 390670498e..c56f7e76d3 100644 --- a/docs/reference/api/pages.md +++ b/docs/reference/api/pages.md @@ -91,6 +91,9 @@ interface VoidVueOptions { hoverDelay?: number; // default: 75 (ms) cacheFor?: number | string | [string, string]; // default: '30s' }; + webmcp?: { + navigate?: boolean; // offer a WebMCP `navigate` tool for your pages (default: false) + }; } ``` @@ -106,6 +109,9 @@ Imported from `"@void/vue"`. | `useNavigation()` | Returns pending navigation state: `{ state, location, method }`, where `state` is `"idle"`, `"loading"`, or `"submitting"` and `location` is the pending destination. | | `useForm(url, defaults, options?)` | Typed reactive form helper bound to a page action URL. Returns `{ data, post, put, patch, delete, pending, errors, error, hasChanges, wasSuccessful, recentlySuccessful, reset, setError, clearErrors, clearError }`. | | `useIslandForm(defaults)` | Form helper for island components. Same form state as `useForm()`, plus `submit(url, method)`. `post(url)`, `put(url)`, `patch(url)`, and `delete(url)` take the action URL and submit with `fetch()`. With [`routing.typedRoutes`](../config.md#routing-typedroutes), the URL is a [`RouteUrl`](./types.md#route-types). | +| `useAgentTool(tool)` | Offers a [WebMCP](../../guide/pages-routing/webmcp.md) tool to browser agents while the component is mounted. Does nothing where WebMCP is unavailable. | +| `defineAgentTool(tool)` | Declares a WebMCP tool for `useAgentTool()`, checking its name and inferring its input type from `input`. | +| `isWebMcpSupported()` | Returns whether the browser supports WebMCP. | | `action(url, options?)` | Awaitable one-shot page action helper. Uses `POST` by default and accepts `{ data, method, params, preserveScroll }`, where `method` can be `"PUT"`, `"PATCH"`, or `"DELETE"`. Returns an [`ActionResult`](../../guide/pages-routing/actions-and-forms.md#action-helper). | | `isVoidActionError(error)` | Checks and narrows `error` to `VoidActionError`, including thrown `401`, `403`, and `500` errors. See [Handling action errors](../../guide/pages-routing/actions-and-forms.md#validation-errors). | | `isCallSiteActionError(error)` | Recognizes the expected errors returned by `action()`, such as `409` or `422`. | @@ -147,6 +153,9 @@ interface VoidReactOptions { hoverDelay?: number; // default: 75 (ms) cacheFor?: number | string | [string, string]; // default: '30s' }; + webmcp?: { + navigate?: boolean; // offer a WebMCP `navigate` tool for your pages (default: false) + }; } ``` @@ -162,6 +171,9 @@ Imported from `"@void/react"`. | `useNavigation()` | Returns pending navigation state: `{ state, location, method }`, where `state` is `"idle"`, `"loading"`, or `"submitting"` and `location` is the pending destination. | | `useForm(url, defaults, options?)` | Form helper hook. Returns `{ data, setData, post, put, patch, delete, pending, errors, error, hasChanges, wasSuccessful, recentlySuccessful, reset, setError, clearErrors, clearError }`. | | `useIslandForm(defaults)` | Form helper hook for island components. Same form state as `useForm()`. `post(url)`, `put(url)`, `patch(url)`, and `delete(url)` take the action URL and submit with `fetch()`. With [`routing.typedRoutes`](../config.md#routing-typedroutes), the URL is a [`RouteUrl`](./types.md#route-types). | +| `useAgentTool(tool)` | Offers a [WebMCP](../../guide/pages-routing/webmcp.md) tool to browser agents while the component is mounted. Does nothing where WebMCP is unavailable. | +| `defineAgentTool(tool)` | Declares a WebMCP tool for `useAgentTool()`, checking its name and inferring its input type from `input`. | +| `isWebMcpSupported()` | Returns whether the browser supports WebMCP. | | `action(url, options?)` | Awaitable one-shot page action helper. Uses `POST` by default and accepts `{ data, method, params, preserveScroll }`, where `method` can be `"PUT"`, `"PATCH"`, or `"DELETE"`. Returns an [`ActionResult`](../../guide/pages-routing/actions-and-forms.md#action-helper). | | `isVoidActionError(error)` | Checks and narrows `error` to `VoidActionError`, including thrown `401`, `403`, and `500` errors. See [Handling action errors](../../guide/pages-routing/actions-and-forms.md#validation-errors). | | `isCallSiteActionError(error)` | Recognizes the expected errors returned by `action()`, such as `409` or `422`. | @@ -232,6 +244,9 @@ interface VoidSvelteOptions { hoverDelay?: number; // default: 75 (ms) cacheFor?: number | string | [string, string]; // default: '30s' }; + webmcp?: { + navigate?: boolean; // offer a WebMCP `navigate` tool for your pages (default: false) + }; } ``` @@ -244,6 +259,9 @@ Imported from `"@void/svelte"`. | `Link` | Svelte component for SPA navigation. Renders `` for GET, `