|
12 | 12 | import signal |
13 | 13 | import shutil |
14 | 14 | import socket |
15 | | -from contextlib import asynccontextmanager |
16 | | - |
17 | 15 | from fastmcp import FastMCP |
| 16 | +from fastmcp.server.lifespan import lifespan |
18 | 17 | from starlette.responses import JSONResponse |
19 | 18 |
|
20 | 19 | from src.config import load_config |
@@ -84,16 +83,16 @@ def signal_handler(signum, frame): |
84 | 83 | logger.debug("Signal handlers registered for SIGTERM and SIGINT") |
85 | 84 |
|
86 | 85 |
|
87 | | -@asynccontextmanager |
88 | | -async def lifespan(mcp: FastMCP): |
| 86 | +@lifespan |
| 87 | +async def app_lifespan(server: FastMCP): |
89 | 88 | """Startup and shutdown logic for the FastMCP server""" |
90 | 89 | # Load configuration |
91 | 90 | config = load_config("config.yaml") |
92 | 91 | setup_logging(config.server.log_level) |
93 | 92 | logger.info("Starting CodeBadger Server") |
94 | 93 |
|
95 | 94 | # Setup signal handlers for graceful shutdown |
96 | | - _setup_signal_handlers(mcp) |
| 95 | + _setup_signal_handlers(server) |
97 | 96 |
|
98 | 97 | # Ensure required directories exist |
99 | 98 | os.makedirs(config.storage.workspace_root, exist_ok=True) |
@@ -148,26 +147,25 @@ async def lifespan(mcp: FastMCP): |
148 | 147 | ) |
149 | 148 |
|
150 | 149 | # Register MCP tools now that services are initialized |
151 | | - register_tools(mcp, services) |
| 150 | + register_tools(server, services) |
152 | 151 |
|
153 | 152 | logger.info("All services initialized") |
154 | 153 | logger.info("CodeBadger Server is ready") |
155 | 154 |
|
156 | | - yield |
157 | | - |
158 | | - # Shutdown |
159 | | - await _graceful_shutdown() |
160 | | - logger.info("CodeBadger Server shutdown complete") |
| 155 | + yield services |
161 | 156 |
|
162 | 157 | except Exception as e: |
163 | 158 | logger.error(f"Error during server lifecycle: {e}", exc_info=True) |
164 | 159 | raise |
| 160 | + finally: |
| 161 | + await _graceful_shutdown() |
| 162 | + logger.info("CodeBadger Server shutdown complete") |
165 | 163 |
|
166 | 164 |
|
167 | 165 | # Initialize FastMCP server |
168 | 166 | mcp = FastMCP( |
169 | 167 | "CodeBadger Server", |
170 | | - lifespan=lifespan |
| 168 | + lifespan=app_lifespan |
171 | 169 | ) |
172 | 170 |
|
173 | 171 | # Note: Tools are registered inside the lifespan function |
|
0 commit comments