Overview
Concrete WebSocket MCP server running in the Chrome extension that connects to the bridge daemon, receives tool-call requests, executes browser tools, and returns MCP-formatted results.
Description
The WsMcpServer class is the extension-side endpoint of the MCP relay. It connects as a WebSocket client to the daemon's `/extension` endpoint (localhost only), handles `tools/call` JSON-RPC requests by looking up the tool in `allBrowserTools`, executing it via `tool.invoke()`, and converting results to MCP content blocks via `buildMcpContent` (text for JSON, image for base64 data URLs). It supports auto-reconnect with exponential backoff (500ms → 10s with jitter), keepalive via `chrome.alarms`, and URL persistence in `chrome.storage.local`. Connection validation enforces localhost-only URLs.
Usage
- Connect to daemon: `await wsMcpServer.connect("ws://localhost:9223/extension")`
- Disconnect: `await wsMcpServer.disconnect()`
- Check status: `const status = wsMcpServer.getStatus()`
- Status changes: `wsMcpServer.onStatusChange((state) => { ... })`
Code Reference
Source Location
- Repository: AIPex
- File: packages/browser-runtime/src/ws-bridge/ws-mcp-server.ts
- Lines: L85-351 (class), L55-57 (findBrowserTool), L62-83 (buildMcpContent)
- Support: packages/browser-runtime/src/ws-bridge/ws-transport.ts:L45-129 (WebSocketClientTransport)
Signature
export class WsMcpServer {
async connect(url: string): Promise<void>
async disconnect(): Promise<void>
isConnected(): boolean
getStatus(): WsMcpServerState
onStatusChange(listener: StatusListener): () => void
async getSavedUrl(): Promise<string | null>
}
interface WsMcpServerState {
status: ConnectionStatus; // "disconnected" | "connecting" | "connected" | "error"
url: string | null;
error: string | null;
connectedAt: number | null;
reconnectAttempt: number;
}
function findBrowserTool(name: string): FunctionTool | undefined
function buildMcpContent(data: unknown): Array<{ type: string; text?: string; data?: string; mimeType?: string }>
Import
import { WsMcpServer } from "@aipexstudio/browser-runtime";
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| url |
string |
Yes |
WebSocket URL (must be localhost, ws:// or wss://)
|
Outputs
| Name |
Type |
Description
|
| getStatus() |
WsMcpServerState |
Connection state and metadata
|
Usage Examples
Connect Extension to Daemon
import { WsMcpServer } from "@aipexstudio/browser-runtime";
const wsServer = new WsMcpServer();
wsServer.onStatusChange((state) => {
console.log("WS MCP status:", state.status);
updateConnectionUI(state);
});
// Connect to the daemon started by mcp-bridge
await wsServer.connect("ws://localhost:9223/extension");
// The server now listens for tools/call JSON-RPC messages,
// executes them via allBrowserTools, and returns results.
Reconnect Saved URL on Restart
const savedUrl = await wsServer.getSavedUrl();
if (savedUrl) {
await wsServer.connect(savedUrl);
}
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.