fastmcp.client.transports
Functions
infer_transport
- ClientTransport: Used directly without modification
- FastMCP or FastMCP1Server: Creates an in-memory FastMCPTransport
- Path or str (file path): Creates PythonStdioTransport (.py) or NodeStdioTransport (.js)
- AnyUrl or str (URL): Creates StreamableHttpTransport (default) or SSETransport (for /sse endpoints)
- MCPConfig or dict: Creates MCPConfigTransport, potentially connecting to multiple servers
/sse
.
For MCPConfig with multiple servers, a composite client is created where each server
is mounted with its name as prefix. This allows accessing tools and resources from multiple
servers through a single unified client interface, using naming patterns like
servername_toolname
for tools and protocol://servername/path
for resources.
If the MCPConfig contains only one server, a direct connection is established without prefixing.
Examples:
Classes
SessionKwargs
Keyword arguments for the MCP ClientSession constructor.
ClientTransport
Abstract base class for different MCP client transport mechanisms.
A Transport is responsible for establishing and managing connections
to an MCP server, and providing a ClientSession within an async context.
Methods:
connect_session
**session_kwargs
: Keyword arguments to pass to the ClientSession constructor (e.g., callbacks, timeouts).
close
WSTransport
Transport implementation that connects to an MCP server via WebSockets.
Methods:
connect_session
SSETransport
Transport implementation that connects to an MCP server via Server-Sent Events.
Methods:
connect_session
StreamableHttpTransport
Transport implementation that connects to an MCP server via Streamable HTTP Requests.
Methods:
connect_session
StdioTransport
Base transport for connecting to an MCP server via subprocess with stdio.
This is a base class that can be subclassed for specific command-based
transports like Python, Node, Uvx, etc.
Methods:
connect_session
connect
disconnect
close
PythonStdioTransport
Transport for running Python scripts.
FastMCPStdioTransport
Transport for running FastMCP servers using the FastMCP CLI.
NodeStdioTransport
Transport for running Node.js scripts.
UvStdioTransport
Transport for running commands via the uv tool.
UvxStdioTransport
Transport for running commands via the uvx tool.
NpxStdioTransport
Transport for running commands via the npx tool.
FastMCPTransport
In-memory transport for FastMCP servers.
This transport connects directly to a FastMCP server instance in the same
Python process. It works with both FastMCP 2.x servers and FastMCP 1.0
servers from the low-level MCP SDK. This is particularly useful for unit
tests or scenarios where client and server run in the same runtime.
Methods:
connect_session
MCPConfigTransport
Transport for connecting to one or more MCP servers defined in an MCPConfig.
This transport provides a unified interface to multiple MCP servers defined in an MCPConfig
object or dictionary matching the MCPConfig schema. It supports two key scenarios:
- If the MCPConfig contains exactly one server, it creates a direct transport to that server.
- If the MCPConfig contains multiple servers, it creates a composite client by mounting all servers on a single FastMCP instance, with each server’s name, by default, used as its mounting prefix.
{server_name}_{tool_name}
and resources with the pattern protocol://{server_name}/path/to/resource
.
This is particularly useful for creating clients that need to interact with multiple specialized
MCP servers through a single interface, simplifying client code.
Examples: