@agentnext/webmcp 0.1.1 → 0.1.3
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/README.md +94 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# @agentnext/webmcp
|
|
2
|
+
|
|
3
|
+
Expose AgentNext actions as WebMCP browser tools — register actions with `document.modelContext` so AI clients can discover and invoke them directly from the browser.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Converts `ActionDefinition` to a WebMCP-compatible tool registration
|
|
8
|
+
- Zod → JSON Schema conversion via `zod-to-json-schema`
|
|
9
|
+
- Safe async execution wrapper (catches errors, returns structured output)
|
|
10
|
+
- Gracefully no-ops in non-browser environments or when WebMCP is unavailable
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @agentnext/webmcp @agentnext/core zod
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### Register a single action with WebMCP
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { z } from "zod";
|
|
24
|
+
import { action } from "@agentnext/core";
|
|
25
|
+
import { registerActionWithWebMCP } from "@agentnext/webmcp";
|
|
26
|
+
|
|
27
|
+
const addToCart = action({
|
|
28
|
+
name: "add_to_cart",
|
|
29
|
+
description: "Adds a product to the shopping cart by product ID and quantity.",
|
|
30
|
+
input: z.object({
|
|
31
|
+
productId: z.string(),
|
|
32
|
+
quantity: z.number().int().min(1).default(1),
|
|
33
|
+
}),
|
|
34
|
+
execute: async ({ productId, quantity }) => {
|
|
35
|
+
const result = await cart.add(productId, quantity);
|
|
36
|
+
return result;
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Registers the action with document.modelContext if WebMCP is available
|
|
41
|
+
await registerActionWithWebMCP(addToCart);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Checking if WebMCP is supported
|
|
45
|
+
|
|
46
|
+
`registerActionWithWebMCP` returns `{ supported: false }` when `document.modelContext` is not present (e.g. server-side rendering, or a browser without WebMCP). You can use this to branch logic:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
const result = await registerActionWithWebMCP(addToCart);
|
|
50
|
+
|
|
51
|
+
if (!result?.supported) {
|
|
52
|
+
console.log("WebMCP is not available in this environment.");
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Registering multiple actions at startup
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { registerActionWithWebMCP } from "@agentnext/webmcp";
|
|
60
|
+
import { addToCart, removeFromCart, checkout } from "./actions";
|
|
61
|
+
|
|
62
|
+
// Register all actions when the app initialises
|
|
63
|
+
await Promise.all([
|
|
64
|
+
registerActionWithWebMCP(addToCart),
|
|
65
|
+
registerActionWithWebMCP(removeFromCart),
|
|
66
|
+
registerActionWithWebMCP(checkout),
|
|
67
|
+
]);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Using with `@agentnext/react`
|
|
71
|
+
|
|
72
|
+
The `useAgentAction` hook from `@agentnext/react` calls `registerActionWithWebMCP` automatically — you don't need to call it manually in React apps:
|
|
73
|
+
|
|
74
|
+
```tsx
|
|
75
|
+
import { useAgentAction } from "@agentnext/react";
|
|
76
|
+
import { addToCart } from "./actions";
|
|
77
|
+
|
|
78
|
+
export function CartPage() {
|
|
79
|
+
useAgentAction(addToCart); // registered with WebMCP automatically
|
|
80
|
+
return <div>Cart</div>;
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### How it works
|
|
85
|
+
|
|
86
|
+
1. Your `ActionDefinition.input` (a Zod schema) is converted to JSON Schema using `zod-to-json-schema`.
|
|
87
|
+
2. The resulting tool descriptor is passed to `document.modelContext.registerTool()`.
|
|
88
|
+
3. When an AI client invokes the tool, the `execute` wrapper validates input, runs your handler, and returns a JSON string (or error object) safe for LLM consumption.
|
|
89
|
+
|
|
90
|
+
## API Reference
|
|
91
|
+
|
|
92
|
+
| Export | Description |
|
|
93
|
+
| ---------------------------------- | --------------------------------------------------------------------------- |
|
|
94
|
+
| `registerActionWithWebMCP(action)` | Registers an `ActionDefinition` as a WebMCP tool. Returns a promise resolving to the registration result, or `{ supported: false }` if WebMCP is unavailable. |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentnext/webmcp",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "WebMCP adapter for AgentNext — registers actions with document.modelContext",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "AgentNext",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"prepublishOnly": "npm run build"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@agentnext/core": "^0.1.
|
|
37
|
+
"@agentnext/core": "^0.1.3",
|
|
38
38
|
"zod-to-json-schema": "^3.23.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|