@mcp-b/global is the MCP-B runtime entry point layered on top of WebMCP. It orchestrates the polyfill, creates a BrowserMcpServer, sets up transport, and replaces document.modelContext with the server instance.
After initialization, document.modelContext still supports the strict WebMCP surface, but it also exposes MCP-B extension methods such as prompts, resources, sampling, elicitation, and transport-facing helpers.
Package selection
For the reasoning behind this split, see Strict Core vs. MCP-B Extensions.
Installation
Minimal example
Auto-initialization
Importing@mcp-b/global in a browser environment auto-initializes document.modelContext. This behavior is controlled by window.__webModelContextOptions.
Functions
initializeWebModelContext(options?)
Initializes the global adapter. Replaces document.modelContext with a BrowserMcpServer instance.
- No-op in non-browser environments (SSR safe).
- Idempotent: calling multiple times after first initialization is a no-op.
- Skipped if
document.modelContextis already aBrowserMcpServer(cross-bundle guard).
- Calls
initializeWebMCPPolyfill()to installdocument.modelContextandnavigator.modelContextTesting(skipped if native API exists). - Saves a reference to the current
document.modelContextasnative. - Creates a
BrowserMcpServerwith{ native }so core operations mirror to the underlying context. - Syncs existing tools from the native/polyfill context into the server.
- Replaces
document.modelContextwith theBrowserMcpServerinstance. - Creates and connects the transport.
cleanupWebModelContext()
Tears down the adapter, closes transport, and restores document.modelContext to its pre-initialization state.
initializeWebModelContext() can be called again to re-initialize.
Configuration
WebModelContextInitOptions
TransportConfiguration
Transport is auto-selected based on context:
Methods on document.modelContext
After initialization, document.modelContext is a BrowserMcpServer instance. It exposes:
Strict WebMCP methods (mirrored to native/polyfill):
Extension methods (MCP-B only):
For the MCP-B-only methods on
document.modelContext, see @mcp-b/webmcp-ts-sdk reference. For the standard-owned browser surface, see WebMCP standard API.
Tool routing
@mcp-b/global mirrors the strict tool lifecycle down to the underlying native or polyfill context so browser-facing tooling can still see registered tools. During initialization it can also backfill existing tools from navigator.modelContextTesting when that shim is available.
Type exports
Related packages
@mcp-b/webmcp-polyfill— Strict core polyfill (used internally)@mcp-b/webmcp-ts-sdk— BrowserMcpServer (used internally)@mcp-b/transports— Tab and iframe transports (used internally)@mcp-b/webmcp-types— Type definitions
