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.Prerequisites
- Modern browser supporting ES2020+ (Chrome, Edge, Firefox, Safari)
- MCP-B Extension for testing tools
- Node.js 18+ and package manager (for NPM method) OR basic HTML knowledge (for script tag method)
Installation
Via IIFE Script Tag (Easiest - No Build Required)
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>:
- ✅ Self-contained - All dependencies bundled (285KB minified)
- ✅ Auto-initializes -
window.navigator.modelContextready immediately - ✅ No build step - Just drop it in your HTML
- ✅ Works everywhere - Compatible with all modern browsers
- ✅ Global access - Also exposes
window.WebMCPfor advanced usage
Via NPM
For applications using a bundler (Vite, Webpack, etc.):Overview
This package implements the W3C Web Model Context API specification, addingnavigator.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
- Production-ready - Used in real-world examples like the TicTacToe mini-app
Tool Registration
The Default: registerTool()
For 99% of use cases, use registerTool() to add tools one at a time:
- ✅ Simple and intuitive
- ✅ Automatic cleanup with
unregister() - ✅ Perfect for React/Vue component-scoped tools
- ✅ No need to worry about tool lifecycle management
Advanced: provideContext() for base tools
Advanced: provideContext() for base tools
Only use Important:
provideContext() when you need to define application-level base tools: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
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.API Reference
navigator.modelContext.registerTool(tool) - Recommended
Register a single tool dynamically. This is the recommended approach for most use cases.
Parameters:
tool- A single tool descriptor
- Object with
unregister()function to remove the tool
- ✅ Persist across
provideContext()calls - ✅ Perfect for component lifecycle management
- ✅ Fine-grained control over individual tools
- ✅ Can be unregistered when no longer needed
navigator.modelContext.provideContext(context) - Use Sparingly
Register base/app-level tools. Use sparingly - only for top-level base tools.
Parameters:
context.tools- Array of tool descriptors
registerTool() are NOT affected.
Example:
Tool Descriptor
Each tool must have:Tool Response Format
Tools must return an object with:Complete Examples
Simple Todo Example
Dynamic Tool Registration (Component Lifecycle)
Perfect for managing tools tied to component lifecycle:Tool Persistence Example
Event-Based Tool Calls (Advanced)
For manifest-based or advanced scenarios, you can handle tool calls as events:Advanced Configuration
Dual-Server Mode (Tab + Iframe)
By default,@mcp-b/global runs two MCP servers that share the same tool registry:
- Tab Server (
TabServerTransport) - For same-window communication (e.g., browser extensions) - Iframe Server (
IframeChildTransport) - Auto-enabled when running in an iframe (window.parent !== window)
- Same-window clients (e.g., browser extension content scripts)
- Parent pages (when your app runs in an iframe)
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.
How It Works
When you register tools viaregisterTool() or provideContext(), they become available on both servers simultaneously:
Usage in Iframes
When your application runs inside an iframe (like in the MCP-UI integration pattern), the iframe server automatically enables: In the iframe (your app):Configuration Options
UseinitializeWebModelContext(options) to customize transport behavior:
Data Attribute Configuration
When using the IIFE script tag, configure via data attributes:Real-World Example: TicTacToe Mini-App
The TicTacToe example demonstrates dual-server mode in production:- Tab clients (browser extensions in the same window)
- Parent page (chat UI via IframeParentTransport)
Security Best Practices
Feature Detection
Check if the API is available:Debugging
In development mode, access the internal bridge:What’s Included
- Web Model Context API - Standard
window.navigator.modelContextinterface - Dynamic Tool Registration -
registerTool()withunregister()function - MCP Bridge - Automatic bridging to Model Context Protocol
- Dual-Server Mode - Tab + Iframe servers sharing the same tool registry
- Tab Transport - Communication layer for same-window contexts
- Iframe Transport - Cross-origin parent-child communication (auto-enabled in iframes)
- Event System - Hybrid tool call handling
- TypeScript Types - Full type definitions included
Security Considerations
Tool Validation
Always validate inputs in your tool implementations:Related Documentation
@mcp-b/react-webmcp
React hooks with automatic lifecycle
@mcp-b/transports
Transport layer implementation
Core Concepts
Two-bucket tool management
Security Guide
Tool security best practices
Quick Start
Get started guide
Examples
Working implementations
External Resources
- @modelcontextprotocol/sdk - Official MCP SDK
- Web Model Context API Explainer - W3C proposal
- Model Context Protocol Spec - Protocol specification
- Microsoft Edge Explainer - Browser integration
