Skip to main content

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
Returns: Promise<string> - Server ID

connectToServer()

Connect to a registered MCP server and initialize the connection.
string
required
Server ID (from registerServer)
Returns: Promise<MCPConnectionResult>

discoverIfConnected()

Discover server capabilities if connection is in CONNECTED or READY state.
string
required
Server ID to discover
DiscoverOptions
number
default:"30000"
Timeout in milliseconds
Returns: Promise<MCPDiscoverResult | undefined>

removeServer()

Remove an MCP server - closes connection if active and removes from storage.
string
required
Server ID to remove
Returns: Promise<void>

Listing Resources

listTools()

Get all available tools from connected MCP servers.
Returns: (Tool & { serverId: string })[]

listPrompts()

Get all available prompts from connected MCP servers.
Returns: (Prompt & { serverId: string })[]

listResources()

Get all available resources from connected MCP servers.
Returns: (Resource & { serverId: string })[]

listResourceTemplates()

Get all available resource templates from connected MCP servers.
Returns: (ResourceTemplate & { serverId: string })[]

listServers()

List all registered MCP servers from storage.
Returns: MCPServerRow[]

Calling Tools

callTool()

Call a tool on an MCP server.
CallToolParams
required
string
required
Server ID
string
required
Tool name
Record<string, unknown>
required
Tool arguments
Returns: Promise<CallToolResult>

getPrompt()

Get a prompt from an MCP server.
GetPromptParams
required
string
required
Server ID
string
required
Prompt name
Record<string, unknown>
Prompt arguments
Returns: Promise<GetPromptResult>

readResource()

Read a resource from an MCP server.
ReadResourceParams
required
string
required
Server ID
string
required
Resource URI
Returns: Promise<ReadResourceResult>

AI SDK Integration

getAITools()

Get all MCP tools as AI SDK tool definitions. Use with generateText() or streamText().
Returns: 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.
Returns: Promise<void>

closeConnection()

Close a connection to an MCP server (but keep it in storage).
string
required
Server ID
Returns: Promise<void>

closeAllConnections()

Close all active connections to MCP servers (but keep them in storage).
Returns: Promise<void>

OAuth Flow

configureOAuthCallback()

Configure OAuth callback handling for MCP servers.
MCPClientOAuthCallbackConfig
required
string
URL to redirect to on successful OAuth
string
URL to redirect to on failed OAuth
(result: MCPClientOAuthResult) => Response
Custom handler for OAuth callback

isCallbackRequest()

Check if a request is an OAuth callback request.
Request
required
The request to check
Returns: boolean

handleCallbackRequest()

Handle an OAuth callback request.
Request
required
The OAuth callback request
Returns: Promise<MCPClientOAuthResult>

establishConnection()

Establish connection in the background after OAuth completion.
string
required
Server ID
Returns: Promise<void>

RPC Servers (Durable Objects)

addRpcMcpServer()

Connect to an MCP server running as a Durable Object.
See Agent class reference for full signature.

Full Example

Events

onServerStateChanged

Subscribe to server state changes (registered, connected, removed, etc.).

onObservabilityEvent

Subscribe to observability events from MCP connections.