health_check
Check MySQL database connection health and server status to verify operational readiness and diagnose connectivity issues.
Instructions
Check database connection health and get server status
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:602-641 (handler)The handler function that executes the health_check tool logic: pings the database connection for latency, fetches server version and key status metrics (uptime, threads_connected, questions).async () => { const startTime = Date.now(); const p = await getPool(); // Test connection with ping const connection = await p.getConnection(); await connection.ping(); connection.release(); const pingLatency = Date.now() - startTime; // Get server version and status const [versionRows] = await p.query<RowDataPacket[]>("SELECT VERSION() as version"); const [statusRows] = await p.query<RowDataPacket[]>("SHOW STATUS WHERE Variable_name IN ('Uptime', 'Threads_connected', 'Questions')"); const version = versionRows[0]?.version || "unknown"; const status: Record<string, string> = {}; for (const row of statusRows) { status[row.Variable_name] = row.Value; } const output = { healthy: true, pingLatencyMs: pingLatency, serverVersion: version, uptime: status.Uptime ? parseInt(status.Uptime, 10) : null, threadsConnected: status.Threads_connected ? parseInt(status.Threads_connected, 10) : null, totalQueries: status.Questions ? parseInt(status.Questions, 10) : null, }; return { content: [ { type: "text" as const, text: `Database connection healthy (ping: ${pingLatency}ms, version: ${version})`, }, ], structuredContent: output, }; }
- src/index.ts:598-642 (registration)Registers the health_check tool with the MCP server, specifying the tool name, description, empty input schema, and handler function.server.tool( "health_check", "Check database connection health and get server status", {}, async () => { const startTime = Date.now(); const p = await getPool(); // Test connection with ping const connection = await p.getConnection(); await connection.ping(); connection.release(); const pingLatency = Date.now() - startTime; // Get server version and status const [versionRows] = await p.query<RowDataPacket[]>("SELECT VERSION() as version"); const [statusRows] = await p.query<RowDataPacket[]>("SHOW STATUS WHERE Variable_name IN ('Uptime', 'Threads_connected', 'Questions')"); const version = versionRows[0]?.version || "unknown"; const status: Record<string, string> = {}; for (const row of statusRows) { status[row.Variable_name] = row.Value; } const output = { healthy: true, pingLatencyMs: pingLatency, serverVersion: version, uptime: status.Uptime ? parseInt(status.Uptime, 10) : null, threadsConnected: status.Threads_connected ? parseInt(status.Threads_connected, 10) : null, totalQueries: status.Questions ? parseInt(status.Questions, 10) : null, }; return { content: [ { type: "text" as const, text: `Database connection healthy (ping: ${pingLatency}ms, version: ${version})`, }, ], structuredContent: output, }; } );