This guide explains the different transport options for connecting to MCP servers with the Agents SDK.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/cloudflare/agents/llms.txt
Use this file to discover all available pages before exploring further.
For a primer on MCP Servers and how they are implemented in the Agents SDK with
McpAgent, see Creating MCP Servers.Streamable HTTP Transport (Recommended)
The Streamable HTTP transport is the recommended way to connect to MCP servers.How it works
When a client connects to your MCP server:Durable Object Connection
The WebSocket connects to your
McpAgent Durable Object which manages connection stateMcpAgent.serve() method:
serve() method returns a Worker with a fetch handler that:
- Handles CORS preflight requests
- Manages WebSocket upgrades
- Routes messages to your Durable Object
Connection from clients
Clients connect using thestreamable-http transport:
SSE Transport (Deprecated)
We also support the legacy SSE (Server-Sent Events) transport, but it is deprecated in favor of Streamable HTTP. If you need SSE transport for compatibility:RPC Transport (Experimental)
The RPC transport is a custom transport designed for internal applications where your MCP server and agent are both running on Cloudflare. They can even run in the same Worker! It sends JSON-RPC messages directly over Cloudflare’s RPC bindings without going over the public internet.Why use RPC transport?
- Faster: No network overhead - direct function calls
- Simpler: No HTTP endpoints, no connection management
- Internal only: Perfect for agents calling MCP servers within the same Worker
Connecting an Agent to an McpAgent via RPC
The RPC transport uses Durable Object bindings to connect yourAgent (MCP client) directly to your McpAgent (MCP server).
Connect your Agent to the MCP server
In your
Agent, call addMcpServer() with the Durable Object binding in onStart():RPC connections are automatically restored after Durable Object hibernation, just like HTTP connections. The binding name and props are persisted to storage so the connection can be re-established without any extra code.Deduplication: For RPC transport, if
addMcpServer is called with a name that already has an active connection, the existing connection is returned instead of creating a duplicate. This makes it safe to call addMcpServer in onStart() without worrying about creating multiple connections on restart.Passing props from client to server
Since RPC transport does not have an OAuth flow, you can pass user context (like userId, role, etc.) directly as props:McpAgent can then access these props:
- Type-safe: TypeScript extracts the Props type from your McpAgent generic
- Persistent: Stored in Durable Object storage via
updateProps() - Available immediately: Set before any tool calls are made
- User authentication context
- Tenant/organization IDs
- Feature flags or permissions
- Any per-connection configuration
How RPC transport works under the hood
When you calladdMcpServer() with a Durable Object binding, the SDK:
- Creates an
RPCClientTransportthat wraps the DO stub - Calls
handleMcpMessage()on theMcpAgentfor each JSON-RPC message - The
McpAgentroutes messages through itsRPCServerTransportto the MCP server - Responses flow back synchronously through the RPC call
- JSON-RPC 2.0 validation (via the MCP SDK’s schema)
- Batch requests
- Notifications (messages without
idfield) - Automatic reconnection after Durable Object hibernation (when called from
onStart())
Configuring RPC Transport Server Timeout
The RPC transport has a configurable timeout for waiting for tool responses. By default, the server will wait 60 seconds for a tool handler to respond. You can customize this by overridinggetRpcTransportOptions() in your McpAgent:
Choosing a transport
| Transport | Use when | Pros | Cons |
|---|---|---|---|
| Streamable HTTP | External MCP servers, production apps | Standard protocol, secure, supports auth | Slight network overhead |
| RPC | Internal agents | Fastest, simplest setup | No auth, Service Bindings only |
| SSE | Legacy compatibility | Backwards compatible | Deprecated, use Streamable HTTP |
Examples
Streamable HTTP
See the MCP example
RPC Transport
See the RPC transport example
MCP Client
See the MCP client example
Next Steps
Creating Servers
Build your own MCP server with the Agents SDK
Connecting Clients
Connect your agent to external MCP servers
Securing Servers
Implement OAuth and security best practices
API Reference
View the full API documentation