# P5.1→NS3 缺失项 #9:MCP Server 的健康检查端点和监控指标 ## 简要结论 - 状态:⚠️ 部分解决 - 风险等级:🟡 低 - 后端 FastAPI 已有 `/health` 端点,但 MCP Server(`apps/mcp-server/server.py`)无健康检查端点和监控指标 ## 详细审查 ### 审查范围 - `apps/backend/app/main.py` — 后端主入口 - `apps/mcp-server/server.py` — MCP Server 实现 - `apps/mcp-server/pyproject.toml` — MCP Server 依赖配置 ### 发现 #### ✅ 后端 FastAPI 健康检查(已实现) `apps/backend/app/main.py` 中定义了健康检查端点: ```python @app.get("/health", tags=["系统"]) async def health_check(): """健康检查端点,用于探活和监控。""" return {"status": "ok"} ``` 此外还有诊断端点 `/debug/config-paths`,返回关键路径配置信息。 #### ❌ MCP Server 健康检查(未实现) `apps/mcp-server/server.py` 分析: 1. **无 `/health` 端点**:MCP Server 基于 Starlette + MCP SDK 构建,`lifespan` 函数仅管理数据库连接池的打开/关闭,无健康检查路由。 2. **无监控指标**:无请求计数、延迟统计、错误率等监控指标暴露。 3. **有认证中间件**:`AuthMiddleware` 验证 Bearer Token,但无健康检查的豁免路径。 4. **MCP Server 架构**:提供 `list_tables`、`describe_table`、`query_sql` 等数据库查询工具,通过 SSE 协议与 AI 客户端通信。当前无法从外部探测其存活状态。 5. **数据库连接池**:`lifespan` 中 `pool.open(wait=True, timeout=30)` 管理连接池,但连接池健康状态未暴露。 ### 证据 **后端健康检查(main.py)**: ```python @app.get("/health", tags=["系统"]) async def health_check(): return {"status": "ok"} ``` **MCP Server lifespan(server.py L385-391)**: ```python async def lifespan(app: Starlette): pool.open(wait=True, timeout=30) try: async with mcp.session_manager.run(): yield finally: pool.close(timeout=5) ``` **MCP Server 无 health 路由**:搜索 `health|ping|status|monitor` 在 `apps/mcp-server/` 中无匹配结果。 ### 建议 1. **为 MCP Server 添加 `/health` 端点**:在 Starlette 应用中注册健康检查路由,返回连接池状态和服务版本 ```python @app.route("/health") async def health(request): pool_status = "ok" if pool._pool and not pool._pool.closed else "degraded" return JSONResponse({"status": pool_status, "service": "mcp-server"}) ``` 2. **健康检查豁免认证**:在 `AuthMiddleware.dispatch` 中对 `/health` 路径跳过 Token 验证 3. **可选:暴露基础监控指标**:请求计数、平均延迟、连接池使用率等(可通过 Prometheus 格式暴露)