React hooks for Model Context Protocol (MCP) - Let AI agents like Claude, ChatGPT, Cursor, and Copilot control your React components
Why Use @mcp-b/react-webmcp?
| Feature | Benefit |
|---|---|
| React-First Design | Hooks follow React patterns with automatic cleanup and StrictMode support |
| Type-Safe with Zod | Full TypeScript support with Zod schema validation for inputs/outputs |
| Two-Way Integration | Both expose tools TO AI agents AND consume tools FROM MCP servers |
| Execution State Tracking | Built-in loading, success, and error states for UI feedback |
| Works with Any AI | Compatible with Claude, ChatGPT, Gemini, Cursor, Copilot, and any MCP client |
Installation
usewebmcp instead.
For client functionality, you’ll also need:
document.modelContext API. Install @mcp-b/global or use a browser that implements the Web Model Context API.
Provider hooks register tools with document.modelContext.registerTool(tool, { signal }) and abort the controller on unmount. The hooks retain a navigator.modelContext fallback for older preview runtimes, but document.modelContext is the canonical v3 surface. On Chrome Beta 147 native (which ignores the second arg) cleanup cannot remove the tool. Install @mcp-b/global for spec-aligned behavior.
Quick Start - Provider (Registering Tools)
Quick Start - Client (Consuming Tools)
API Overview
Provider Hooks
| Hook | Description |
|---|---|
useWebMCP(config, deps?) | Register a tool with full control over behavior and state |
useWebMCPContext(name, description, getValue) | Simplified hook for read-only context exposure |
Client Hooks
| Hook / Component | Description |
|---|---|
McpClientProvider | Provider component managing an MCP client connection |
useMcpClient() | Access client, tools, connection status, and capabilities |
Zod Version Compatibility
This package supports Zod 3.25.76+ (3.x only).Documentation
For full API reference, output schemas, memoization patterns, migration guide, best practices, and complete examples, see the React WebMCP Guide.Related Packages
@mcp-b/global- W3C Web Model Context API polyfill (required for provider hooks)@mcp-b/transports- Browser-specific MCP transports@mcp-b/chrome-devtools-mcp- Connect desktop AI agents to browser toolsusewebmcp- React hooks for strict core WebMCP API only
