Configure how clients connect to and communicate with MCP servers.
New in version 2.0.0Transports handle the underlying connection between your client and MCP servers. While the client can automatically select a transport based on what you pass to it, instantiating transports explicitly gives you full control over configuration.
STDIO transport communicates with MCP servers through subprocess pipes. When using STDIO, your client launches and manages the server process, controlling its lifecycle and environment.
STDIO servers run in isolated environments by default. They do not inherit your shell’s environment variables. You must explicitly pass any configuration the server needs.
Since STDIO servers do not inherit your environment, you need strategies for passing configuration.Selective forwarding passes only the variables your server needs:
Copy
import osfrom fastmcp.client.transports import StdioTransportrequired_vars = ["API_KEY", "DATABASE_URL", "REDIS_HOST"]env = {var: os.environ[var] for var in required_vars if var in os.environ}transport = StdioTransport(command="python", args=["server.py"], env=env)client = Client(transport)
Loading from .env files keeps configuration separate from code:
STDIO transports maintain sessions across multiple client contexts by default (keep_alive=True). This reuses the same subprocess for multiple connections, improving performance.
Copy
from fastmcp.client.transports import StdioTransporttransport = StdioTransport(command="python", args=["server.py"])client = Client(transport)async def efficient_multiple_operations(): async with client: await client.ping() async with client: # Reuses the same subprocess await client.call_tool("process_data", {"file": "data.csv"})
For complete isolation between connections, disable session persistence:
Copy
transport = StdioTransport(command="python", args=["server.py"], keep_alive=False)
Server-Sent Events transport is maintained for backward compatibility. Use Streamable HTTP for new deployments unless you have specific infrastructure requirements.
In-memory transport connects directly to a FastMCP server instance within the same Python process. This eliminates both subprocess management and network overhead, making it ideal for testing.
Copy
from fastmcp import FastMCP, Clientimport osmcp = FastMCP("TestServer")@mcp.tooldef greet(name: str) -> str: prefix = os.environ.get("GREETING_PREFIX", "Hello") return f"{prefix}, {name}!"client = Client(mcp)async with client: result = await client.call_tool("greet", {"name": "World"})
Unlike STDIO transports, in-memory servers share the same memory space and environment variables as your client code.