Overview
MCPClientManager allows Agents to connect to external MCP servers and access their tools, prompts, and resources. It’s automatically available via this.mcp in all Agents.
Registration & Connection
registerServer()
Register an MCP server without connecting. Creates the connection object, sets up observability, and saves to storage.string
required
Unique identifier for the server
RegisterServerOptions
required
string
required
Server URL (http/https for remote, rpc:// for Durable Object)
string
required
Human-readable server name
string
OAuth callback URL (auto-derived from request if omitted)
string
OAuth authorization URL
string
OAuth client ID
ConstructorParameters<typeof Client>[1]
MCP client options
MCPTransportOptions
Transport configuration (headers, type)
RetryOptions
Retry options for connection attempts
Promise<string> - Server ID
connectToServer()
Connect to a registered MCP server and initialize the connection.string
required
Server ID (from registerServer)
Promise<MCPConnectionResult>
discoverIfConnected()
Discover server capabilities if connection is in CONNECTED or READY state.string
required
Server ID to discover
Promise<MCPDiscoverResult | undefined>
removeServer()
Remove an MCP server - closes connection if active and removes from storage.string
required
Server ID to remove
Promise<void>
Listing Resources
listTools()
Get all available tools from connected MCP servers.(Tool & { serverId: string })[]
listPrompts()
Get all available prompts from connected MCP servers.(Prompt & { serverId: string })[]
listResources()
Get all available resources from connected MCP servers.(Resource & { serverId: string })[]
listResourceTemplates()
Get all available resource templates from connected MCP servers.(ResourceTemplate & { serverId: string })[]
listServers()
List all registered MCP servers from storage.MCPServerRow[]
Calling Tools
callTool()
Call a tool on an MCP server.CallToolParams
required
Promise<CallToolResult>
getPrompt()
Get a prompt from an MCP server.GetPromptParams
required
Promise<GetPromptResult>
readResource()
Read a resource from an MCP server.Promise<ReadResourceResult>
AI SDK Integration
getAITools()
Get all MCP tools as AI SDK tool definitions. Use withgenerateText() or streamText().
ToolSet
Call
await this.mcp.ensureJsonSchema() before using getAITools() if you’re not using await this.mcp.waitForConnections().Connection Management
waitForConnections()
Wait for all in-flight connection and discovery operations to settle.WaitOptions
number
Maximum time to wait in milliseconds.
0 returns immediately, undefined waits indefinitely.Promise<void>
closeConnection()
Close a connection to an MCP server (but keep it in storage).string
required
Server ID
Promise<void>
closeAllConnections()
Close all active connections to MCP servers (but keep them in storage).Promise<void>
OAuth Flow
configureOAuthCallback()
Configure OAuth callback handling for MCP servers.MCPClientOAuthCallbackConfig
required
isCallbackRequest()
Check if a request is an OAuth callback request.Request
required
The request to check
boolean
handleCallbackRequest()
Handle an OAuth callback request.Request
required
The OAuth callback request
Promise<MCPClientOAuthResult>
establishConnection()
Establish connection in the background after OAuth completion.string
required
Server ID
Promise<void>
RPC Servers (Durable Objects)
addRpcMcpServer()
Connect to an MCP server running as a Durable Object.Full Example
Events
onServerStateChanged
Subscribe to server state changes (registered, connected, removed, etc.).onObservabilityEvent
Subscribe to observability events from MCP connections.Related
- McpAgent - Build MCP servers
- Agent Class - Agent base class