Skip to main content
Connect your agent to external MCP (Model Context Protocol) servers to use their tools, resources, and prompts. This enables your agent to interact with GitHub, Slack, databases, and other services through a standardized protocol.

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

Use addMcpServer() to connect to an MCP server:

Basic Usage

Transport Options

MCP supports multiple transport types:

Custom Headers

For servers behind authentication (like Cloudflare Access) or using bearer tokens:

Retry Options

Configure retry behavior for connection and reconnection attempts:
These options are persisted and used when reconnecting after hibernation or after OAuth completion. Default: 3 attempts, 500ms base delay, 5s max delay.

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)
If you need to connect to an internal MCP server, use the RPC transport with a Durable Object binding instead of HTTP.

Return Value

addMcpServer() returns the connection state:
  • ready - Server connected and tools discovered
  • authenticating - Server requires OAuth; redirect user to authUrl

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:
For example: 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 the createMcpOAuthProvider method in your agent subclass:
Your custom class must implement the 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). Import DurableObjectOAuthClientProvider 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, use this.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

This disconnects from the server and removes it from storage.

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, use this.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. Use waitForConnections() to wait until all in-flight connection and discovery operations have settled:
This is useful when you need to call 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

The examples/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()

Add and connect to an MCP server. Throws if connection or discovery fails. For non-OAuth servers, 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()

Disconnect from and remove an MCP server.

getMcpServers()

Get the current state of all MCP servers and their capabilities.

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