How to Deploy a FastMCP Server to Production

Run your server over HTTP, add health checks, scale with stateless mode and put it behind systemd and nginx.

Deployment dashboard showing a healthy FastMCP server and its build log

Key takeaways

  • Run over HTTP directly or as an ASGI app with Uvicorn.
  • Add a /health custom route for probes.
  • Enable stateless mode before running several workers.
  • Terminate TLS with nginx and set proxy_buffering off.

A server that works on your laptop is only halfway there. This guide covers the decisions and settings that turn it into a dependable service other people can rely on, from the first HTTP run to TLS, scaling and monitoring.

Choose How to Host

You have two broad options.

  • Self-managed hosting means running the HTTP server on infrastructure you control: a virtual machine, a container platform or a platform-as-a-service.
  • Managed hosting is offered by Prefect through FastMCP Gateway. You push your server to a GitHub repository, sign in with GitHub, create a project from the repository and set the entry point (for example my_server.py:mcp). The server then becomes available at a URL such as https://your-project.fastmcp.app/mcp.

Managed hosting removes most operational work. Self-managed hosting gives you full control over the network, data location and costs. The rest of this guide focuses on self-managed hosting, because it involves more decisions.

Prepare the Project

  • Pin dependencies. Commit uv.lock or a requirements.txt with exact versions.
  • Move configuration to environment variables. Database URLs, API keys and tokens should never live in source files.
  • Add tests. A deployment pipeline should refuse to ship a server whose tests fail.

Option 1: Run the HTTP Server Directly

from fastmcp import FastMCP

mcp = FastMCP("My Server")


@mcp.tool
def process_data(input: str) -> str:
    """Process data on the server."""
    return f"Processed: {input}"


if __name__ == "__main__":
    mcp.run(transport="http", host="0.0.0.0", port=8000)

Binding to 0.0.0.0 makes the server reachable from outside the machine or container. The equivalent CLI command is fastmcp run server.py --transport http --host 0.0.0.0 --port 8000. This is the simplest route and suits small services.

Option 2: Serve an ASGI App with Uvicorn

For more control over workers and middleware, create an ASGI application and run it with Uvicorn:

# app.py
from my_server import mcp

app = mcp.http_app()
uvicorn app:app --host 0.0.0.0 --port 8000

The default endpoint path is /mcp. Change it with mcp.http_app(path="/api/mcp") if it conflicts with an existing route.

Add a Health Check

Load balancers and orchestrators need a cheap endpoint to probe. A custom route provides one:

from starlette.requests import Request
from starlette.responses import JSONResponse


@mcp.custom_route("/health", methods=["GET"])
async def health_check(request: Request) -> JSONResponse:
    return JSONResponse({"status": "healthy", "service": "mcp-server"})

Custom routes skip the server’s authentication middleware, which is exactly what a health probe needs. For the same reason, never return sensitive information from them.

Scale with Stateless Mode

By default, sessions live in the memory of a single process. If a load balancer spreads requests across several workers or instances, a client may reach a worker that does not know its session. Stateless mode removes that dependency:

app = mcp.http_app(stateless_http=True)
FASTMCP_STATELESS_HTTP=true uvicorn app:app --host 0.0.0.0 --port 8000 --workers 4

Start with a single worker and add more only when monitoring shows that you need them.

Allow Browser Clients Carefully

Most MCP clients are not browsers and need no CORS configuration. If a browser-based client such as the MCP Inspector must connect, allow exact origins only and expose the session header:

from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware

middleware = [
    Middleware(
        CORSMiddleware,
        allow_origins=["https://app.example.com"],
        allow_methods=["GET", "POST", "DELETE", "OPTIONS"],
        allow_headers=["mcp-protocol-version", "mcp-session-id", "Authorization", "Content-Type"],
        expose_headers=["mcp-session-id"],
    )
]

app = mcp.http_app(middleware=middleware)

Avoid allow_origins=["*"] in production. FastMCP also offers an opt-in guard that checks Host and Origin headers, enabled with host_origin_protection=True and lists of allowed hosts and origins.

Keep It Running with systemd

On a Linux server, systemd restarts the process if it stops and starts it again after a reboot. Create /etc/systemd/system/fastmcp.service:

[Unit]
Description=FastMCP Server
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/opt/fastmcp
ExecStart=/opt/fastmcp/.venv/bin/uvicorn app:app --host 127.0.0.1 --port 8000
Restart=always
RestartSec=5
Environment="PATH=/opt/fastmcp/.venv/bin"

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now fastmcp
sudo systemctl status fastmcp

Binding to 127.0.0.1 here is deliberate: only nginx, on the same machine, should reach Uvicorn directly.

Terminate TLS with nginx

Place nginx in front of Uvicorn to handle HTTPS. Streaming responses require buffering to be switched off:

location / {
    proxy_pass http://127.0.0.1:8000;
    proxy_http_version 1.1;
    proxy_set_header Connection '';
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_buffering off;
    proxy_cache off;
    proxy_read_timeout 300s;
    proxy_send_timeout 300s;
}
sudo nginx -t && sudo systemctl reload nginx

If clients connect but never receive streamed results, check proxy_buffering off first.

Running in a Container

Most container platforms expect your server packaged with a Dockerfile or a requirements file. The principle is the same as above: install your pinned dependencies, then run the Uvicorn command as the container’s entry point, bound to 0.0.0.0 so the platform can route traffic to it. Pass secrets through the platform’s environment-variable settings rather than baking them into the image.

Monitoring and Logs

  • Probe the /health endpoint from your uptime monitor.
  • Collect logs centrally; journalctl -u fastmcp shows them on a systemd host.
  • Watch error rates and response times after each release, and keep the previous version ready to roll back.

Production Checklist

  • Authentication is configured and tested.
  • Secrets come from environment variables, not source files.
  • The FastMCP version is pinned or locked.
  • HTTPS is enforced through a reverse proxy.
  • A health endpoint is monitored.
  • Stateless mode is enabled if more than one worker runs.
  • CORS is enabled only if browser clients need it, with exact origins.
  • Logs are collected somewhere you can search.

Frequently Asked Questions

For public servers, yes. A reverse proxy such as nginx handles HTTPS certificates and keeps Uvicorn bound to localhost. Some platforms provide this layer for you automatically.

Sessions live in one process's memory by default. Behind a load balancer, a request may reach a different worker. Enable stateless mode with stateless_http=True or the matching environment variable.

Yes. Prefect offers managed hosting through FastMCP Gateway: connect a GitHub repository, set the entry point and receive an HTTPS URL. Container platforms are another option.

In environment variables or the platform's secrets manager. Keeping tokens and connection strings out of source code prevents leaks and lets the same build run in every environment.

Keep reading

Related Guides