Skip to main content
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.

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>:
What you get:
  • Self-contained - All dependencies bundled (285KB minified)
  • Auto-initializes - window.navigator.modelContext ready immediately
  • No build step - Just drop it in your HTML
  • Works everywhere - Compatible with all modern browsers
  • Global access - Also exposes window.WebMCP for advanced usage

Via NPM

For applications using a bundler (Vite, Webpack, etc.):

Overview

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
  • 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:
Why this works great:
  • ✅ Simple and intuitive
  • ✅ Automatic cleanup with unregister()
  • ✅ Perfect for React/Vue component-scoped tools
  • ✅ No need to worry about tool lifecycle management
Only use provideContext() when you need to define application-level base tools:
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.
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

Register a single tool dynamically. This is the recommended approach for most use cases. Parameters:
  • tool - A single tool descriptor
Returns:
  • Object with unregister() function to remove the tool
Benefits:
  • ✅ Persist across provideContext() calls
  • ✅ Perfect for component lifecycle management
  • ✅ Fine-grained control over individual tools
  • ✅ Can be unregistered when no longer needed
Example:
Register base/app-level tools. Use sparingly - only for top-level base tools. Parameters:
  • context.tools - Array of tool descriptors
Warning: This replaces all base tools each time it’s called. Tools registered via 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

For more examples, see the Examples page.

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:
  1. Tab Server (TabServerTransport) - For same-window communication (e.g., browser extensions)
  2. Iframe Server (IframeChildTransport) - Auto-enabled when running in an iframe (window.parent !== window)
Both servers expose the same tools, allowing your tools to be accessed from:
  • 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 via registerTool() 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):
In the parent page:

Configuration Options

Use initializeWebModelContext(options) to customize transport behavior:
Disable Tab Server (iframe-only mode):
Disable Iframe Server (tab-only mode):
Custom Transport (advanced):

Data Attribute Configuration

When using the IIFE script tag, configure via data attributes:
Or use JSON for advanced configuration:

Real-World Example: TicTacToe Mini-App

The TicTacToe example demonstrates dual-server mode in production:
The game component registers three tools that become available to both:
  • Tab clients (browser extensions in the same window)
  • Parent page (chat UI via IframeParentTransport)
See the complete TicTacToe example in the mcp-ui-webmcp repository for the full implementation.

Security Best Practices

Production Security:
  • Never use allowedOrigins: ['*'] in production for iframe server
  • Always specify explicit parent origins: allowedOrigins: ['https://trusted-parent.com']
  • Tab server can use ['*'] for same-window clients (less risky)
  • Validate all tool inputs regardless of origin

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.modelContext interface
  • Dynamic Tool Registration - registerTool() with unregister() 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:

@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

License

MIT - see LICENSE for details

Support