fastmcp.client.client
ClientSessionState
Client
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:
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.
session
initialize_result
set_roots
send_roots_list_changed
.
set_sampling_callback
set_elicitation_callback
is_connected
new
close
ping
cancel
progress
set_logging_level
send_roots_list_changed
list_resources_mcp
RuntimeError
: If called while the client is not connected.list_resources
RuntimeError
: If called while the client is not connected.list_resource_templates_mcp
RuntimeError
: If called while the client is not connected.list_resource_templates
RuntimeError
: If called while the client is not connected.read_resource_mcp
uri
: The URI of the resource to read. Can be a string or an AnyUrl object.RuntimeError
: If called while the client is not connected.read_resource
uri
: The URI of the resource to read. Can be a string or an AnyUrl object.RuntimeError
: If called while the client is not connected.list_prompts_mcp
RuntimeError
: If called while the client is not connected.list_prompts
RuntimeError
: If called while the client is not connected.get_prompt_mcp
name
: The name of the prompt to retrieve.arguments
: Arguments to pass to the prompt. Defaults to None.RuntimeError
: If called while the client is not connected.get_prompt
name
: The name of the prompt to retrieve.arguments
: Arguments to pass to the prompt. Defaults to None.RuntimeError
: If called while the client is not connected.complete_mcp
ref
: The reference to complete.argument
: Arguments to pass to the completion request.RuntimeError
: If called while the client is not connected.complete
ref
: The reference to complete.argument
: Arguments to pass to the completion request.RuntimeError
: If called while the client is not connected.list_tools_mcp
RuntimeError
: If called while the client is not connected.list_tools
RuntimeError
: If called while the client is not connected.call_tool_mcp
name
: The name of the tool to call.arguments
: Arguments to pass to the tool.timeout
: The timeout for the tool call. Defaults to None.progress_handler
: The progress handler to use for the tool call. Defaults to None.RuntimeError
: If called while the client is not connected.call_tool
name
: The name of the tool to call.arguments
: Arguments to pass to the tool. Defaults to None.timeout
: The timeout for the tool call. Defaults to None.progress_handler
: The progress handler to use for the tool call. Defaults to None.ToolError
: If the tool call results in an error.RuntimeError
: If called while the client is not connected.CallToolResult