Skip to main content
The MCPClientManager is your gateway to MCP servers. It handles connections, manages multiple servers, and provides a unified interface for calling tools, reading resources, and accessing prompts.

When to Use MCPClientManager

Use MCPClientManager when you need to:
  • Connect to one or more MCP servers
  • Execute tools programmatically (without an LLM)
  • Build applications that aggregate tools from multiple servers
  • Set up test environments for your MCP server

Two Types of Servers

MCP servers come in two transport types:

STDIO Servers (Local)

STDIO servers run as subprocesses on your machine. You provide a command and arguments, and the SDK spawns the process.
Best for:
  • Local development
  • Servers you’re building
  • CLI-based MCP servers
  • Servers requiring local resources

HTTP Servers (Remote)

HTTP servers connect via SSE (Server-Sent Events) or Streamable HTTP over the network.
Best for:
  • Production MCP servers
  • Third-party integrations (Asana, Slack, etc.)
  • Shared team servers
  • Serverless deployments

HTTP Authentication Options

For HTTP servers, MCPClientManager supports four auth patterns:
  • accessToken for a static bearer token
  • refreshToken + clientId for non-interactive OAuth token exchange with automatic refresh
  • authProvider when you need to provide a custom MCP SDK OAuth provider
  • onUnauthorized for custom 401 recovery when you manage tokens externally
If you already have a refresh token, you can let the SDK handle access token exchange and refresh for you:
With this configuration, the SDK:
  • exchanges the refresh token for an access token during connection
  • automatically retries with a fresh access token when the server returns an auth challenge
  • stores rotated refresh tokens returned by the authorization server
refreshToken is only supported for HTTP servers. When you use it, do not also set accessToken, authProvider, or an Authorization header in requestInit.headers.

Custom 401 recovery with onUnauthorized

Use onUnauthorized when you supply an accessToken but manage token rotation yourself — for example, when tokens come from a secrets vault or a backend service rather than a standard OAuth flow:
When the server returns HTTP 401, the SDK:
  1. Calls onUnauthorized once to obtain a fresh access token.
  2. Strips any stale Authorization header from requestInit.
  3. Rebuilds the transport with the new token and retries the operation.
  4. If the retry also returns 401, the error is surfaced to the caller.
onUnauthorized only fires on strict HTTP 401 responses — not 403. It is also skipped when refreshToken or authProvider is configured, since those patterns handle token refresh internally. Parallel 401s for the same server share a single refresh call.

Connecting and Disconnecting

After configuring servers, you must explicitly connect:
Always disconnect servers when you’re done, especially in tests. This cleans up subprocess handles and network connections.

Working with Multiple Servers

A key strength of MCPClientManager is managing multiple servers simultaneously:

Executing Tools Directly

You can call tools without involving an LLM—useful for unit tests:

Integration with HostRunner

The most common pattern is connecting servers, then creating a HostRunner with their tools:

Error Handling

Connection failures throw errors. Wrap in try/catch for production code:

Health Checks

Verify a server is responsive:

Next Steps

Testing with LLMs

Connect LLMs to your MCP tools

MCPClientManager Reference

Full API documentation