Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

webmcp.md

387 lines11.1 KB

1---
2title: WebMCP Tools
3id: webmcp
4order: 5
5description: "Let browser agents call your TanStack AI client tools through the experimental WebMCP API."
6keywords:
7 - tanstack ai
8 - webmcp
9 - browser agents
10 - client tools
11 - registerWebMCPTools
12 - useRegisterWebMCPTools
13 - createRegisterWebMCPTools
14 - injectRegisterWebMCPTools
15---
16​
17WebMCP publishes named actions on the page. A browser agent calls an action by name instead of clicking the UI.
18​
19You register your TanStack client tools on `document.modelContext`. The agent finds them there and calls them.
20​
21> [!WARNING]
22> WebMCP is experimental. Treat it as an extra for browsers that support it. Your page must work without it.
23​
24This page sends your tools to WebMCP. To use the WebMCP tools of a page in your own chat, see [Page WebMCP Tools in Chat](./webmcp-page-tools).
25​
26## Set it up
27​
28### 1. Install the packages
29​
30<!-- ::start:tabs variant="package-manager" mode="install" -->
31​
32react: @tanstack/ai @tanstack/ai-react zod
33vue: @tanstack/ai @tanstack/ai-vue zod
34solid: @tanstack/ai @tanstack/ai-solid zod
35svelte: @tanstack/ai @tanstack/ai-svelte zod
36preact: @tanstack/ai @tanstack/ai-preact zod
37angular: @tanstack/ai @tanstack/ai-angular zod
38vanilla: @tanstack/ai @tanstack/ai-client zod
39octane: @tanstack/ai @tanstack/ai-octane octane zod
40remix: @tanstack/ai @tanstack/ai-remix remix zod
41​
42<!-- ::end:tabs -->
43​
44### 2. Define a client tool
45​
46Put the tool at module scope. This tool opens a help panel:
47​
48```ts
49// tools.ts
50import { toolDefinition } from '@tanstack/ai'
51import { z } from 'zod'
52​
53export const openHelpPanel = toolDefinition({
54 name: 'open_help_panel',
55 description: 'Open the help panel for a topic',
56 inputSchema: z.object({
57 topic: z.string(),
58 }),
59 outputSchema: z.object({
60 opened: z.boolean(),
61 }),
62}).client((input) => {
63 const panel = document.querySelector<HTMLElement>('#help-panel')
64 if (!panel) {
65 return { opened: false }
66 }
67​
68 panel.dataset.topic = input.topic
69 panel.showPopover()
70 return { opened: true }
71})
72​
73export const webMCPTools = [openHelpPanel]
74```
75​
76### 3. Register the tool
77​
78Call the register function for your framework. When the component goes away, the function removes the tools.
79​
80<!-- ::start:framework -->
81​
82# React
83​
84Call `useRegisterWebMCPTools` in a component. Keep the tool array and the options stable, for example at module scope.
85​
86```tsx
87import { useRegisterWebMCPTools } from '@tanstack/ai-react'
88import { webMCPTools } from './tools'
89​
90const webMCPOptions = {
91 onError(error: unknown) {
92 console.error('WebMCP registration failed', error)
93 },
95​
96export function HelpPage() {
97 useRegisterWebMCPTools(webMCPTools, webMCPOptions)
98​
99 return (
100 <section id="help-panel" popover="auto">
101 <h2>Help</h2>
102 </section>
103 )
105```
106​
107# Vue
108​
109Call `useRegisterWebMCPTools` in a setup scope. Vue removes the tools when it disposes the scope.
110​
111```vue
112<script setup lang="ts">
113import { useRegisterWebMCPTools } from '@tanstack/ai-vue'
114import { webMCPTools } from './tools'
115​
116useRegisterWebMCPTools(webMCPTools, {
117 onError: (error) => console.error('WebMCP registration failed', error),
118})
119</script>
120​
121<template>
122 <section id="help-panel" popover>
123 <h2>Help</h2>
124 </section>
125</template>
126```
127​
128# Solid
129​
130Call `useRegisterWebMCPTools` in a reactive owner. Solid removes the tools when it cleans up the owner.
131​
132```tsx
133import { useRegisterWebMCPTools } from '@tanstack/ai-solid'
134import { webMCPTools } from './tools'
135​
136export function HelpPage() {
137 useRegisterWebMCPTools(webMCPTools, {
138 onError: (error) => console.error('WebMCP registration failed', error),
139 })
140​
141 return (
142 <section id="help-panel" popover="auto">
143 <h2>Help</h2>
144 </section>
145 )
147```
148​
149# Svelte
150​
151Call `createRegisterWebMCPTools` in the component script. Svelte removes the tools when it destroys the component.
152​
153```svelte
154<script lang="ts">
155 import { createRegisterWebMCPTools } from '@tanstack/ai-svelte'
156 import { webMCPTools } from './tools'
157​
158 createRegisterWebMCPTools(webMCPTools, {
159 onError: (error) => console.error('WebMCP registration failed', error),
160 })
161</script>
162​
163<section id="help-panel" popover>
164 <h2>Help</h2>
165</section>
166```
167​
168# Preact
169​
170Call `useRegisterWebMCPTools` in a component. Keep the tool array and the options stable, for example at module scope.
171​
172```tsx
173import { useRegisterWebMCPTools } from '@tanstack/ai-preact'
174import { webMCPTools } from './tools'
175​
176const webMCPOptions = {
177 onError(error: unknown) {
178 console.error('WebMCP registration failed', error)
179 },
181​
182export function HelpPage() {
183 useRegisterWebMCPTools(webMCPTools, webMCPOptions)
184​
185 return (
186 <section id="help-panel" popover="auto">
187 <h2>Help</h2>
188 </section>
189 )
191```
192​
193# Angular
194​
195Call `injectRegisterWebMCPTools` in an injection context. Angular removes the tools through `DestroyRef`.
196​
197```ts ignore
198import { Component } from '@angular/core'
199import { injectRegisterWebMCPTools } from '@tanstack/ai-angular'
200import { webMCPTools } from './tools'
201​
202@Component({
203 selector: 'app-help-page',
204 standalone: true,
205 template: '<section id="help-panel" popover><h2>Help</h2></section>',
206})
207export class HelpPage {
208 webMCP = injectRegisterWebMCPTools(webMCPTools, {
209 onError: (error) => console.error('WebMCP registration failed', error),
210 })
212```
213​
214# Octane
215​
216Call `useRegisterWebMCPTools` in a component. Keep the tool array and the options stable, for example at module scope.
217​
218```tsx
219import { useRegisterWebMCPTools } from '@tanstack/ai-octane'
220import { webMCPTools } from './tools'
221​
222const webMCPOptions = {
223 onError(error: unknown) {
224 console.error('WebMCP registration failed', error)
225 },
227​
228export function HelpPage() {
229 useRegisterWebMCPTools(webMCPTools, webMCPOptions)
230​
231 return (
232 <section id="help-panel" popover="auto">
233 <h2>Help</h2>
234 </section>
235 )
237```
238​
239# Remix
240​
241Pass the component `Handle` to `createRegisterWebMCPTools`. Remix uses `handle.signal` to remove the tools.
242​
243```tsx
244import { createRegisterWebMCPTools } from '@tanstack/ai-remix'
245import { clientEntry, createElement } from 'remix/ui'
246import { webMCPTools } from './tools'
247​
248export const HelpPage = clientEntry(
249 import.meta.url,
250 function HelpPage(handle) {
251 createRegisterWebMCPTools(handle, webMCPTools, {
252 onError: (error) => console.error('WebMCP registration failed', error),
253 })
254​
255 return () =>
256 createElement(
257 'section',
258 { id: 'help-panel', popover: 'auto' },
259 createElement('h2', {}, 'Help'),
260 )
261 },
263```
264​
265<!-- ::end:framework -->
266​
267That is all. In a browser with WebMCP, an agent on this page can now find and call `open_help_panel`. In other browsers, the page works the same as before.
268​
269## How it works
270​
2711. The register function turns each tool into a WebMCP tool. The input schema becomes JSON Schema, so the agent knows what to send.
2722. The agent calls the tool by name.
2733. WebMCP runs your `.client()` function in the page.
2744. The result goes back to the agent. It does not start or continue a TanStack chat run.
275​
276During SSR, in an insecure context, or in a browser without WebMCP, the register function adds no tools. It does not throw, so you can call it everywhere.
277​
278Each tool must have:
279​
280- A `.client()` function. WebMCP needs code that it can run.
281- A description that is not empty.
282- A name of 1 to 128 ASCII letters, numbers, underscores, hyphens, or periods.
283- An input schema that accepts an object.
284- No `needsApproval: true`. See [Security](#security).
285​
286If a tool breaks one of these rules, registration fails and `onError` gets the error.
287​
288## Advanced
289​
290### Register without a framework
291​
292If your own code owns the lifecycle, use `registerWebMCPTools` from `@tanstack/ai-client`. The `signal` option is required. The tools stay registered until the signal aborts.
293​
294```ts
295import { registerWebMCPTools } from '@tanstack/ai-client'
296import { webMCPTools } from './tools'
297​
298const registration = new AbortController()
299​
300await registerWebMCPTools(webMCPTools, {
301 signal: registration.signal,
302 toolOptions: {
303 open_help_panel: {
304 title: 'Open help',
305 annotations: {
306 readOnlyHint: false,
307 untrustedContentHint: false,
308 },
309 },
310 },
311})
312​
313export function disposeWebMCPTools() {
314 registration.abort()
316```
317​
318Call `disposeWebMCPTools()` from the teardown code of your application shell. Do not call it on `pagehide`, because the browser can restore the page from the back/forward cache.
319​
320`registerWebMCPTools` returns a promise. If registration fails, the promise rejects.
321​
322### Options
323​
324All framework wrappers accept these options. `registerWebMCPTools` accepts the first two, plus `signal`.
325​
326- `toolOptions`: a `title` and `annotations` for each tool, by tool name.
327- `context`: the value that your handler gets as `context`. If a client tool declares a context, `context` and the options argument are required.
328- `onError`: gets a registration failure.
329​
330The annotations are hints for the browser agent:
331​
332- `readOnlyHint`: the tool does not change state.
333- `untrustedContentHint`: the tool can return content that your app does not trust.
334​
335The annotations do not enforce permissions or approval.
336​
337### Stable values in React, Preact, and Octane
338​
339In these frameworks, a new `tools` or `options` value replaces the registrations. If the contents do not change, keep both values stable. Stable values prevent extra changes to the WebMCP registry.
340​
341### What your handler gets
342​
343WebMCP calls your client handler directly. The handler gets these values:
344​
345- `abortSignal`: the WebMCP signal for this call. Pass it to browser APIs that you can cancel.
346- `context`: the value from the register options.
347- `emitCustomEvent`: does nothing, because no TanStack stream exists.
348- `toolCallId`: not set, because WebMCP does not give one.
349​
350The two signals have different jobs:
351​
352- The registration signal removes the tools.
353- The execution signal (`abortSignal`) cancels one call.
354​
355Cancellation is cooperative. Your handler must read `abortSignal`, or pass it to an API that supports cancellation.
356​
357### Validation
358​
359The register function converts each input schema to JSON Schema for WebMCP.
360​
361- **Standard Schema** (for example Zod): TanStack AI validates the input before your handler runs. It also validates the output before the result goes back to WebMCP.
362- **Raw JSON Schema**: the schema only describes the data. TanStack AI does not validate with it. Validate untrusted input inside the handler, and return a value that matches the output schema.
363​
364Tool results must be JSON-serializable. If a result cannot be serialized, WebMCP reports the failure to its caller.
365​
366### Security
367​
368A tool with `needsApproval: true` cannot register with WebMCP. WebMCP annotations cannot enforce the TanStack approval flow.
369​
370Do not protect a sensitive action with a browser check only. Your server endpoint must enforce these rules:
371​
372- Authentication.
373- Authorization.
374- Rate limits.
375- Business rules.
376​
377### Duplicate tool names
378​
379Tool names must be unique.
380​
381- If one call has two tools with the same name, the call fails before it changes the registry.
382- If another owner already registered a name, WebMCP rejects that name.
383- If registration fails, the register function removes only the tools that this call added.
384​
385### Upgrade from 0.x
386​
387The 1.0 names of the wrapper functions and their option types are in the [1.0 upgrade guide](../migration/v1#other-client-renames).