Learn how to run your FastMCP server locally for development and testing
run()
Methodrun()
method on your FastMCP instance. This method starts the server and blocks until it’s stopped, handling all the connection management for you.
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.python my_server.py
.
run()
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.
run()
method along with networking options:
http://localhost:8000/mcp/
. This URL is the MCP endpoint that clients will connect to. HTTP transport enables:
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.
uv
to manage Python environments and dependencies:
--python
, --with
, --project
, or --with-requirements
, the server runs via uv run
subprocess instead of using your local environment.--
:
run()
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:
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_route
decorator:
/mcp/
. For more complex web applications, consider mounting your MCP server into a FastAPI or Starlette app.
if __name__ == "__main__"
pattern works well for standalone scripts, but some deployment scenarios require different approaches. FastMCP handles these cases automatically.
if __name__
block at all. The CLI will find your FastMCP instance and run it: