@mcp-b/webmcp-polyfill installs the strict WebMCP runtime on document.modelContext when no native implementation exists. It is the package to use when you want the browser standard in browsers today without MCP-B extensions.
Package selection
Minimal example
Functions
initializeWebMCPPolyfill(options?)
Installs the strict core polyfill on document.modelContext.
- No-op in non-browser environments.
- Non-destructive: if
document.modelContextalready exists (native or from a previous install), initialization is skipped. - Safe to call repeatedly.
Options
installTestingShim values
initializeWebModelContextPolyfill(options?)
Alias for initializeWebMCPPolyfill.
cleanupWebMCPPolyfill()
Restores previous document.modelContext and navigator.modelContextTesting property descriptors and resets the polyfill install state.
IIFE / script tag
The IIFE build auto-initializes on load. Configure viawindow.__webMCPPolyfillOptions:
Methods on document.modelContext
After initialization, document.modelContext exposes these methods:
registerTool(tool, options?)
Adds a single tool to the registry.
- Requires a non-empty
name, non-emptydescription, andexecutefunction. - Throws on duplicate tool names.
- If
inputSchemais omitted, defaults to{ type: "object", properties: {} }. - Pass
options.signalto remove the tool when the signal aborts.
getTools()
Returns Promise<ModelContextToolInfo[]>. Each tool includes a JSON-stringified inputSchema, origin, and window.
executeTool(tool, inputArgsJson, options?)
Executes a tool descriptor returned from getTools(). The input arguments are a JSON object string. The return value is Promise<string | null>.
unregisterTool(name) (deprecated)
Removes a tool by name for older MCP-B integrations. Unknown names are a no-op. Prefer registerTool(tool, { signal }).
The polyfill does not provide MCP-B extension methods such as
listTools() or callTool() on
document.modelContext. For those helpers, use @mcp-b/global.Testing shim
WheninstallTestingShim is enabled, the polyfill installs navigator.modelContextTesting with the standard testing API:
Input schema support
The polyfill accepts three formats forinputSchema:
Standard JSON Schema conversion is attempted with targets
draft-2020-12 first, then draft-07. When both Standard validator and Standard JSON Schema are present on the same object, JSON conversion is preferred.
Validation
The polyfill validates tool descriptors on registration:
Input arguments are validated at execution time using JSON Schema validation (via
@cfworker/json-schema). Standard Schema validators are used when available.
Type inference
For compile-time type inference, pair the polyfill with@mcp-b/webmcp-types:
Interop with @mcp-b/global
If the polyfill is installed first,@mcp-b/global wraps the existing context with BrowserMcpServer to add MCP-B extension APIs without replacing the core object identity. Use @mcp-b/global directly when you need callTool, resources, prompts, or transport. For guidance on choosing between them, see Choose a Runtime.