Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395---title: clientsidebarTitle: client---
# `fastmcp.client.client`
## Classes
### `ClientSessionState` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L219" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Holds all session-related state for a Client instance.
This allows clean separation of configuration (which is copied) fromsession state (which should be fresh for each new client instance).
### `CallToolResult` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L252" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Parsed result from a tool call.
### `Client` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L262" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
MCP client that delegates connection management to a Transport instance.
The Client class is responsible for MCP protocol logic, while the Transporthandles connection establishment and management. Client provides methods forworking 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 sessionmanagement. This allows efficient session reuse in any scenario withnested 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 whileother tasks were waiting on them, causing deadlocks. The issue was exposedin 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 coordinatebetween 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 getreplaced 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) probes`server/discover` and negotiates the modern era, denylist-falling-back to theinitialize handshake for any server that is not positive evidence of a modernpeer — safe against a mixed fleet of legacy and modern servers. `"legacy"`forces the initialize handshake, byte-identical to pre-v4 behavior; opt into itto pin the old handshake. A modern version string (e.g. `"2026-07-28"`) adoptsthat version directly without a probe.- `prior_discover`: A previously obtained `DiscoverResult` to adopt when `mode` is aversion pin, reused instead of synthesizing a minimal one. Ignored otherwise.- `input_required_max_rounds`: Cap on `InputRequiredResult` (SEP-2322) retry roundsfor `call_tool` / `get_prompt` / `read_resource` before the driver gives up.Only reachable on 2026-era servers that emit `InputRequiredResult`.- `cache`: Client-side response caching (SEP-2549), opt-in. `None` (default) and`False` disable it; `True` enables the default in-memory store honoringserver `ttlMs`/`cacheScope` hints; a `CacheConfig` customizes it. Honoring ismodern-only, so a cache is inert on legacy connections. A custom `CacheConfig`store requires `target_id`, since FastMCP transports expose no server URL toderive a shared-store identity from.- `extensions`: Opt-in client extensions (SEP-2133), a sequence of`mcp.client.extension.ClientExtension` instances. Each contributes itscapability advertisement, its result claims, and its notification bindings,all of which are threaded into the underlying session. User-suppliednotification bindings compose with FastMCP's internal task-status bindingrather than replacing it. A claimed `call_tool` result is resolvedtransparently through the owning extension's resolver. For an advertise-onlyentry, use `mcp.client.advertise(identifier, settings)`.- `result_claims`: Additional `ResultClaim`s (SEP-2133) keyed by the identifier ofan extension already advertised through `extensions`, merged with thatextension's own claims. Rarely needed directly; prefer declaring claims onthe extension itself. Claimed shapes are modern-only and inert on a legacyconnection.
**Examples:**
```python# Connect to FastMCP serverclient = Client("http://localhost:8080")
async with client: # List available resources resources = await client.list_resources()
# Call a tool result = await client.call_tool("my_tool", {"param": "value"})```
**Methods:**
#### `session` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L653" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonsession(self) -> ClientSession```
Get the current active session. Raises RuntimeError if not connected.
#### `prior_discover` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L663" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonprior_discover(self) -> mcp_types.DiscoverResult | None```
The configured result to adopt when `mode` pins a modern version.
#### `initialize_result` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L668" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythoninitialize_result(self) -> mcp_types.InitializeResult | None```
Get the result of the initialization request.
`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-neutralaccess to the negotiated server metadata.
#### `protocol_version` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L679" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonprotocol_version(self) -> str | None```
The negotiated protocol version, or `None` when disconnected.
Set during connect-time negotiation regardless of era: the initializehandshake, `server/discover`, or a direct version pin all populate it.
#### `server_capabilities` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L689" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonserver_capabilities(self) -> mcp_types.ServerCapabilities | None```
The server's advertised capabilities, or `None` when disconnected.
Populated from whichever negotiation result the era produced (the`InitializeResult` on legacy, the `DiscoverResult` on modern).
#### `server_info` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L699" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonserver_info(self) -> mcp_types.Implementation | None```
The session's server identity, or `None` when disconnected.
Populated from whichever negotiation result the era produced (the`InitializeResult` on legacy, the `DiscoverResult` on modern). A directlypinned modern version uses a synthesized identity with an empty name.
#### `instructions` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L710" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythoninstructions(self) -> str | None```
The server's instructions, or `None` when absent or disconnected.
Populated from whichever negotiation result the era produced (the`InitializeResult` on legacy, the `DiscoverResult` on modern).
#### `set_roots` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L719" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonset_roots(self, roots: RootsList | RootsHandler) -> None```
Set the roots for the client. This does not automatically call `send_roots_list_changed`.
#### `set_sampling_callback` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L723" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonset_sampling_callback(self, sampling_callback: SamplingHandler, sampling_capabilities: mcp_types.SamplingCapability | None = None) -> None```
Set the sampling callback for the client.
#### `set_elicitation_callback` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L738" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonset_elicitation_callback(self, elicitation_callback: ElicitationHandler) -> None```
Set the elicitation callback for the client.
#### `is_connected` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L749" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonis_connected(self) -> bool```
Check if the client is currently connected.
#### `new` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L753" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonnew(self) -> Client[ClientTransportT]```
Create a new client instance with the same configuration but fresh session state.
This creates a new client with the same transport, handlers, and configuration,but with no active session. Useful for creating independent sessions that don'tshare state with the original client.
**Returns:**- A new Client instance with the same configuration but disconnected state.
#### `initialize` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L893" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythoninitialize(self, timeout: datetime.timedelta | float | int | None = None) -> mcp_types.InitializeResult```
Send an initialize request to the server.
This method performs the MCP initialization handshake with the server,exchanging capabilities and server information. It is idempotent - callingit multiple times returns the cached result from the first call.
The initialization happens automatically when entering the client contextmanager unless `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 adoptthe modern `server/discover` era, which has no `InitializeResult`; in that casethis 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.
**Returns:**- The server's initialization response containing server info,capabilities, protocol version, and optional instructions.
**Raises:**- `RuntimeError`: If the client is not connected, initialization times out, or thenegotiated era carries no `InitializeResult` (a modern `discover` connection).
#### `close` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L1393" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonclose(self)```
#### `ping` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L1399" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonping(self) -> bool```
Send a ping request.
#### `cancel` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L1404" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythoncancel(self, request_id: str | int, reason: str | None = None) -> None```
Send a cancellation notification for an in-progress request.
#### `progress` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L1419" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonprogress(self, progress_token: str | int, progress: float, total: float | None = None, message: str | None = None) -> None```
Send a progress notification.
#### `set_logging_level` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L1433" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonset_logging_level(self, level: mcp_types.LoggingLevel) -> None```
Send a logging/setLevel request.
Handshake-era servers only. `logging/setLevel` asks the server toremember a level for the rest of the session, and the 2026-07-28protocol has no session to remember it in — the method is absent fromthat era's registry. Log *notifications* are unaffected: they ride therequest'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` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L1456" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonsend_roots_list_changed(self) -> None```
Send a roots/list_changed notification.
#### `complete_mcp` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L1464" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythoncomplete_mcp(self, ref: mcp_types.ResourceTemplateReference | mcp_types.PromptReference, argument: dict[str, str], context_arguments: dict[str, Any] | None = None) -> mcp_types.CompleteResult```
Send a completion request and return the complete MCP protocol result.
**Args:**- `ref`: The reference to complete.- `argument`: Arguments to pass to the completion request.- `context_arguments`: Optional context arguments toinclude with the completion request. Defaults to None.
**Returns:**- mcp_types.CompleteResult: The complete response object from the protocol,containing the completion and any additional metadata.
**Raises:**- `RuntimeError`: If called while the client is not connected.- `MCPError`: If the request results in a TimeoutError | JSONRPCError
#### `complete` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L1495" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythoncomplete(self, ref: mcp_types.ResourceTemplateReference | mcp_types.PromptReference, argument: dict[str, str], context_arguments: dict[str, Any] | None = None) -> mcp_types.Completion```
Send a completion request to the server.
**Args:**- `ref`: The reference to complete.- `argument`: Arguments to pass to the completion request.- `context_arguments`: Optional context arguments toinclude with the completion request. Defaults to None.
**Returns:**- mcp_types.Completion: The completion object.
**Raises:**- `RuntimeError`: If called while the client is not connected.- `MCPError`: If the request results in a TimeoutError | JSONRPCError
#### `generate_name` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/client.py#L1522" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythongenerate_name(cls, name: str | None = None) -> str```