Skip to content

Server is Shutdown when hot reload (FastMCP + FastAPI) #296

Closed
@chungtran4078

Description

@chungtran4078

Description

I added FastAPI into my code to perform reload when something changed.
My server.py:

from fastmcp import FastMCP
from fastapi import FastAPI


app = FastAPI()
mcp = FastMCP.from_fastapi(app, "APCTools")


@mcp.resource(uri="apc://policy", name="The Policy of APC")
async def read_policy() -> str:
    return json.dumps({
        "status": "success",
        "policy": "The policy of APC company allows employees to sleep freely during working hours, receive a salary increase once a month, and arrive late up to 40 hours per week.",
        "error": ""
    })

app.mount("/", mcp.sse_app())

Start server command: uvicorn app.server:app --host 0.0.0.0 --port 8000 --reload

Everything fine till I perform save file server.py, this time server will reload and shutdown

postgres-mcp-server-container  | INFO:     Waiting for application startup.
postgres-mcp-server-container  | INFO:     Application startup complete.
postgres-mcp-server-container  | INFO:     192.168.65.1:41967 - "GET /sse HTTP/1.1" 200 OK
postgres-mcp-server-container  | INFO:     192.168.65.1:26200 - "POST /messages/?session_id=a44e28b433964125b93e57f22692fe6a HTTP/1.1" 202 Accepted
postgres-mcp-server-container  | INFO:     192.168.65.1:26200 - "POST /messages/?session_id=a44e28b433964125b93e57f22692fe6a HTTP/1.1" 202 Accepted
postgres-mcp-server-container  | INFO:     192.168.65.1:26200 - "POST /messages/?session_id=a44e28b433964125b93e57f22692fe6a HTTP/1.1" 202 Accepted
postgres-mcp-server-container  | 2025-05-02 01:55:45,368 - INFO - Processing request of type ListResourcesRequest
postgres-mcp-server-container  | WARNING:  StatReload detected changes in 'server.py'. Reloading...
postgres-mcp-server-container  | INFO:     Shutting down
postgres-mcp-server-container  | INFO:     Waiting for connections to close. (CTRL+C to force quit)

Example Code

Version Information

FastMCP version: 2.2.6
MCP version: 1.6.0
Python version: 3.13.3
Platform: macOS-15.1-x86_64-i386-64bit-Mach-O

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions