react-webmcp 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +294 -0
- package/dist/index.d.mts +407 -0
- package/dist/index.d.ts +407 -0
- package/dist/index.js +255 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +238 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 tech-sumit
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
# react-webmcp
|
|
2
|
+
|
|
3
|
+
React hooks and components for the [WebMCP](https://github.com/webmachinelearning/webmcp) standard — expose structured tools for AI agents on your website.
|
|
4
|
+
|
|
5
|
+
WebMCP is a W3C-proposed web standard (Chrome 146+) that allows websites to register tools that AI agents can discover and invoke directly, replacing unreliable screen-scraping with robust, schema-driven interaction.
|
|
6
|
+
|
|
7
|
+
**react-webmcp** provides idiomatic React primitives for both the Imperative and Declarative WebMCP APIs.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **`useWebMCPTool`** — Register a single tool with automatic lifecycle management (mount/unmount)
|
|
12
|
+
- **`useWebMCPContext`** — Register multiple tools at once via `provideContext()`
|
|
13
|
+
- **`useToolEvent`** — Listen for `toolactivated` and `toolcancel` browser events
|
|
14
|
+
- **`<WebMCPForm>`** — Declarative form component with `toolname` / `tooldescription` attributes
|
|
15
|
+
- **`<WebMCPInput>`, `<WebMCPSelect>`, `<WebMCPTextarea>`** — Form controls with `toolparam*` attributes
|
|
16
|
+
- **`<WebMCPProvider>`** — Context provider for availability detection
|
|
17
|
+
- Full TypeScript support with `navigator.modelContext` type augmentation
|
|
18
|
+
- Works with the [Model Context Tool Inspector](https://chromewebstore.google.com/detail/model-context-tool-inspec/gbpdfapgefenggkahomfgkhfehlcenpd) extension
|
|
19
|
+
|
|
20
|
+
## Requirements
|
|
21
|
+
|
|
22
|
+
- **React** 18+
|
|
23
|
+
- **Chrome** 146.0.7672.0+ with the `WebMCP for testing` flag enabled at `chrome://flags/#enable-webmcp-testing`
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install react-webmcp
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Quick Start
|
|
32
|
+
|
|
33
|
+
### Imperative API (Hooks)
|
|
34
|
+
|
|
35
|
+
Register tools as React hooks — they are automatically registered on mount and unregistered on unmount:
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import { useWebMCPTool } from "react-webmcp";
|
|
39
|
+
|
|
40
|
+
function TodoApp() {
|
|
41
|
+
const [todos, setTodos] = useState<string[]>([]);
|
|
42
|
+
|
|
43
|
+
useWebMCPTool({
|
|
44
|
+
name: "addTodo",
|
|
45
|
+
description: "Add a new item to the todo list",
|
|
46
|
+
inputSchema: {
|
|
47
|
+
type: "object",
|
|
48
|
+
properties: {
|
|
49
|
+
text: { type: "string", description: "The todo item text" },
|
|
50
|
+
},
|
|
51
|
+
required: ["text"],
|
|
52
|
+
},
|
|
53
|
+
execute: ({ text }) => {
|
|
54
|
+
setTodos((prev) => [...prev, text as string]);
|
|
55
|
+
return { content: [{ type: "text", text: `Added todo: ${text}` }] };
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<ul>
|
|
61
|
+
{todos.map((t, i) => (
|
|
62
|
+
<li key={i}>{t}</li>
|
|
63
|
+
))}
|
|
64
|
+
</ul>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Declarative API (Components)
|
|
70
|
+
|
|
71
|
+
Wrap standard HTML forms with WebMCP components to expose them as tools:
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
import {
|
|
75
|
+
WebMCPForm,
|
|
76
|
+
WebMCPInput,
|
|
77
|
+
WebMCPSelect,
|
|
78
|
+
useToolEvent,
|
|
79
|
+
} from "react-webmcp";
|
|
80
|
+
|
|
81
|
+
function ReservationForm() {
|
|
82
|
+
useToolEvent("toolactivated", (toolName) => {
|
|
83
|
+
console.log(`Agent activated: ${toolName}`);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return (
|
|
87
|
+
<WebMCPForm
|
|
88
|
+
toolName="book_table"
|
|
89
|
+
toolDescription="Book a table at the restaurant"
|
|
90
|
+
onSubmit={(e) => {
|
|
91
|
+
e.preventDefault();
|
|
92
|
+
if (e.agentInvoked) {
|
|
93
|
+
e.respondWith(Promise.resolve("Booking confirmed!"));
|
|
94
|
+
}
|
|
95
|
+
}}
|
|
96
|
+
>
|
|
97
|
+
<WebMCPInput
|
|
98
|
+
name="name"
|
|
99
|
+
type="text"
|
|
100
|
+
toolParamDescription="Customer's full name"
|
|
101
|
+
required
|
|
102
|
+
/>
|
|
103
|
+
<WebMCPSelect
|
|
104
|
+
name="guests"
|
|
105
|
+
toolParamDescription="Number of guests"
|
|
106
|
+
>
|
|
107
|
+
<option value="1">1 Person</option>
|
|
108
|
+
<option value="2">2 People</option>
|
|
109
|
+
<option value="3">3 People</option>
|
|
110
|
+
<option value="4">4 People</option>
|
|
111
|
+
</WebMCPSelect>
|
|
112
|
+
<button type="submit">Book</button>
|
|
113
|
+
</WebMCPForm>
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## API Reference
|
|
119
|
+
|
|
120
|
+
### Hooks
|
|
121
|
+
|
|
122
|
+
#### `useWebMCPTool(config)`
|
|
123
|
+
|
|
124
|
+
Registers a single tool with `navigator.modelContext.registerTool()` on mount and unregisters on unmount.
|
|
125
|
+
|
|
126
|
+
| Parameter | Type | Description |
|
|
127
|
+
|-----------|------|-------------|
|
|
128
|
+
| `name` | `string` | Unique tool identifier |
|
|
129
|
+
| `description` | `string` | Human-readable description for agents |
|
|
130
|
+
| `inputSchema` | `JSONSchema` | JSON Schema for input parameters |
|
|
131
|
+
| `outputSchema` | `JSONSchema` | *(optional)* JSON Schema for output |
|
|
132
|
+
| `annotations` | `ToolAnnotations` | *(optional)* Hints like `readOnlyHint` |
|
|
133
|
+
| `execute` | `(input) => any` | Handler function called on invocation |
|
|
134
|
+
|
|
135
|
+
#### `useWebMCPContext(config)`
|
|
136
|
+
|
|
137
|
+
Replaces all registered tools using `provideContext()`. Calls `clearContext()` on unmount.
|
|
138
|
+
|
|
139
|
+
```tsx
|
|
140
|
+
useWebMCPContext({
|
|
141
|
+
tools: [
|
|
142
|
+
{ name: "tool1", description: "...", inputSchema: {}, execute: () => "ok" },
|
|
143
|
+
{ name: "tool2", description: "...", inputSchema: {}, execute: () => "ok" },
|
|
144
|
+
],
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
#### `useToolEvent(event, callback, toolNameFilter?)`
|
|
149
|
+
|
|
150
|
+
Listens for WebMCP browser events.
|
|
151
|
+
|
|
152
|
+
| Parameter | Type | Description |
|
|
153
|
+
|-----------|------|-------------|
|
|
154
|
+
| `event` | `"toolactivated" \| "toolcancel"` | Event name |
|
|
155
|
+
| `callback` | `(toolName: string) => void` | Event handler |
|
|
156
|
+
| `toolNameFilter` | `string` | *(optional)* Only fire for this tool |
|
|
157
|
+
|
|
158
|
+
#### `useWebMCPStatus()`
|
|
159
|
+
|
|
160
|
+
Returns WebMCP availability status (requires `<WebMCPProvider>`).
|
|
161
|
+
|
|
162
|
+
```tsx
|
|
163
|
+
const { available, testingAvailable } = useWebMCPStatus();
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Components
|
|
167
|
+
|
|
168
|
+
#### `<WebMCPForm>`
|
|
169
|
+
|
|
170
|
+
Renders a `<form>` with WebMCP declarative attributes.
|
|
171
|
+
|
|
172
|
+
| Prop | Type | Description |
|
|
173
|
+
|------|------|-------------|
|
|
174
|
+
| `toolName` | `string` | Maps to `toolname` attribute |
|
|
175
|
+
| `toolDescription` | `string` | Maps to `tooldescription` attribute |
|
|
176
|
+
| `toolAutoSubmit` | `boolean` | *(optional)* Maps to `toolautosubmit` |
|
|
177
|
+
| `onSubmit` | `(event) => void` | Enhanced event with `agentInvoked` and `respondWith` |
|
|
178
|
+
| `onToolActivated` | `(name) => void` | *(optional)* Tool activation callback |
|
|
179
|
+
| `onToolCancel` | `(name) => void` | *(optional)* Tool cancel callback |
|
|
180
|
+
|
|
181
|
+
#### `<WebMCPInput>`, `<WebMCPSelect>`, `<WebMCPTextarea>`
|
|
182
|
+
|
|
183
|
+
Standard HTML form elements with additional WebMCP props:
|
|
184
|
+
|
|
185
|
+
| Prop | Type | Description |
|
|
186
|
+
|------|------|-------------|
|
|
187
|
+
| `toolParamTitle` | `string` | *(optional)* Maps to `toolparamtitle` |
|
|
188
|
+
| `toolParamDescription` | `string` | *(optional)* Maps to `toolparamdescription` |
|
|
189
|
+
|
|
190
|
+
All standard HTML attributes are also supported.
|
|
191
|
+
|
|
192
|
+
#### `<WebMCPProvider>`
|
|
193
|
+
|
|
194
|
+
Context provider that makes WebMCP availability info accessible via `useWebMCPStatus()`.
|
|
195
|
+
|
|
196
|
+
### Utilities
|
|
197
|
+
|
|
198
|
+
#### `isWebMCPAvailable()`
|
|
199
|
+
|
|
200
|
+
Returns `true` if `navigator.modelContext` is available.
|
|
201
|
+
|
|
202
|
+
#### `isWebMCPTestingAvailable()`
|
|
203
|
+
|
|
204
|
+
Returns `true` if `navigator.modelContextTesting` is available (inspector API).
|
|
205
|
+
|
|
206
|
+
#### `getModelContext()`
|
|
207
|
+
|
|
208
|
+
Returns the `navigator.modelContext` object or `null`.
|
|
209
|
+
|
|
210
|
+
## Tool Annotations
|
|
211
|
+
|
|
212
|
+
Annotations provide metadata hints to AI agents:
|
|
213
|
+
|
|
214
|
+
```tsx
|
|
215
|
+
useWebMCPTool({
|
|
216
|
+
name: "deleteAccount",
|
|
217
|
+
description: "Permanently delete the user account",
|
|
218
|
+
inputSchema: { type: "object", properties: { confirm: { type: "boolean" } } },
|
|
219
|
+
annotations: {
|
|
220
|
+
readOnlyHint: "false",
|
|
221
|
+
destructiveHint: "true",
|
|
222
|
+
idempotentHint: "false",
|
|
223
|
+
},
|
|
224
|
+
execute: ({ confirm }) => {
|
|
225
|
+
if (!confirm) return "Deletion cancelled.";
|
|
226
|
+
// ...delete logic
|
|
227
|
+
return "Account deleted.";
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Demos
|
|
233
|
+
|
|
234
|
+
### Flight Search (Imperative API)
|
|
235
|
+
|
|
236
|
+
Replicates Google's [react-flightsearch](https://googlechromelabs.github.io/webmcp-tools/demos/react-flightsearch/) demo using `useWebMCPTool` hooks. Exposes 4 tools: `searchFlights`, `listFlights`, `setFilters`, `resetFilters`.
|
|
237
|
+
|
|
238
|
+
```bash
|
|
239
|
+
cd demos/flight-search
|
|
240
|
+
npm install
|
|
241
|
+
npm run dev
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### French Bistro (Declarative API)
|
|
245
|
+
|
|
246
|
+
Replicates Google's [french-bistro](https://googlechromelabs.github.io/webmcp-tools/demos/french-bistro/) demo using `<WebMCPForm>` and `<WebMCPInput>` components. Exposes the `book_table_le_petit_bistro` tool.
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
cd demos/french-bistro
|
|
250
|
+
npm install
|
|
251
|
+
npm run dev
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## Testing with the Inspector
|
|
255
|
+
|
|
256
|
+
1. Install the [Model Context Tool Inspector](https://chromewebstore.google.com/detail/model-context-tool-inspec/gbpdfapgefenggkahomfgkhfehlcenpd) Chrome extension
|
|
257
|
+
2. Enable `chrome://flags/#enable-webmcp-testing`
|
|
258
|
+
3. Run a demo app and open the extension
|
|
259
|
+
4. You should see the registered tools listed with their schemas
|
|
260
|
+
5. Execute tools manually or test with the built-in Gemini agent
|
|
261
|
+
|
|
262
|
+
## How It Works
|
|
263
|
+
|
|
264
|
+
```
|
|
265
|
+
Your React App
|
|
266
|
+
│
|
|
267
|
+
├─ useWebMCPTool() ──► navigator.modelContext.registerTool()
|
|
268
|
+
├─ <WebMCPForm> ──► <form toolname="..." tooldescription="...">
|
|
269
|
+
│
|
|
270
|
+
└─ Browser (Chrome 146+)
|
|
271
|
+
│
|
|
272
|
+
├─ navigator.modelContext ◄── Your tools registered here
|
|
273
|
+
├─ navigator.modelContextTesting ◄── Inspector reads from here
|
|
274
|
+
│
|
|
275
|
+
└─ AI Agent (Gemini, etc.) ◄── Discovers and calls tools
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
## TypeScript
|
|
279
|
+
|
|
280
|
+
The library extends the global `Navigator` interface with `modelContext` and `modelContextTesting` types. Import the types:
|
|
281
|
+
|
|
282
|
+
```typescript
|
|
283
|
+
import type {
|
|
284
|
+
WebMCPToolDefinition,
|
|
285
|
+
JSONSchema,
|
|
286
|
+
ToolAnnotations,
|
|
287
|
+
WebMCPFormSubmitEvent,
|
|
288
|
+
ModelContext,
|
|
289
|
+
} from "react-webmcp";
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
## License
|
|
293
|
+
|
|
294
|
+
MIT
|