Skip to content

Commit 43e7efd

Browse files
feat(mcp): integrate MCP server lifespan management
- Create MCP HTTP app for lifespan integration - Initialize MCP server lifespan for session management - Update app.mount to use the new MCP HTTP app
1 parent 8814bc5 commit 43e7efd

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

api/main.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
logger = logging.getLogger(__name__)
4444

4545

46+
# Create MCP HTTP app (needed for lifespan integration)
47+
mcp_http_app = mcp_server.http_app(path="/mcp")
48+
49+
4650
@asynccontextmanager
4751
async def lifespan(app: FastAPI):
4852
"""Manage application lifecycle."""
@@ -56,7 +60,10 @@ async def lifespan(app: FastAPI):
5660
except Exception as e:
5761
logger.error(f"Failed to initialize database: {e}")
5862

59-
yield
63+
# Initialize MCP server lifespan (required for session management)
64+
async with mcp_http_app.lifespan(app):
65+
logger.info("MCP server initialized")
66+
yield
6067

6168
# Cleanup database connection
6269
logger.info("Shutting down pyplots API...")
@@ -126,7 +133,8 @@ async def add_cache_headers(request: Request, call_next):
126133

127134

128135
# Mount MCP server for AI assistant integration
129-
app.mount("/mcp", mcp_server.http_app())
136+
# Note: mcp_http_app is created earlier with lifespan integration
137+
app.mount("/mcp", mcp_http_app)
130138

131139
# Register routers
132140
app.include_router(health_router)

0 commit comments

Comments
 (0)