Skip to main content
The MCPClientManager class manages connections to one or more MCP servers and provides methods to interact with their tools, resources, and prompts.

Import

Constructor

Parameters

Record<string, StdioServerConfig | HttpServerConfig>
required
A map of server IDs to their configurations.

Server Configuration Types

StdioServerConfig

For subprocess-based MCP servers.

HttpServerConfig

For remote MCP servers via SSE or Streamable HTTP.
Refresh Token Example
When refreshToken is provided, the SDK will exchange it for an access token during connection and automatically refresh tokens if the server challenges the current token.
refreshToken is mutually exclusive with accessToken, authProvider, and requestInit.headers.Authorization.
Client Identity Example
Use clientInfo to pin the identity sent in MCP initialize. This is useful when testing how a server behaves for different clients (e.g., Claude, ChatGPT):
The first entry in supportedProtocolVersions is proposed to the server; the full list is the accept-set. A server that negotiates any listed version is accepted.
onUnauthorized Example
Use onUnauthorized when you manage access tokens externally (e.g., from a secrets vault) and want the SDK to automatically recover from a 401 without a full reconnect flow:
When the server returns HTTP 401, the SDK calls onUnauthorized once, updates the access token, rebuilds the transport, and retries the operation. A second 401 after the retry surfaces the error to the caller.
onUnauthorized is only active for accessToken-based HTTP configs. It is ignored when refreshToken or authProvider is set, and it is never triggered by 403 responses.

Example


Methods

connectToServer()

Establishes a connection to a configured server.

Parameters

Returns

Promise<Client> - Resolves with the connected MCP client, rejects on failure.

Example

Throws

  • Error if serverId is not in the configuration
  • Error if connection fails (network, subprocess spawn, etc.)

disconnectServer()

Closes the connection to a server and cleans up resources.

Parameters

Returns

Promise<void>

Example


pingServer()

Sends a ping to check if a server is responsive.

Parameters

Returns

void - This method is synchronous and does not return a value.

Throws

  • Error if the server is not connected
  • Error if the ping fails

Example


listTools()

Returns all tools available on a server.

Parameters

Returns

Promise<{ tools: Tool[] }> - Tool list response from the server.

Tool Object

Example


executeTool()

Executes a tool and returns the result.

Parameters

Returns

Promise<unknown> - The tool’s return value (type depends on the tool).

Example

Throws

  • Error if tool doesn’t exist
  • Error if arguments are invalid
  • Error if tool execution fails

listResources()

Returns all resources available on a server.

Parameters

Returns

Promise<Resource[]> - Array of resource definitions.

Resource Object

Example


readResource()

Reads the content of a resource.

Parameters

Returns

Promise<ResourceContent> - The resource content.

Example


listPrompts()

Returns all prompts available on a server.

Parameters

Returns

Promise<Prompt[]> - Array of prompt definitions.

Prompt Object

Example


getPrompt()

Gets a prompt with optional arguments.

Parameters

Returns

Promise<PromptResult> - The prompt content with messages.

Example


getTools()

Returns all tools from specified servers or all connected servers.

Parameters

Returns

Promise<Tool[]> - Array of tool definitions from all specified servers.

Tool Object

Each tool in the returned array includes:

Example

getTools() returns all tools including app-only ones. Tools with _meta.ui.visibility = ["app"] are filtered out by getToolsForAiSdk() per SEP-1865 before being passed to the model. These tools remain callable by apps via the MCP bridge.

getToolsForAiSdk()

Returns tools in Vercel AI SDK format, ready to pass directly to generateText() or streamText(). App-only tools are filtered out by default per SEP-1865.

Parameters

Returns

Promise<ToolSet> - Tools in Vercel AI SDK format with execution wired up.

Example


Complete Example