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
Promise<ElicitResult> - User’s response or cancellation
getTransportType()
Get the transport type for this MCP Agent instance."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.string - Session ID
getWebSocket()
Get the unique WebSocket connection (SSE transport only).Connection | null - WebSocket connection or null
getRpcTransportOptions()
Override to customize RPC transport behavior (e.g., timeout).RPCServerTransportOptions
Static Methods
serve()
Create a fetch handler for the MCP server.string
required
URL path to serve the MCP server on
ServeOptions
serveSSE()
Create a fetch handler for SSE transport (legacy).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
Related
- MCPClientManager - Connect to MCP servers from Agents
- Agent Class - Base Agent class
- MCP SDK - Model Context Protocol