Overview
The MCP client capability lets your agent:- Connect to external MCP servers - GitHub, Slack, databases, AI services
- Use their tools - Call functions exposed by MCP servers
- Access resources - Read data from MCP servers
- Use prompts - Leverage pre-built prompt templates
This page covers connecting to MCP servers as a client. To create your own MCP server, see Creating MCP Servers.
Quick Start
Adding MCP Servers
UseaddMcpServer() to connect to an MCP server:
Basic Usage
Transport Options
MCP supports multiple transport types:- Streamable HTTP (Recommended)
- SSE (Legacy)
- Auto-detect
Custom Headers
For servers behind authentication (like Cloudflare Access) or using bearer tokens:Retry Options
Configure retry behavior for connection and reconnection attempts:URL Security
MCP server URLs are validated before connection to prevent Server-Side Request Forgery (SSRF). The following URL targets are blocked:- Private/internal IP ranges (RFC 1918:
10.x,172.16-31.x,192.168.x) - Loopback addresses (
127.x,::1) - Link-local addresses (
169.254.x,fe80::) - Cloud metadata endpoints (
169.254.169.254)
Return Value
addMcpServer() returns the connection state:
ready- Server connected and tools discoveredauthenticating- Server requires OAuth; redirect user toauthUrl
OAuth Authentication
Many MCP servers require OAuth authentication. The agent handles the OAuth flow automatically.How It Works
Handling OAuth in Your Agent
OAuth Callback
The callback URL is automatically constructed:https://my-worker.workers.dev/agents/my-agent/default/callback
OAuth tokens are securely stored in SQLite and persist across agent restarts.
Custom Callback Handling
For custom OAuth completion behavior:Custom OAuth Provider
By default, agents use dynamic client registration to authenticate with MCP servers. If you need to use a different OAuth strategy — such as pre-registered client credentials, mTLS-based authentication, or other mechanisms — override thecreateMcpOAuthProvider method in your agent subclass:
AgentMcpOAuthProvider interface, which extends the MCP SDK’s OAuthClientProvider with additional properties (authUrl, clientId, serverId) and methods (checkState, consumeState, deleteCodeVerifier) used by the agent’s MCP connection lifecycle.
Custom storage backend
The most common customization is using a different storage backend while keeping the built-in OAuth logic (CSRF state, PKCE, nonce generation, token management). ImportDurableObjectOAuthClientProvider and pass your own storage adapter:
Using MCP Capabilities
Once connected, access the server’s capabilities:Getting Available Tools
Resources and Prompts
Server Status
Integration with AI SDK
To use MCP tools with the Vercel AI SDK, usethis.mcp.getAITools() which converts MCP tools to AI SDK format:
getMcpServers().tools returns raw MCP Tool objects for inspection. Use this.mcp.getAITools() when passing tools to the AI SDK.Managing Servers
Removing a Server
Persistence
MCP servers persist across agent restarts:- Server configuration stored in SQLite
- OAuth tokens stored securely
- Connections restored automatically when agent wakes
Listing All Servers
Client-Side Integration
Connected clients receive real-time MCP updates via WebSocket:Advanced: MCPClientManager
For fine-grained control, usethis.mcp directly:
Step-by-Step Connection
Event Subscription
Waiting for Connections
After hibernation or when connections are being restored in the background, MCP tools may not be immediately available. UsewaitForConnections() to wait until all in-flight connection and discovery operations have settled:
this.mcp.getAITools() immediately after the agent wakes from hibernation. Without waiting, tools from servers that are still reconnecting will be missing.
AIChatAgent handles this automatically via the waitForMcpConnections property (defaults to { timeout: 10_000 }). You only need waitForConnections() directly when using Agent with MCP, or when you want finer control inside onChatMessage.Error Recovery
Examples
MCP Client Demo
Theexamples/mcp-client example demonstrates:
- Adding and removing MCP servers dynamically
- Custom OAuth callback handling (popup-closing behavior)
- Listing tools from connected servers
- Real-time state updates to the frontend
API Reference
addMcpServer()
callbackHost is not required — you can call addMcpServer("name", url) with no options. For RPC transport, pass a DurableObjectNamespace binding instead of a URL. See MCP Transports for details.
Calling
addMcpServer is idempotent when both the server name and URL match an existing active connection — the existing connection is returned without creating a duplicate. This makes it safe to call in onStart() without worrying about duplicate connections on restart.If you call addMcpServer with the same name but a different URL, a new connection is created. Both connections remain active and their tools are merged in getAITools(). To replace a server, call removeMcpServer(oldId) first.URLs are normalized before comparison (trailing slashes, default ports, and hostname case are handled), so https://MCP.Example.com and https://mcp.example.com/ are treated as the same URL.removeMcpServer()
getMcpServers()
MCPServersState
MCPServer
Next Steps
Creating Servers
Build your own MCP server with the Agents SDK
Transports
Learn about different MCP transport options
Securing Servers
Implement OAuth and security best practices
Examples
View the MCP client example on GitHub