Install
openclaw skills install web-mcpWebMCP - Enable AI agents to interact with your web applications through structured tools. Implements the WebMCP standard for Next.js/React apps with tool registration, event bridge pattern, and contextual tool loading. Use when building AI-agent accessible web apps, adding MCP capabilities to existing projects, or creating tool-based interfaces for AI interactions.
openclaw skills install web-mcpEnable AI agents to interact with your web applications through structured tools. WebMCP provides a clean, self-documenting interface between AI agents and your web app.
WebMCP is a web standard that gives AI agents an explicit, structured contract for interacting with websites. Instead of screen-scraping or brittle DOM selectors, a WebMCP-enabled page exposes tools — each with:
# Initialize WebMCP in your Next.js project
webmcp init
# Add a new tool
webmcp add-tool searchProducts
# Generate TypeScript types
webmcp generate-types
const searchTool = {
name: "searchProducts",
description: "Search for products by query",
inputSchema: {
type: "object",
properties: {
query: { type: "string", description: "Search query" }
},
required: ["query"]
},
outputSchema: { type: "string" },
execute: async (params) => {
// Implementation
},
annotations: {
readOnlyHint: "true"
}
};
Tools are registered when components mount and unregistered when they unmount:
useEffect(() => {
registerSearchTools(); // Tools appear to agent
return () => {
unregisterSearchTools(); // Tools disappear
};
}, []);
Tools communicate with React through CustomEvents:
Agent → execute() → dispatch CustomEvent → React updates → signal completion → Agent receives result
┌─────────────────────────────────────────┐
│ Browser (navigator.modelContext) │
│ │
│ ┌───────────┐ registers/ ┌────┐ │
│ │ AI Agent │◄──unregisters────│web │ │
│ │ (Claude) │ tools │mcp│ │
│ │ │ │.ts│ │
│ │ calls─────┼─────────────────►│ │ │
│ └───────────┘ └──┬─┘ │
│ │ │
│ CustomEvent│ │
│ dispatch │ │
│ ▼ │
│ ┌──────────────────────────────────┐ │
│ │ React Component Tree │ │
│ │ │ │
│ │ ┌──────────┐ ┌──────────┐ │ │
│ │ │/products │ │ /cart │ │ │
│ │ │useEffect:│ │useEffect:│ │ │
│ │ │ register │ │ register │ │ │
│ │ │ search │ │ cart │ │ │
│ │ │ tools │ │ tools │ │ │
│ │ └──────────┘ └──────────┘ │ │
│ └──────────────────────────────────┘ │
└─────────────────────────────────────────┘
# In your Next.js project
npx webmcp init
# Or install globally
npm install -g @webmcp/cli
webmcp init
webmcp init
This creates:
lib/webmcp.ts - Core implementationhooks/useWebMCP.ts - React hookcomponents/WebMCPProvider.tsx - Provider component// lib/webmcp.ts
export const searchProductsTool = {
name: "searchProducts",
description: "Search for products",
execute: async (params) => {
return dispatchAndWait("searchProducts", params, "Search completed");
},
inputSchema: {
type: "object",
properties: {
query: { type: "string" }
},
required: ["query"]
},
annotations: { readOnlyHint: "true" }
};
// app/products/page.tsx
"use client";
import { useEffect, useState } from "react";
import { registerProductTools, unregisterProductTools } from "@/lib/webmcp";
export default function ProductsPage() {
const [results, setResults] = useState([]);
const [completedRequestId, setCompletedRequestId] = useState(null);
// Signal completion after render
useEffect(() => {
if (completedRequestId) {
window.dispatchEvent(
new CustomEvent(`tool-completion-${completedRequestId}`)
);
setCompletedRequestId(null);
}
}, [completedRequestId]);
// Register tools + listen for events
useEffect(() => {
const handleSearch = (event: CustomEvent) => {
const { requestId, query } = event.detail;
// Perform search
setResults(searchProducts(query));
if (requestId) setCompletedRequestId(requestId);
};
window.addEventListener("searchProducts", handleSearch);
registerProductTools();
return () => {
window.removeEventListener("searchProducts", handleSearch);
unregisterProductTools();
};
}, []);
return <div>{/* Product UI */}</div>;
}
| Command | Description |
|---|---|
webmcp init | Initialize WebMCP in project |
webmcp add-tool <name> | Add new tool definition |
webmcp generate-types | Generate TypeScript types |
webmcp example <type> | Create example project |
{
name: "viewCart",
description: "View cart contents",
annotations: { readOnlyHint: "true" }
}
{
name: "addToCart",
description: "Add item to cart",
annotations: { readOnlyHint: "false" }
}
{
name: "setFilters",
inputSchema: {
type: "object",
properties: {
category: { type: "string", enum: ["electronics", "clothing"] },
maxPrice: { type: "number" }
}
}
}
webmcp example e-commerce
Features:
webmcp example dashboard
Features:
webmcp example blog
Features:
Use camelCase verbs that describe the action:
searchProductsaddToCartupdateProfileproduct_searchhandleCartWrite clear, specific descriptions:
Always include descriptions for parameters:
properties: {
query: {
type: "string",
description: "The search query to find products by name or category"
}
}
Register tools only when relevant:
// Product page
useEffect(() => {
registerProductTools();
return () => unregisterProductTools();
}, []);
// Cart page
useEffect(() => {
registerCartTools();
return () => unregisterCartTools();
}, []);
Always handle timeouts and errors:
async function execute(params) {
try {
return await dispatchAndWait("action", params, "Success", 5000);
} catch (error) {
return `Error: ${error.message}`;
}
}
WebMCP requires browsers that support:
For development, use the WebMCP polyfill:
import "@webmcp/polyfill";