webmcp-polyfill

webmcp-polyfill

Enables browsers to act as MCP servers by relaying tools, resources, and prompts to AI agents via a WebSocket-to-stdio bridge.

Category
访问服务器

README

webmcp-polyfill

Bridge the Model Context Protocol (MCP) into the browser. Let web pages register tools, resources, and prompts that AI agents can discover and invoke — just like a native MCP server.

Architecture

┌──────────────┐     stdio (JSON-RPC)     ┌──────────────────┐     WebSocket     ┌──────────────────┐
│  AI Agent    │ ◄──────────────────────► │  Relay Server    │ ◄───────────────► │  Browser Page    │
│  (OpenCode)  │                          │  (src/server.ts) │                   │  (BrowserMCPHost) │
└──────────────┘                          │  port :3098      │                   └──────────────────┘
                                          └──────────────────┘
  • Relay Server — a Bun process that speaks MCP over stdio to the AI agent, while running a WebSocket server that browser pages connect to.
  • Browser Host (BrowserMCPHost) — a WebSocket client with zero Node dependencies. Uses the native browser WebSocket API.
  • Server Host (MCPHost) — a standalone host that listens on a TCP port for direct WebSocket connections (no relay needed).
  • Core Registries — pure TypeScript in-memory registries (ToolRegistry, ResourceRegistry, PromptRegistry) shared between server and browser.

How it works

  1. The relay server starts as a standard MCP stdio server (@modelcontextprotocol/sdk).
  2. A browser page creates a BrowserMCPHost, registers tools, and connects to the relay via WebSocket.
  3. On connection, the relay discovers all browser-side tools via tools/list and merges them into its own registry.
  4. The AI agent sees a unified tool list. When it invokes a browser tool, the relay proxies the tools/call over WebSocket to the browser.
  5. When the browser disconnects, its tools are automatically cleaned up.

Relationship to WebMCP / MCP

This project polyfills MCP into environments that can't run a TCP server — specifically browsers. Under the standard MCP model, a tool host must listen on a socket or pipe. A browser tab can't do that. webmcp-polyfill inverts the connection: the relay listens, and browsers connect as clients. The relay then translates between the agent's stdio MCP transport and the browser's WebSocket transport.

The webmcp_* prefixed tools (webmcp_resources_list, webmcp_resources_read, webmcp_prompts_list, webmcp_prompts_get) are bridge tools that expose resources and prompts through the MCP tool interface, since MCP stdio clients primarily support the tools/ namespace.

Use Cases

  • Web-based devtools — register browser extension tools (DOM inspection, performance profiling, screenshot capture) that AI agents can call directly.
  • In-browser automation — let an agent control a web app by invoking tools that click buttons, fill forms, or navigate pages.
  • Live dashboards — expose browser-side data (WebSocket feeds, canvas state, WebGL stats) as MCP resources readable by agents.
  • Hybrid workflows — combine server-side tools (file system, database) with browser-side tools (page state, user interactions) in a single session.
  • Prototyping MCP servers — use the browser as a quick REPL to register and test tools without setting up a Node/Bun project.

Quick Start

# Install dependencies
bun install

# Generate self-signed TLS certs (for WSS support)
bun run gen-cert

# Start the relay server (MCP over stdio)
bun run mcp

# In another terminal, start the browser demo
bun run web

Minimal browser host

import { BrowserMCPHost } from "webmcp-polyfill/sdk/browser";

const host = new BrowserMCPHost("ws://localhost:3098");

host.tool("greet", {
  description: "Greet someone by name",
  inputSchema: {
    type: "object",
    properties: { name: { type: "string" } },
    required: ["name"],
  },
  execute: async (args) => {
    return { greeting: `Hello, ${args.name}!` };
  },
});

host.connect();

Minimal server host

import { MCPHost } from "webmcp-polyfill/sdk";

const host = new MCPHost({ port: 3098 });

host.tool("add", {
  description: "Add two numbers",
  inputSchema: {
    type: "object",
    properties: { a: { type: "number" }, b: { type: "number" } },
    required: ["a", "b"],
  },
  execute: async ({ a, b }) => a + b,
});

host.listen();

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选