Skip to main content
@mcp-b/webmcp-polyfill installs the strict WebMCP runtime on navigator.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 navigator.modelContext.
Behavior:
  • No-op in non-browser environments.
  • Non-destructive: if navigator.modelContext already 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 navigator.modelContext and navigator.modelContextTesting property descriptors and resets the polyfill install state.

IIFE / script tag

The IIFE build auto-initializes on load. Configure via window.__webMCPPolyfillOptions:

Methods on navigator.modelContext

After initialization, navigator.modelContext exposes these methods:

registerTool(tool)

Adds a single tool to the registry.
  • Requires a non-empty name, non-empty description, and execute function.
  • Throws on duplicate tool names.
  • If inputSchema is omitted, defaults to { type: "object", properties: {} }.

unregisterTool(name)

Removes a tool by name. Unknown names are a no-op.
The polyfill does not provide listTools() or callTool() on navigator.modelContext. For tool listing and execution, use navigator.modelContextTesting (when the testing shim is enabled) or switch to @mcp-b/global.

Testing shim

When installTestingShim is enabled, the polyfill installs navigator.modelContextTesting with the standard testing API:
For full details on the testing API, see WebMCP standard API.

Input schema support

The polyfill accepts three formats for inputSchema: 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:
For full inference documentation, see @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. For the layering rationale, see Native vs Polyfill vs Global.