Skip to content

[BUG] Uvicorn reload hangs when FastAPI-MCP has active SSE connections #199

@jagerzhang

Description

@jagerzhang

Describe the bug
When using FastAPI-MCP with SSE connections active, Uvicorn's reload functionality gets stuck during shutdown phase, requiring manual process termination. This severely impacts development workflow.

To Reproduce

  1. Create minimal FastAPI app main.py with MCP mounted:
from fastapi import FastAPI
from fastapi_mcp import FastApiMCP

app = FastAPI()

mcp = FastApiMCP(app)

# Mount the MCP server directly to your FastAPI app
mcp.mount()
  1. Start server with reload:

uvicorn --reload --port 9000 main:app

Open browser to SSE endpoint: http://localhost:9000/mcp and hold on:

Image
  1. Modify any file (e.g. main.py) to trigger reload

Expected Behavior:

Server should:

  1. Detect file changes
  2. Gracefully close existing connections
  3. Restart with new code

Actual Behavior:

Server hangs at shutdown phase:
INFO:     Shutting down
INFO:     Waiting for connections to close. (CTRL+C to force quit)

System Information:
fastapi==0.115.12
fastapi-mcp==0.3.6
uvicorn==0.34.2
Python==3.12.10
OS: Linux

Proposed Solutions:

  1. Add proper SSE connection tracking in FastAPI-MCP
  2. Implement graceful shutdown handler for SSE
  3. Provide API to manually close connections before reload

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions