-
Notifications
You must be signed in to change notification settings - Fork 932
[BUG] lifespan context manager not triggered when using mount_http() in fastapi-mcp 0.4.0 #256
Description
Environment
fastapi-mcp version: 0.4.0
FastAPI version: 0.125.0
Python version: 3.14.2
Description
When using FastApiMCP.mount_http() with a FastAPI application that defines a lifespan context manager, the lifespan events are not triggered. The server logs indicate that the ASGI lifespan protocol is unsupported:
INFO: ASGI 'lifespan' protocol appears unsupported.
This prevents proper initialization and cleanup of resources (database connections, cache, background tasks, etc.) which is critical for production applications.
from fastapi import FastAPI
from fastapi_mcp import FastApiMCP
from contextlib import asynccontextmanager
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(name)
Lifespan context manager
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup
logger.info("🚀 Application starting up...")
logger.info("Initializing database connections...")
logger.info("Loading cache...")
yield
# Shutdown
logger.info("🛑 Application shutting down...")
logger.info("Closing database connections...")
logger.info("Clearing cache...")
FastAPI app with lifespan
app = FastAPI(
title="MCP Service",
description="",
version="1.0.0",
openapi_url="/openapi.json",
docs_url="/docs",
redoc_url="/redoc",
lifespan=lifespan
)
@app.get("/health")
async def health():
return {"status": "healthy"}
MCP setup
mcp = FastApiMCP(app)
mcp.mount_http()
Actual Behavior
Console output:
INFO: Started server process [37181]
INFO: Waiting for application startup.
INFO: ASGI 'lifespan' protocol appears unsupported.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000