Skip to main content
Register WebMCP tools in any frontend framework by following its native lifecycle patterns. For cross-cutting patterns like conditional registration, error handling, and annotations, see Add Tools to an Existing App.

Install the runtime

React users also need a hook package. Install @mcp-b/react-webmcp (recommended) or usewebmcp alongside your chosen runtime. See Choose a hook package below.

Initialize at your entry point

Import @mcp-b/global once before any component mounts. The import is a side effect that installs document.modelContext.

Register a tool

Each framework has its own lifecycle hooks for mount and unmount. Register tools on mount, unregister on unmount.
Two hook packages are available:Both handle registration on mount and cleanup on unmount automatically.

Create a reusable abstraction

React already has dedicated hook packages (@mcp-b/react-webmcp and usewebmcp), so no custom abstraction is needed. For other frameworks, extract the register/abort lifecycle into a reusable pattern.

Handle SSR

@mcp-b/global accesses browser APIs on import, so SSR frameworks need client-side guards. If you use @mcp-b/webmcp-polyfill instead, it is SSR-safe out of the box and these guards are not needed.
Mark components with 'use client'. For components that access window or document directly, use dynamic imports:

Verify registration

Open the browser console and run:
This returns an array of all registered tools with their names, descriptions, and input schemas. For a richer inspection experience, see Debug and Troubleshoot.