Learn how to run and deploy your FlashMCP server using various transport protocols like STDIO, Streamable HTTP, and SSE.
run()
Methodrun()
method on a FlashMCP
instance.
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
.
MCP servers can be run with a variety of different transport options, depending on your application’s requirements. The run()
method can take a transport
argument and other transport-specific keyword arguments to configure how the server operates.
FlashMCP run
, it ignores the if __name__ == "__main__"
block entirely. Instead, it looks for a FlashMCP object named mcp
, server
, or app
and calls its run()
method directly with the transport options you specify.This means you can use FlashMCP run
to override the transport specified in your code, which is particularly useful for testing or changing deployment methods without modifying the code.dev
command to run your server with the MCP Inspector:
Transport | Use Cases | Recommendation |
---|---|---|
STDIO | Local tools, command-line scripts, and integrations with clients like Claude Desktop | Best for local tools and when clients manage server processes |
Streamable HTTP | Web-based deployments, microservices, exposing MCP over a network | Recommended choice for web-based deployments |
SSE | Existing web-based deployments that rely on SSE | Deprecated - prefer Streamable HTTP for new projects |
run()
. However, you can specify it explicitly to make your intent clear:
New in version: 2.3.0
Streamable HTTP is a modern, efficient transport for exposing your MCP server via HTTP. It is the recommended transport for web-based deployments.
To run a server using Streamable HTTP, you can use the run()
method with the transport
argument set to "streamable-http"
. This will start a Uvicorn server on the default host (127.0.0.1
), port (8000
), and path (/mcp
).
run()
method.
run()
method with the transport
argument set to "sse"
. This will start a Uvicorn server on the default host (127.0.0.1
), port (8000
), and with default SSE path (/sse
) and message path (/messages/
).
SSETransport
to connect to the server. FlashMCP will attempt to infer the appropriate transport from the provided configuration, but HTTP URLs are assumed to be Streamable HTTP (as of FlashMCP 2.3.0).run()
method. You can also adjust the SSE path (which clients should connect to) and the message POST endpoint (which clients use to send subsequent messages).
run()
method seen in previous examples is a synchronous method that internally uses anyio.run()
to run the asynchronous server. For applications that are already running in an async context, FlashMCP provides the run_async()
method.
run()
method cannot be called from inside an async function because it already 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. Note that this is less flexible than using a full ASGI framework, but can be useful for adding simple endpoints like health checks to your standalone server.