@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:navigator.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 navigator.modelContext API. When anative context is provided, operations mirror to it.
registerTool(tool)
Registers a single tool. Returns { unregister: () => void }.
- Mirrors to
native.registerTool()first (ifnativeis 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)
Removes a tool by name. Also calls native.unregisterTool(name) when native is provided.
Extension methods
These methods are not part of the WebMCP specification. They are MCP-B extensions.listTools()
Returns ToolListItem[] for all enabled registered tools.
callTool(params)
Executes a registered tool by name. Returns Promise<ToolResponse>.
executeTool(name, args?)
Convenience wrapper for callTool. Returns Promise<ToolResponse>.
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>.
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.
connect method:
- Sets up tool, resource, and prompt request handlers.
- Replaces upstream handlers that expect Zod schemas with handlers that support plain JSON Schema.
- 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.
syncNativeTools()
Convenience method that calls backfillTools using the native context’s listTools() and callTool().
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)McpServeraliased toBrowserMcpServerTransportinterfacemergeCapabilitieshelper- Protocol version constants
- Request/response schemas
Related packages
@mcp-b/global— UsesBrowserMcpServerinternally@mcp-b/transports— Tab and iframe transports@mcp-b/webmcp-types— Type definitions@modelcontextprotocol/sdk— Upstream MCP SDK
