Skip to main content
usewebmcp is the strict-core React package. It registers tools with document.modelContext on mount and unregisters them on unmount without adding MCP-B-only hooks. document.modelContext must already exist at runtime through native browser support, @mcp-b/webmcp-polyfill, or @mcp-b/global.

Installation

Minimal example

Hook

useWebMCP(config, deps?)

Returns: WebMCPReturn

Config fields

One of execute or handler is required. If both are present, execute wins.

ToolExecutionState

Type inference

Input inference works with two schema formats:
  • JSON Schema literals with as const
  • Standard Schema v1 implementations such as Zod v4, Valibot, and ArkType
Output inference comes from outputSchema when you pass a JSON Schema literal:
  • the execute or handler return type is inferred from the schema
  • state.lastResult is typed accordingly
  • the MCP response includes structuredContent

Re-registration triggers

The hook unregisters and re-registers when any of these values change:
  • name
  • description
  • inputSchema reference
  • outputSchema reference
  • annotations reference
  • values in deps
These callbacks are stored in refs and do not trigger re-registration:
  • execute
  • handler
  • onSuccess
  • onError
  • formatOutput
Define inputSchema, outputSchema, and annotations outside the component or memoize them. Fresh object references on every render force unnecessary re-registration.

Output schema contract

When outputSchema is defined, the implementation must return a JSON-serializable value that matches that schema for MCP-B helpers that consume it. Object, array, string, number, boolean, and null schemas are supported for type inference and hook result shaping. Native Chrome WebMCP does not currently define or enforce outputSchema; the browser standard tool dictionary defines inputSchema.

Exported types