Skip to main content
This package provides two sets of hooks: provider hooks for registering your tools with automatic lifecycle management and Zod validation, and client hooks for connecting to MCP servers and calling tools. The hooks handle the boilerplate of tool registration/cleanup, leaving you to focus on tool logic.

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

For client functionality:

Features

Provider Hooks (Registering Tools)
  • Type-safe with Zod validation
  • Automatic lifecycle management (StrictMode compatible)
  • Execution state tracking for UI feedback
Client Hooks (Consuming Tools)
  • 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)

Returns:

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

Available Transports:
  • TabClientTransport - Same-page MCP server
  • ExtensionClientTransport - Chrome extension server
  • InMemoryTransport - 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:
After:
Client hooks (McpClientProvider, useMcpClient) remain unchanged.

@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