Skip to main content
@mcp-b/webmcp-ts-sdk adapts the official @modelcontextprotocol/sdk for browser environments. It provides BrowserMcpServer, a class that extends the upstream McpServer to support dynamic tool registration after transport connection.
Most applications should use @mcp-b/global instead of this package directly. @mcp-b/global creates and manages a BrowserMcpServer internally. Use this package only when you need direct control over the MCP server instance.

Minimal example


Why this package exists

The official MCP SDK throws an error when registering server capabilities after a transport connection:
In browser environments, tools arrive dynamically as pages load. The transport must be connected at startup, but tools are registered later via document.modelContext.registerTool(). BrowserMcpServer solves this by pre-registering tool capabilities in the constructor, before the transport connects.

BrowserMcpServer

Constructor

BrowserMcpServerOptions

Extends ServerOptions from the upstream SDK.

WebMCP standard methods

These methods implement the document.modelContext API. When a native context is provided, operations mirror to it.

registerTool(tool, options?)

Registers a single tool. Returns Promise<void>. Pass options.signal to remove the tool later.
  • Mirrors to native.registerTool() first (if native is provided).
  • Rolls back the native registration if the server-side registration fails.
  • Streamed tools (stream: true) are not yet supported and throw an error.

unregisterTool(name) (deprecated)

Removes a tool by name for older MCP-B integrations. Prefer registerTool(tool, { signal }) and call AbortController.abort().

Extension methods

These methods are not part of the WebMCP specification. They are MCP-B extensions.

listTools()

Returns ToolListItem[] for all enabled registered tools.

getTools()

Returns WebMCP producer tool descriptors for registered tools.

executeTool(tool, inputArgsJson)

Executes a tool descriptor returned from getTools(). Returns Promise<string | null>.

executeTool(name, args?) and callTool(params) (deprecated)

These MCP-B compatibility helpers execute tools by name and return Promise<ToolResponse>. Prefer getTools() and executeTool(tool, inputArgsJson) for WebMCP DOM callers. MCP transport clients still use the MCP SDK client.callTool(...) API.

Resource methods

registerResource(descriptor)

Registers an MCP resource. Returns { unregister: () => void }.

listResources()

Returns metadata for all enabled resources.

readResource(uri)

Reads a resource by URI. Throws if the resource is not found.

Prompt methods

registerPrompt(descriptor)

Registers an MCP prompt. Returns { unregister: () => void }.

listPrompts()

Returns metadata for all enabled prompts, including argument schemas.

getPrompt(name, args?)

Retrieves a prompt by name. Validates args against the schema if present.

Sampling and elicitation

createMessage(params, options?)

Requests LLM sampling from the connected client. Returns Promise<CreateMessageResult>.
Default timeout: 10 seconds (via AbortSignal.timeout).

elicitInput(params, options?)

Requests user input from the connected client. Returns Promise<ElicitResult>.

Transport connection

connect(transport)

Connects to an MCP transport. Sets up request handlers before connection to avoid the “cannot register after connect” restriction.
The connect method:
  1. Sets up tool, resource, and prompt request handlers.
  2. Replaces upstream handlers that expect Zod schemas with handlers that support plain JSON Schema.
  3. Calls super.connect(transport) to establish the connection.

Schema handling

BrowserMcpServer accepts both Zod schemas and plain JSON Schema objects. The handling depends on the schema format: Input schemas without a type field receive type: "object" automatically. Empty {} schemas default to { type: "object", properties: {} }.

backfillTools(tools, execute)

Registers tools that were already present on the native/polyfill context before this server was created. Skips tools that are already registered on the server.
Returns the number of tools synced.

syncNativeTools()

Backfills tools already present on the provided native context. Current native contexts are read through getTools() and executeTool(tool, inputArgsJson). Older compatibility contexts exposing listTools() and callTool() are still supported.

Re-exports

This package re-exports types, classes, and utilities from @modelcontextprotocol/sdk:
  • All MCP protocol types (Tool, Resource, Prompt, etc.)
  • Server (base server class, unchanged)
  • McpServer aliased to BrowserMcpServer
  • Transport interface
  • mergeCapabilities helper
  • Protocol version constants
  • Request/response schemas