The run()
Method
Every FastMCP server needs to be started to accept connections. The simplest way to run a server is by calling the run()
method on your FastMCP instance. This method starts the server and blocks until it’s stopped, handling all the connection management for you.
For maximum compatibility, it’s best practice to place the
run()
call within an if __name__ == "__main__":
block. This ensures the server starts only when the script is executed directly, not when imported as a module.my_server.py
python my_server.py
.
Transport Protocols
MCP servers communicate with clients through different transport protocols. Think of transports as the “language” your server speaks to communicate with clients. FastMCP supports three main transport protocols, each designed for specific use cases and deployment scenarios. The choice of transport determines how clients connect to your server, what network capabilities are available, and how many clients can connect simultaneously. Understanding these transports helps you choose the right approach for your application.STDIO Transport (Default)
STDIO (Standard Input/Output) is the default transport for FastMCP servers. When you callrun()
without arguments, your server uses STDIO transport. This transport communicates through standard input and output streams, making it perfect for command-line tools and desktop applications like Claude Desktop.
With STDIO transport, the client spawns a new server process for each session and manages its lifecycle. The server reads MCP messages from stdin and writes responses to stdout. This is why STDIO servers don’t stay running - they’re started on-demand by the client.
- Local development and testing
- Claude Desktop integration
- Command-line tools
- Single-user applications
HTTP Transport (Streamable)
HTTP transport turns your MCP server into a web service accessible via a URL. This transport uses the Streamable HTTP protocol, which allows clients to connect over the network. Unlike STDIO where each client gets its own process, an HTTP server can handle multiple clients simultaneously. The Streamable HTTP protocol provides full bidirectional communication between client and server, supporting all MCP operations including streaming responses. This makes it the recommended choice for network-based deployments. To use HTTP transport, specify it in therun()
method along with networking options:
http://localhost:8000/mcp/
. This URL is the MCP endpoint that clients will connect to. HTTP transport enables:
- Network accessibility
- Multiple concurrent clients
- Integration with web infrastructure
- Remote deployment capabilities
SSE Transport (Legacy)
Server-Sent Events (SSE) transport was the original HTTP-based transport for MCP. While still supported for backward compatibility, it has limitations compared to the newer Streamable HTTP transport. SSE only supports server-to-client streaming, making it less efficient for bidirectional communication.Choosing the Right Transport
Each transport serves different needs. STDIO is perfect when you need simple, local execution - it’s what Claude Desktop and most command-line tools expect. HTTP transport is essential when you need network access, want to serve multiple clients, or plan to deploy your server remotely. SSE exists only for backward compatibility and shouldn’t be used in new projects. Consider your deployment scenario: Are you building a tool for local use? STDIO is your best choice. Need a centralized service that multiple clients can access? HTTP transport is the way to go.The FastMCP CLI
FastMCP provides a powerful command-line interface for running servers without modifying the source code. The CLI can automatically find and run your server with different transports, manage dependencies, and handle development workflows:mcp
, server
, or app
) and runs it with the specified options. This is particularly useful for testing different transports or configurations without changing your code.
Dependency Management
The CLI integrates withuv
to manage Python environments and dependencies:
When using
--python
, --with
, --project
, or --with-requirements
, the server runs via uv run
subprocess instead of using your local environment.Passing Arguments to Servers
When servers accept command line arguments (using argparse, click, or other libraries), you can pass them after--
:
Async Usage
FastMCP servers are built on async Python, but the framework provides both synchronous and asynchronous APIs to fit your application’s needs. Therun()
method we’ve been using is actually a synchronous wrapper around the async server implementation.
For applications that are already running in an async context, FastMCP provides the run_async()
method:
The
run()
method cannot be called from inside an async function because it creates its own async event loop internally. If you attempt to call run()
from inside an async function, you’ll get an error about the event loop already running.Always use run_async()
inside async functions and run()
in synchronous contexts.run()
and run_async()
accept the same transport arguments, so all the examples above apply to both methods.
Custom Routes
When using HTTP transport, you might want to add custom web endpoints alongside your MCP server. This is useful for health checks, status pages, or simple APIs. FastMCP lets you add custom routes using the@custom_route
decorator:
/mcp/
. For more complex web applications, consider mounting your MCP server into a FastAPI or Starlette app.
Alternative Initialization Patterns
Theif __name__ == "__main__"
pattern works well for standalone scripts, but some deployment scenarios require different approaches. FastMCP handles these cases automatically.
CLI-Only Servers
When using the FastMCP CLI, you don’t need theif __name__
block at all. The CLI will find your FastMCP instance and run it: