Prerequisites
- React 18+ or React 19 (supports both versions)
- @mcp-b/global package installed
- Zod for schema validation
- MCP-B Extension for testing tools
- Basic understanding of React hooks and async/await
Installation
Features
Provider Hooks (Registering Tools)- Type-safe with Zod validation
- Automatic lifecycle management (StrictMode compatible)
- Execution state tracking for UI feedback
- MCP server connection management
- Real-time tool list updates
- Automatic reconnection handling
Provider API (Registering Tools)
Basic Usage
Read-Only Context
API Reference
useWebMCP(config)
| Option | Type | Required | Description |
|---|---|---|---|
name | string | ✓ | Unique tool identifier |
description | string | ✓ | Human-readable description |
inputSchema | Record<string, ZodType> | - | Zod validation schema |
handler | (input) => Promise<TOutput> | ✓ | Tool execution function |
annotations | ToolAnnotations | - | Metadata hints (readOnlyHint, idempotentHint, etc.) |
formatOutput | (output) => string | - | Custom output formatter |
onError | (error, input) => void | - | Error handler |
useWebMCPContext(name, description, getValue)
Simplified hook for read-only context. Auto-registers and returns current state.
Client API (Consuming Tools)
Basic Usage
API Reference
McpClientProvider
TabClientTransport- Same-page MCP serverExtensionClientTransport- Chrome extension serverInMemoryTransport- Testing
useMcpClient()
Returns:
Complete Example
Best Practices
Tool Naming: Use verb-noun format with domain prefix:posts_like, graph_navigate, table_filter
Annotations: Set readOnlyHint (true for queries), idempotentHint (true if safe to retry), destructiveHint (for deletions)
Error Handling: Throw descriptive errors. Use onError for logging/toasts. Handle connection errors in client components.
Performance: Tools auto-dedupe in StrictMode. Use useWebMCPContext for lightweight read-only data.
Migration from Legacy Package
If you’re migrating from an older version of this package (previously named@mcp-b/mcp-react-hooks):
Before:
McpClientProvider, useMcpClient) remain unchanged.
Related Documentation
@mcp-b/global
Web Model Context API polyfill
@mcp-b/transports
Transport implementations
Core Concepts
Architecture and tool lifecycle
Security Guide
Best practices for tool security
Quick Start
Get started with WebMCP
Examples
Real-world React implementations
