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
UseMCPClientManager 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.- 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.- 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:
accessTokenfor a static bearer tokenrefreshToken+clientIdfor non-interactive OAuth token exchange with automatic refreshauthProviderwhen you need to provide a custom MCP SDK OAuth provideronUnauthorizedfor custom 401 recovery when you manage tokens externally
- 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
UseonUnauthorized 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:
- Calls
onUnauthorizedonce to obtain a fresh access token. - Strips any stale
Authorizationheader fromrequestInit. - Rebuilds the transport with the new token and retries the operation.
- 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 ofMCPClientManager 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 aHostRunner 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

