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

webmcp.md

353 lines10.3 KB

1---
2title: WebMCP Tools
3id: webmcp
4order: 5
5description: "Expose TanStack AI client tools to browser agents 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​
17You have a browser action that a page agent needs to discover and execute. WebMCP exposes your TanStack client tools through `document.modelContext`.
18​
19> **Experimental:** WebMCP support is experimental. Use it as progressive enhancement, not as a required application path.
20​
21During SSR, in insecure contexts, or in unsupported browsers, registration resolves without adding tools. Your application continues to work without WebMCP.
22​
23This page sends your tools to WebMCP. To give the WebMCP tools on a page to your own chat, see [Page WebMCP Tools in Chat](./webmcp-page-tools).
24​
25## 1. Install the packages
26​
27<!-- ::start:tabs variant="package-manager" mode="install" -->
28​
29react: @tanstack/ai @tanstack/ai-react zod
30vue: @tanstack/ai @tanstack/ai-vue zod
31solid: @tanstack/ai @tanstack/ai-solid zod
32svelte: @tanstack/ai @tanstack/ai-svelte zod
33preact: @tanstack/ai @tanstack/ai-preact zod
34angular: @tanstack/ai @tanstack/ai-angular zod
35vanilla: @tanstack/ai @tanstack/ai-client zod
36octane: @tanstack/ai @tanstack/ai-octane octane zod
37remix: @tanstack/ai @tanstack/ai-remix remix zod
38​
39<!-- ::end:tabs -->
40​
41## 2. Define an executable client tool
42​
43Define the tool at module scope. A browser agent executes the tool to open a help panel.
44​
45```ts
46// tools.ts
47import { toolDefinition } from '@tanstack/ai'
48import { z } from 'zod'
49​
50export const openHelpPanel = toolDefinition({
51 name: 'open_help_panel',
52 description: 'Open the help panel for a topic',
53 inputSchema: z.object({
54 topic: z.string(),
55 }),
56 outputSchema: z.object({
57 opened: z.boolean(),
58 }),
59}).client((input) => {
60 const panel = document.querySelector<HTMLElement>('#help-panel')
61 if (!panel) {
62 return { opened: false }
63 }
64​
65 panel.dataset.topic = input.topic
66 panel.showPopover()
67 return { opened: true }
68})
69​
70export const webMCPTools = [openHelpPanel]
71```
72​
73WebMCP requires an executable `.client()` implementation. It also requires a nonempty description and a valid tool name.
74​
75A tool name can contain 1 to 128 ASCII letters, numbers, underscores, hyphens, or periods.
76​
77## 3. Register the tool
78​
79Use the lifecycle wrapper for your framework. Each wrapper removes its registrations during owner cleanup.
80​
81<!-- ::start:framework -->
82​
83# React
84​
85Call `useRegisterWebMCPTools` in a component. If its contents do not change, keep the tool array stable.
86​
87```tsx
88import { useRegisterWebMCPTools } from '@tanstack/ai-react'
89import { webMCPTools } from './tools'
90​
91const webMCPOptions = {
92 onError(error: unknown) {
93 console.error('WebMCP registration failed', error)
94 },
96​
97export function HelpPage() {
98 useRegisterWebMCPTools(webMCPTools, webMCPOptions)
99​
100 return (
101 <section id="help-panel" popover="auto">
102 <h2>Help</h2>
103 </section>
104 )
106```
107​
108# Vue
109​
110Call `useRegisterWebMCPTools` in a setup scope. Vue removes the registrations during scope disposal.
111​
112```vue
113<script setup lang="ts">
114import { useRegisterWebMCPTools } from '@tanstack/ai-vue'
115import { webMCPTools } from './tools'
116​
117useRegisterWebMCPTools(webMCPTools, {
118 onError: (error) => console.error('WebMCP registration failed', error),
119})
120</script>
121​
122<template>
123 <section id="help-panel" popover>
124 <h2>Help</h2>
125 </section>
126</template>
127```
128​
129# Solid
130​
131Call `useRegisterWebMCPTools` in a reactive owner. Solid removes the registrations during owner cleanup.
132​
133```tsx
134import { useRegisterWebMCPTools } from '@tanstack/ai-solid'
135import { webMCPTools } from './tools'
136​
137export function HelpPage() {
138 useRegisterWebMCPTools(webMCPTools, {
139 onError: (error) => console.error('WebMCP registration failed', error),
140 })
141​
142 return (
143 <section id="help-panel" popover="auto">
144 <h2>Help</h2>
145 </section>
146 )
148```
149​
150# Svelte
151​
152Call `createRegisterWebMCPTools` in the component script. Svelte removes the registrations during component destruction.
153​
154```svelte
155<script lang="ts">
156 import { createRegisterWebMCPTools } from '@tanstack/ai-svelte'
157 import { webMCPTools } from './tools'
158​
159 createRegisterWebMCPTools(webMCPTools, {
160 onError: (error) => console.error('WebMCP registration failed', error),
161 })
162</script>
163​
164<section id="help-panel" popover>
165 <h2>Help</h2>
166</section>
167```
168​
169# Preact
170​
171Call `useRegisterWebMCPTools` in a component. If its contents do not change, keep the tool array stable.
172​
173```tsx
174import { useRegisterWebMCPTools } from '@tanstack/ai-preact'
175import { webMCPTools } from './tools'
176​
177const webMCPOptions = {
178 onError(error: unknown) {
179 console.error('WebMCP registration failed', error)
180 },
182​
183export function HelpPage() {
184 useRegisterWebMCPTools(webMCPTools, webMCPOptions)
185​
186 return (
187 <section id="help-panel" popover="auto">
188 <h2>Help</h2>
189 </section>
190 )
192```
193​
194# Angular
195​
196Call `injectRegisterWebMCPTools` in an injection context. Angular removes the registrations through `DestroyRef`.
197​
198```ts ignore
199import { Component } from '@angular/core'
200import { injectRegisterWebMCPTools } from '@tanstack/ai-angular'
201import { webMCPTools } from './tools'
202​
203@Component({
204 selector: 'app-help-page',
205 standalone: true,
206 template: '<section id="help-panel" popover><h2>Help</h2></section>',
207})
208export class HelpPage {
209 webMCP = injectRegisterWebMCPTools(webMCPTools, {
210 onError: (error) => console.error('WebMCP registration failed', error),
211 })
213```
214​
215# Octane
216​
217Call `useRegisterWebMCPTools` in a component. If its contents do not change, keep the tool array stable.
218​
219```tsx
220import { useRegisterWebMCPTools } from '@tanstack/ai-octane'
221import { webMCPTools } from './tools'
222​
223const webMCPOptions = {
224 onError(error: unknown) {
225 console.error('WebMCP registration failed', error)
226 },
228​
229export function HelpPage() {
230 useRegisterWebMCPTools(webMCPTools, webMCPOptions)
231​
232 return (
233 <section id="help-panel" popover="auto">
234 <h2>Help</h2>
235 </section>
236 )
238```
239​
240# Remix
241​
242Pass the component `Handle` to `createRegisterWebMCPTools`. Remix uses `handle.signal` for cleanup.
243​
244```tsx
245import { createRegisterWebMCPTools } from '@tanstack/ai-remix'
246import { clientEntry, createElement } from 'remix/ui'
247import { webMCPTools } from './tools'
248​
249export const HelpPage = clientEntry(
250 import.meta.url,
251 function HelpPage(handle) {
252 createRegisterWebMCPTools(handle, webMCPTools, {
253 onError: (error) => console.error('WebMCP registration failed', error),
254 })
255​
256 return () =>
257 createElement(
258 'section',
259 { id: 'help-panel', popover: 'auto' },
260 createElement('h2', {}, 'Help'),
261 )
262 },
264```
265​
266<!-- ::end:framework -->
267​
268Changes to the `tools` or `options` value replace registrations in React, Preact, and Octane. Stable values prevent unnecessary registry changes.
269​
270All wrappers accept `toolOptions`, `context`, and `onError`. A contextual client tool makes `context` and the options argument required.
271​
272### Migrate from the earlier wrapper names
273​
274The earlier names still work, but they are deprecated. They will be removed in 1.0.0:
275​
276| Deprecated name | Current name |
277| ------------------- | --------------------------- |
278| `useWebMCPTools` | `useRegisterWebMCPTools` |
279| `createWebMCPTools` | `createRegisterWebMCPTools` |
280| `injectWebMCPTools` | `injectRegisterWebMCPTools` |
281​
282The options types have the same change, for example `UseWebMCPToolsOptions` becomes `UseRegisterWebMCPToolsOptions`.
283​
284## Use the framework-neutral registrar
285​
286If another lifecycle owns the registration, use `registerWebMCPTools`. The required signal controls how long the tools stay registered.
287​
288```ts
289import { registerWebMCPTools } from '@tanstack/ai-client'
290import { webMCPTools } from './tools'
291​
292const registration = new AbortController()
293​
294await registerWebMCPTools(webMCPTools, {
295 signal: registration.signal,
296 toolOptions: {
297 open_help_panel: {
298 title: 'Open help',
299 annotations: {
300 readOnlyHint: false,
301 untrustedContentHint: false,
302 },
303 },
304 },
305})
306​
307export function disposeWebMCPTools() {
308 registration.abort()
310```
311​
312Call `disposeWebMCPTools()` from the explicit teardown path for your application shell. Do not use `pagehide`, because BFCache can restore the page.
313​
314The annotations are hints for the browser agent. They do not enforce permissions or approval.
315​
316## Understand the execution boundary
317​
318WebMCP calls the client handler directly. The result returns directly to WebMCP and does not continue a TanStack chat run.
319​
320The execution context has these WebMCP rules:
321​
322- `abortSignal` is the WebMCP execution signal. Pass it to cancellable browser APIs.
323- `context` is the value from the registrar or wrapper options.
324- `emitCustomEvent` does nothing because no TanStack stream exists.
325- `toolCallId` is absent because WebMCP does not provide one.
326​
327The registration signal and the execution signal have different jobs. The registration signal removes tools, while the execution signal cancels one call.
328​
329Cancellation is cooperative. Your handler must observe `abortSignal` or pass it to an API that supports cancellation.
330​
331## Validate data at the page boundary
332​
333The registrar converts each input schema to JSON Schema for WebMCP discovery.
334​
335For a Standard Schema, it validates input before execution. It also validates output before the result returns to WebMCP.
336​
337A raw JSON Schema only describes the data. TanStack AI does not use it for runtime validation.
338​
339If you use raw JSON Schema, validate untrusted input inside the handler. Return a value that matches the output schema.
340​
341Tool results must be JSON-serializable. WebMCP reports a serialization failure to its caller.
342​
343## Keep the security boundary on the server
344​
345`needsApproval: true` tools cannot register with WebMCP. WebMCP annotations cannot enforce the TanStack approval contract.
346​
347Do not expose a sensitive action only through a browser check. The server endpoint must enforce authentication, authorization, rate limits, and business rules.
348​
349Tool names must be unique. Duplicate names in one call fail before registration changes the document registry.
350​
351WebMCP also rejects a name that another owner already registered. If registration fails, the registrar removes only the tools added by that call.
352​
353If WebMCP is available, your page now exposes `open_help_panel`. Other browsers continue to use the page without that agent action.