@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, application code continues through the package-supported document.modelContext surface. The installed runtime object also supports MCP-B extensions, but the global TypeScript declaration stays limited to the package’s browser contracts. Narrow document.modelContext explicitly when you opt into those 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.
Configure before the IIFE
Disable auto-initialization
Functions
initializeWebModelContext(options?)
Initializes the global adapter and installs it on document.modelContext.
Initializer signature
Manual initialization
- Returns nothing in every environment.
- Does nothing outside a secure browser environment (SSR safe).
- Repeated calls and calls after another bundle initialized the runtime are no-ops.
- Returns before native-tool synchronization and transport connection finish.
- Continues to transport connection when initial native-tool synchronization fails.
- Logs a transport connection failure, starts closing the adapter, and restores the previous context.
BrowserMcpServer, replaces document.modelContext, then starts native-tool
reconciliation and transport connection asynchronously. See Runtime
layering for why the wrapper is
installed without changing application usage.
cleanupWebModelContext()
Restores the native or polyfilled context immediately and starts closing the adapter and transport. It does not uninstall @mcp-b/webmcp-polyfill; the polyfill owns its lifecycle separately.
Cleanup
initializeWebModelContext() can be called again to re-initialize.
Configuration
WebModelContextInitOptions
TransportConfiguration
Transport is auto-selected based on context:
When omitted,
allowedOrigins defaults to ['*'] on the selected transport. Disabling both eligible transports makes initialization throw before document.modelContext is replaced.
Manual transport configuration
autoInitialize before importing the package. Static imports run package initialization before the importing module’s body.
Canonical document surface
After initialization, usedocument.modelContext for browser-facing WebMCP registration, discovery, and events. The installed runtime exposes:
WebMCP tool members (mirrored to native/polyfill):
Serialized execution compatibility:
Extension methods (MCP-B only):
TypeScript users can narrow the canonical surface without widening the global declaration:
Narrow MCP-B extensions
@mcp-b/webmcp-ts-sdk as a direct dependency when importing its guard or extension types.
Sampling and elicitation are not direct BrowserMcpServer methods. Legacy push-style SDK APIs live on modelContext.mcpServer.server after narrowing. They remain sensitive to the negotiated protocol revision.
For the MCP-B-only server members, see @mcp-b/webmcp-ts-sdk reference. For the proposed browser surface, see WebMCP API sources.
Tool routing
@mcp-b/global mirrors registrations down to the underlying native or polyfill context so browser-facing tooling can still see them. Initial reconciliation uses the underlying context’s asynchronous getTools() method and requires its descriptor-based, serialized-JSON executeTool() signature. Later toolchange events trigger another reconciliation. The MCP-B testing shim is not a native-tool discovery source.
The live WebMCP proposal currently specifies an object-input executeTool() signature. This reference describes the package behavior.
Type exports
Type imports
Related packages
@mcp-b/webmcp-polyfill— Tool registration and discovery 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
