Skip to main content
@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.
To prevent auto-initialization:

Functions

initializeWebModelContext(options?)

Initializes the global adapter. Replaces document.modelContext with a BrowserMcpServer instance.
Behavior:
  • No-op in non-browser environments (SSR safe).
  • Idempotent: calling multiple times after first initialization is a no-op.
  • Skipped if document.modelContext is already a BrowserMcpServer (cross-bundle guard).
Initialization sequence:
  1. Calls initializeWebMCPPolyfill() to install document.modelContext and navigator.modelContextTesting (skipped if native API exists).
  2. Saves a reference to the current document.modelContext as native.
  3. Creates a BrowserMcpServer with { native } so core operations mirror to the underlying context.
  4. Syncs existing tools from the native/polyfill context into the server.
  5. Replaces document.modelContext with the BrowserMcpServer instance.
  6. Creates and connects the transport.

cleanupWebModelContext()

Tears down the adapter, closes transport, and restores document.modelContext to its pre-initialization state.
After cleanup, 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