Programmatic client for interacting with MCP servers through a well-typed, Pythonic interface.
New in version: 2.0.0
The central piece of MCP client applications is the fastmcp.Client
class. This class provides a programmatic interface for interacting with any Model Context Protocol (MCP) server, handling protocol details and connection management automatically.
The FastMCP Client is designed for deterministic, controlled interactions rather than autonomous behavior, making it ideal for:
async with
context manager for proper connection lifecycle management.
Client
: Handles MCP protocol operations (tools, resources, prompts) and manages callbacksTransport
: Establishes and maintains the connection (WebSockets, HTTP, Stdio, in-memory)FastMCP
instance → In-memory transport (perfect for testing).py
→ Python Stdio transport.js
→ Node.js Stdio transporthttp://
or https://
→ HTTP transportMCPConfig
dictionary → Multi-server clientFastMCP
server directly to the client. This eliminates network complexity and separate processes.New in version: 2.4.0
Create clients from MCP configuration dictionaries, which can include multiple servers. While there is no official standard for MCP configuration format, FastMCP follows established conventions used by tools like Claude Desktop.
ping()
to verify the server is reachable:
Client
constructor accepts several configuration options:
transport
: Transport instance or source for automatic inferencelog_handler
: Handle server log messagesprogress_handler
: Monitor long-running operationssampling_handler
: Respond to server LLM requestsroots
: Provide local context to serverstimeout
: Default timeout for requests (in seconds)