fastmcp.client.client
Classes
ClientSessionState
Holds all session-related state for a Client instance.
This allows clean separation of configuration (which is copied) from
session state (which should be fresh for each new client instance).
CallToolResult
Parsed result from a tool call.
Client
MCP client that delegates connection management to a Transport instance.
The Client class is responsible for MCP protocol logic, while the Transport
handles connection establishment and management. Client provides methods for
working with resources, prompts, tools and other MCP capabilities.
This client supports reentrant context managers (multiple concurrent
async with client: blocks) using reference counting and background session
management. This allows efficient session reuse in any scenario with
nested or concurrent client usage.
MCP SDK 1.10 introduced automatic list_tools() calls during call_tool()
execution. This created a race condition where events could be reset while
other tasks were waiting on them, causing deadlocks. The issue was exposed
in proxy scenarios but affects any reentrant usage.
The solution uses reference counting to track active context managers,
a background task to manage the session lifecycle, events to coordinate
between tasks, and ensures all session state changes happen within a lock.
Events are only created when needed, never reset outside locks.
This design prevents race conditions where tasks wait on events that get
replaced by other tasks, ensuring reliable coordination in concurrent scenarios.
Args:
-
transport: Connection source specification, which can be:- ClientTransport: Direct transport instance
- FastMCP: In-process FastMCP server
- AnyUrl or str: URL to connect to
- Path: File path for local socket
- MCPConfig: MCP server configuration
- dict: Transport configuration
-
roots: Optional RootsList or RootsHandler for filesystem access -
sampling_handler: Optional handler for sampling requests -
log_handler: Optional handler for log messages -
message_handler: Optional handler for protocol messages -
progress_handler: Optional handler for progress notifications -
timeout: Optional timeout for requests (seconds or timedelta) -
init_timeout: Optional timeout for initial connection (seconds or timedelta). Set to 0 to disable. If None, uses the value in the FastMCP global settings. -
mode: Protocol-era negotiation at connect time."auto"(the default) probesserver/discoverand negotiates the modern era, denylist-falling-back to the initialize handshake for any server that is not positive evidence of a modern peer — safe against a mixed fleet of legacy and modern servers."legacy"forces the initialize handshake, byte-identical to pre-v4 behavior; opt into it to pin the old handshake. A modern version string (e.g."2026-07-28") adopts that version directly without a probe. -
prior_discover: A previously obtainedDiscoverResultto adopt whenmodeis a version pin, reused instead of synthesizing a minimal one. Ignored otherwise. -
input_required_max_rounds: Cap onInputRequiredResult(SEP-2322) retry rounds forcall_tool/get_prompt/read_resourcebefore the driver gives up. Only reachable on 2026-era servers that emitInputRequiredResult. -
cache: Client-side response caching (SEP-2549), opt-in.None(default) andFalsedisable it;Trueenables the default in-memory store honoring serverttlMs/cacheScopehints; aCacheConfigcustomizes it. Honoring is modern-only, so a cache is inert on legacy connections. A customCacheConfigstore requirestarget_id, since FastMCP transports expose no server URL to derive a shared-store identity from. -
extensions: Opt-in client extensions (SEP-2133), a sequence ofmcp.client.extension.ClientExtensioninstances. Each contributes its capability advertisement, its result claims, and its notification bindings, all of which are threaded into the underlying session. User-supplied notification bindings compose with FastMCP’s internal task-status binding rather than replacing it. A claimedcall_toolresult is resolved transparently through the owning extension’s resolver. For an advertise-only entry, usemcp.client.advertise(identifier, settings). -
result_claims: AdditionalResultClaims (SEP-2133) keyed by the identifier of an extension already advertised throughextensions, merged with that extension’s own claims. Rarely needed directly; prefer declaring claims on the extension itself. Claimed shapes are modern-only and inert on a legacy connection.
session
prior_discover
mode pins a modern version.
initialize_result
None on a modern (server/discover) connection, which negotiates via a
DiscoverResult rather than an InitializeResult. Use protocol_version,
server_info, server_capabilities, and instructions for era-neutral
access to the negotiated server metadata.
protocol_version
None when disconnected.
Set during connect-time negotiation regardless of era: the initialize
handshake, server/discover, or a direct version pin all populate it.
server_capabilities
None when disconnected.
Populated from whichever negotiation result the era produced (the
InitializeResult on legacy, the DiscoverResult on modern).
server_info
None when disconnected.
Populated from whichever negotiation result the era produced (the
InitializeResult on legacy, the DiscoverResult on modern). A directly
pinned modern version uses a synthesized identity with an empty name.
instructions
None when absent or disconnected.
Populated from whichever negotiation result the era produced (the
InitializeResult on legacy, the DiscoverResult on modern).
set_roots
send_roots_list_changed.
set_sampling_callback
set_elicitation_callback
is_connected
new
- A new Client instance with the same configuration but disconnected state.
initialize
auto_initialize=False was set during client construction.
Manual calls to this method are only needed when auto-initialization is disabled.
With mode="auto" or a pinned modern version, connect-time negotiation may adopt
the modern server/discover era, which has no InitializeResult; in that case
this method raises. Read protocol_version, server_info,
server_capabilities, and instructions instead, or use mode="legacy"
when you need the handshake result.
Args:
timeout: Optional timeout for the initialization request (seconds or timedelta). If None, uses the client’s init_timeout setting.
- The server’s initialization response containing server info, capabilities, protocol version, and optional instructions.
RuntimeError: If the client is not connected, initialization times out, or the negotiated era carries noInitializeResult(a moderndiscoverconnection).
close
ping
cancel
progress
set_logging_level
logging/setLevel asks the server to
remember a level for the rest of the session, and the 2026-07-28
protocol has no session to remember it in — the method is absent from
that era’s registry. Log notifications are unaffected: they ride the
request’s own stream, so a server’s ctx.info() still reaches you.
Filter by level on the receiving side instead, in your log_handler.
send_roots_list_changed
complete_mcp
ref: The reference to complete.argument: Arguments to pass to the completion request.context_arguments: Optional context arguments to include with the completion request. Defaults to None.
- mcp_types.CompleteResult: The complete response object from the protocol, containing the completion and any additional metadata.
RuntimeError: If called while the client is not connected.MCPError: If the request results in a TimeoutError | JSONRPCError
complete
ref: The reference to complete.argument: Arguments to pass to the completion request.context_arguments: Optional context arguments to include with the completion request. Defaults to None.
- mcp_types.Completion: The completion object.
RuntimeError: If called while the client is not connected.MCPError: If the request results in a TimeoutError | JSONRPCError

