Client class connects to any MCP server and exposes its tools, resources, and prompts:
Client.connect() accepts a URL, a stdio transport, an in-process FastMCP instance, or an mcpServers config — see Transports for the full set.
Lifecycle
Connections are ref-counted: multiple callers can share one client. Eachconnect() increments the count, each close() decrements it; the underlying connection is established on the first connect and torn down when the count reaches zero. isConnected() reports whether the underlying connection is live.
Symbol.asyncDispose, so await using closes it automatically on scope exit:
Error handling
callTool() throws a ToolCallError when the server returns isError: true. The error carries the server’s content blocks on its content field:
callToolRaw()never throws — it returns the full result includingisError, for when you want to inspect the raw response.toResult()is a standalone utility that wraps any promise into a discriminated union{ ok: true, value } | { ok: false, error }for error-as-value style:
Timeouts
ClientOptions.defaultOptions accepts per-scope timeout defaults — tool.timeout, resource.timeout, prompt.timeout, and a global timeout fallback. Timeouts are in seconds in the public API. A per-request timeout in the call’s options takes precedence:
Narrow interfaces
Client implements three segregated interfaces — IToolsClient, IResourcesClient, and IPromptsClient — so functions can declare only the capability they need:
Going further
Transports
URLs, stdio subprocesses, and in-process servers.
Authentication
Bearer tokens, OAuth, and client credentials.
Sampling
Forward server sampling requests to Anthropic, OpenAI, or Google.
Multi-server
One client, many servers, automatic namespacing.