Skip to main content

Overview

McpAgent extends the Agent class to provide a foundation for building MCP servers. It handles transport initialization, session management, and client communication.

Type Parameters

Cloudflare.Env
default:"Cloudflare.Env"
Environment type containing bindings
unknown
default:"unknown"
State type for the Agent
Record<string, unknown>
default:"Record<string, unknown>"
Props passed to the Agent

Abstract Members

server

MaybePromise<McpServer | Server>
required
The MCP server instance. Can be a Server or McpServer from the MCP SDK.

init()

() => Promise<void>
required
Initialize the MCP server. Called on Agent start. Set up request handlers here.

Methods

elicitInput()

Request user input with a message and schema (elicitation protocol).
ElicitInputParams
required
string
required
Message to show the user
unknown
required
JSON schema for the expected input
Returns: Promise<ElicitResult> - User’s response or cancellation

getTransportType()

Get the transport type for this MCP Agent instance.
Returns: "sse" | "streamable-http" | "rpc"
The transport type is determined by the naming scheme: sse:${sessionId}, streamable-http:${sessionId}, or rpc:${sessionId}.

getSessionId()

Get the session ID for this MCP Agent instance.
Returns: string - Session ID

getWebSocket()

Get the unique WebSocket connection (SSE transport only).
Returns: Connection | null - WebSocket connection or null

getRpcTransportOptions()

Override to customize RPC transport behavior (e.g., timeout).
Returns: RPCServerTransportOptions

Static Methods

serve()

Create a fetch handler for the MCP server.
string
required
URL path to serve the MCP server on
ServeOptions
string
default:"MCP_OBJECT"
Name of the Durable Object binding in wrangler.jsonc
'streamable-http' | 'sse'
default:"streamable-http"
MCP transport mode
CorsOptions
CORS configuration
DurableObjectJurisdiction
Durable Object jurisdiction
Returns: Fetch handler object

serveSSE()

Create a fetch handler for SSE transport (legacy).
Returns: Fetch handler object

Lifecycle

onStart()

Called when the Agent starts. Sets up the MCP transport and connects the server.

onConnect()

Validates new WebSocket connections for MCP protocol.

Transport Types

Streamable HTTP

Recommended transport for modern MCP clients.

SSE (Legacy)

Server-Sent Events transport for older clients.

RPC (Durable Object)

Direct Durable Object binding for internal MCP servers.

Full Example

wrangler.jsonc Configuration