Skip to main content
@mcp-b/react-webmcp is the full React surface for MCP-B. It supports tool registration, prompt and resource hooks, sampling and elicitation helpers, and client/provider hooks for consuming MCP servers from a React tree. It expects navigator.modelContext to come from native browser support or from a BrowserMcpServer-based runtime such as @mcp-b/global.

Installation

pnpm add @mcp-b/react-webmcp zod
For client functionality:
pnpm add @mcp-b/transports @modelcontextprotocol/sdk
zod and zod-to-json-schema are optional peer dependencies. They are required only when you pass Zod v3 object maps as inputSchema or outputSchema.

Minimal example

Provider hooks

useWebMCP(config, deps?)

import { useWebMCP } from '@mcp-b/react-webmcp';
The signature matches the strict-core hook from usewebmcp with two package-specific differences:
  1. The implementation field is named handler.
  2. inputSchema and outputSchema also accept Zod v3 object maps.
FieldTypeRequiredDescription
namestringYesUnique tool identifier
descriptionstringYesHuman-readable description
inputSchemaJSON Schema, Standard Schema v1, or Zod v3 object mapNoInput parameters
outputSchemaJsonSchemaObject or Zod v3 object mapNoOutput structure
annotationsToolAnnotationsNoMetadata hints
handler(input)(input) => T | Promise<T>YesTool implementation function
formatOutput(output)(output) => stringNoDeprecated. Prefer outputSchema.
onSuccess(result, input)(result, input) => voidNoSuccess callback
onError(error, input)(error, input) => voidNoError callback
Returns: WebMCPReturn

useWebMCPContext(name, description, getValue)

import { useWebMCPContext } from '@mcp-b/react-webmcp';
Convenience wrapper around useWebMCP for read-only context tools. It automatically sets readOnlyHint: true, idempotentHint: true, destructiveHint: false, and openWorldHint: false.
ParameterTypeDescription
namestringTool identifier
descriptionstringDescription for AI assistants
getValue() => TFunction returning the current context value

useWebMCPPrompt(config)

import { useWebMCPPrompt } from '@mcp-b/react-webmcp';
Registers an MCP prompt on navigator.modelContext.
Config fieldTypeRequiredDescription
namestringYesPrompt identifier
descriptionstringNoHuman-readable description
argsSchemaJSON Schema, Standard Schema v1, or Zod v3 object mapNoArgument schema
get(args)(args) => { messages: PromptMessage[] }YesMessage generator

useWebMCPResource(config)

import { useWebMCPResource } from '@mcp-b/react-webmcp';
Registers an MCP resource on navigator.modelContext.
Config fieldTypeRequiredDescription
uristringYesResource URI or URI template
namestringYesHuman-readable name
descriptionstringNoResource description
mimeTypestringNoMIME type of the resource content
read(uri, params?)(uri: URL, params?) => Promise<{ contents: ResourceContents[] }>YesRead handler

useElicitation(config) / useElicitationHandler(config)

Request user input from the connected MCP client.

useSampling(config) / useSamplingHandler(config)

Request LLM completions from the connected MCP client.

Client hooks

McpClientProvider

import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { McpClientProvider } from '@mcp-b/react-webmcp';
import { TabClientTransport } from '@mcp-b/transports';
Provider component that manages an MCP client connection and shares tools, resources, capabilities, and connection state with descendant components.
PropTypeRequiredDescription
childrenReactNodeYesChild components
clientClientYesMCP client instance
transportTransportYesTransport instance
optsRequestOptionsNoConnection request options

useMcpClient()

import { useMcpClient } from '@mcp-b/react-webmcp';
Accesses the client context. It must be used inside McpClientProvider.
FieldTypeDescription
clientClientMCP client instance
toolsTool[]Available tools from the server
resourcesResource[]Available resources from the server
isConnectedbooleanWhether the client is connected
isLoadingbooleanWhether a connection attempt is in progress
errorError | nullConnection error, if any
capabilitiesServerCapabilities | nullServer-reported capabilities
reconnect()() => Promise<void>Manually reconnect to the server

Zod compatibility

@mcp-b/react-webmcp supports Zod ^3.25 || ^4.0 as an optional peer dependency.
  • Zod v3 object maps are converted to JSON Schema at registration time via zod-to-json-schema.
  • Zod v4 schemas work through the Standard Schema v1 interface.