|
11 | 11 | from mcp.server.fastmcp import FastMCP |
12 | 12 | from mcp.server.transport_security import TransportSecuritySettings |
13 | 13 |
|
| 14 | +from helpers.health_probe import _run_health_check |
14 | 15 | from helpers.logging import MAIN_LOGGER_NAME, UVICORN_LOGGING_CONFIG |
15 | 16 | from helpers.matomo import ( |
16 | 17 | apply_matomo_request_context, |
@@ -74,21 +75,32 @@ async def app(scope, receive, send): |
74 | 75 | except PackageNotFoundError: |
75 | 76 | app_version = "unknown" |
76 | 77 |
|
77 | | - body = json.dumps( |
78 | | - { |
79 | | - "status": "ok", |
80 | | - "uptime_since": SERVER_START_TIME.isoformat(), |
81 | | - "version": app_version, |
82 | | - "env": os.getenv("MCP_ENV", "unknown"), |
83 | | - "data_env": os.getenv("DATAGOUV_API_ENV", "unknown"), |
84 | | - } |
85 | | - ).encode("utf-8") |
| 78 | + is_healthy = await _run_health_check() |
| 79 | + if is_healthy: |
| 80 | + body = json.dumps( |
| 81 | + { |
| 82 | + "status": "ok", |
| 83 | + "uptime_since": SERVER_START_TIME.isoformat(), |
| 84 | + "version": app_version, |
| 85 | + "env": os.getenv("MCP_ENV", "unknown"), |
| 86 | + "data_env": os.getenv("DATAGOUV_API_ENV", "unknown"), |
| 87 | + } |
| 88 | + ).encode("utf-8") |
| 89 | + http_status = 200 |
| 90 | + else: |
| 91 | + body = json.dumps({"status": "mcp_unavailable"}).encode("utf-8") |
| 92 | + http_status = 503 |
| 93 | + |
86 | 94 | headers = [ |
87 | 95 | (b"content-type", b"application/json"), |
88 | 96 | (b"content-length", str(len(body)).encode("utf-8")), |
89 | 97 | ] |
90 | 98 | await send( |
91 | | - {"type": "http.response.start", "status": 200, "headers": headers} |
| 99 | + { |
| 100 | + "type": "http.response.start", |
| 101 | + "status": http_status, |
| 102 | + "headers": headers, |
| 103 | + } |
92 | 104 | ) |
93 | 105 | await send({"type": "http.response.body", "body": body}) |
94 | 106 | return |
|
0 commit comments