W3C Web Model Context API polyfill implementing navigator.modelContext. Turn JavaScript functions into AI-accessible tools with registerTool() for browser-based agent integration.
This package implements the W3C Web Model Context API specification, making navigator.modelContext available in your site. Instead of creating complex installation steps, it works out of the box with automatic detection of whether your app is running standalone or embedded in an iframe.
Quick Start: Use registerTool() to add tools. It’s simple, automatic, and works everywhere.
The IIFE (Immediately Invoked Function Expression) version bundles everything into a single file and auto-initializes when loaded. Perfect for simple HTML pages or prototyping.Add the script to your HTML <head>:
Copy
<!DOCTYPE html><html><head> <!-- IIFE version - bundles all dependencies, auto-initializes --> <script src="https://unpkg.com/@mcp-b/global@latest/dist/index.iife.js"></script></head><body> <h1>My AI-Powered App</h1> <script> // window.navigator.modelContext is already available! // Register tools for AI agents (recommended approach) window.navigator.modelContext.registerTool({ name: "get-page-title", description: "Get the current page title", inputSchema: { type: "object", properties: {} }, async execute() { return { content: [{ type: "text", text: document.title }] }; } }); </script></body></html>
What you get:
✅ Self-contained - All dependencies bundled (285KB minified)
This package implements the W3C Web Model Context API specification, adding navigator.modelContext to your website. Your JavaScript functions become tools that AI agents can discover and call.Key Features:
Automatic dual-server mode - Tools accessible from both same-window clients AND parent pages (when in iframe)
Zero configuration - Works out of the box, detects iframe context automatically
Important:provideContext() replaces all base tools each time it’s called. Most developers should use registerTool() instead.See Two-Bucket Tool Management below for details on how these work together.
Implementation Detail: Two-Bucket Tool Management
WebMCP uses a two-bucket system: base tools (via provideContext()) and dynamic tools (via registerTool()). Dynamic tools persist independently, making them perfect for component lifecycle management. See Tool Registration for details.
This dual-server architecture is automatic - no configuration needed for basic usage. The package detects when it’s running in an iframe and enables both servers automatically.
When your application runs inside an iframe (like in the MCP-UI integration pattern), the iframe server automatically enables:In the iframe (your app):
Copy
import { initializeWebModelContext } from '@mcp-b/global';// Initialize with default dual-server modeinitializeWebModelContext({ transport: { tabServer: { allowedOrigins: ['*'], // Allow connections from same window }, // iframeServer auto-enabled when window.parent !== window },});// Now register your toolsnavigator.modelContext.registerTool({ name: "tictactoe_get_state", description: "Get current game state", inputSchema: { type: "object", properties: {} }, async execute() { // Your game logic here return { content: [{ type: "text", text: "Game state: X's turn" }] }; }});
In the parent page:
Copy
import { IframeParentTransport } from '@mcp-b/transports';import { Client } from '@modelcontextprotocol/sdk/client/index.js';const iframe = document.querySelector('iframe');// Connect to iframe's MCP serverconst client = new Client({ name: 'parent', version: '1.0.0' });const transport = new IframeParentTransport({ iframe: iframe, targetOrigin: 'https://iframe-app.com',});await client.connect(transport);// Discover tools from iframeconst { tools } = await client.listTools();console.log('Tools from iframe:', tools);// Output: [{ name: 'tictactoe_get_state', ... }]
if ("modelContext" in navigator) { // API is available navigator.modelContext.provideContext({ tools: [...] });} else { console.warn("Web Model Context API not available");}