DB Health MCP
# db-health-mcp
**One call. One health report for your database.** An MCP server that lets
Claude, Cursor, or any MCP client run a real checkup on your **MySQL**
database — slow queries, missing indexes, fragmentation, dead-weight
indexes — and returns a single scored report with a concrete fix for every
finding. PostgreSQL is supported too.
> Ask your agent: **"What's wrong with my database?"** — and watch it run
> `health_report` instead of guessing.
## The headline: `health_report`
Most database MCP servers hand the agent a bag of small tools and let it
improvise. `health_report` runs every check at once and folds the results
into one report:
- **Score 0–100 with a letter grade** (transparent heuristic: severity
deductions, capped per category — see `src/db_health_mcp/report.py`)
- **Findings sorted by severity**, each with the numbers behind it
- **A suggested fix per finding**, as SQL text — e.g. the exact
`CREATE INDEX` / `OPTIMIZE TABLE` / `DROP INDEX` statement. The server is
read-only: it proposes fixes, it never executes them.
Sample output (abridged, from a real run against MySQL 8.0.46):
```markdown
# Database health report (mysql)
**Score: 44/100 (grade D)**
- Server: 8.0.46-0ubuntu0.24.04.5
- Database size: 5.2 MB
- Active connections: 2
- Largest table: healthdemo.logs (2.5 MB)
Findings: 14 (1 high, 3 warning, 10 info)
## [HIGH] Table bloat / fragmentation: healthdemo.logs
80% of the table's allocated space is free (10.0 MB of 12.5 MB), left
behind by deletes/updates.
Suggested fix: `OPTIMIZE TABLE `healthdemo`.`logs`;`
## [WARNING] Missing indexes: healthdemo.orders
Full table scans have read 600,000 rows from this ~19,922-row table
(30 full passes).
Suggested fix: `CREATE INDEX idx_orders_<column> ON `healthdemo`.`orders` (<column>); ...`
## [INFO] Unused indexes: healthdemo.users
Index has never been used since the server started, but still costs write
overhead on every insert/update.
Suggested fix: `DROP INDEX `idx_nickname_unused` ON `healthdemo`.`users`;`
```
Verified end to end against live **MySQL 8.0.46** and **PostgreSQL 16.15**
(all six tools, both engines), plus 30 unit tests with mocked rows.
## Individual check tools
For follow-up questions, each check is also a tool of its own:
| Tool | MySQL source | PostgreSQL source |
| --- | --- | --- |
| `db_overview` | version, size, connections, largest tables | same |
| `slow_queries` | `performance_schema` statement digests | `pg_stat_statements` |
| `missing_indexes` | `sys.schema_tables_with_full_table_scans` | sequential-scan stats |
| `table_bloat` | InnoDB fragmentation (`information_schema.data_free`) | dead-tuple ratio |
| `index_usage` | `sys.schema_unused_indexes` | `pg_stat_user_indexes` |
The engine is picked automatically from the connection-string scheme
(`mysql://` vs `postgresql://`), and every tool accepts an optional
`database_url` argument, so one server instance can check several
databases.
**Read-only by design.** Sessions are opened read-only
(`SET SESSION TRANSACTION READ ONLY` / `default_transaction_read_only=on`)
with a statement timeout, and every query is a `SELECT` against
catalog/statistics views. The server cannot modify your database.
## Quick start
```bash
pip install db-health-mcp # or: pip install -e . from a clone
```
### Claude Code
```bash
claude mcp add db-health \
-e DATABASE_URL=mysql://user:pass@localhost:3306/mydb \
-- db-health-mcp
```
### Cursor / other MCP clients
```json
{
"mcpServers": {
"db-health": {
"command": "db-health-mcp",
"env": {
"DATABASE_URL": "mysql://user:pass@localhost:3306/mydb"
}
}
}
}
```
PostgreSQL works the same way with a `postgresql://` URL.
Then just ask:
- "What's wrong with my database?"
- "Give my database a health check and list the fixes in priority order."
- "Which tables are missing indexes?"
- "Are there any indexes I can safely drop?"
## Notes
- MySQL checks need MySQL 8 with `performance_schema` and the `sys` schema
enabled (both are defaults). If statement digests are unavailable,
`slow_queries` tells you how to enable them instead of failing; the other
checks still run.
- PostgreSQL `slow_queries` needs the `pg_stat_statements` extension; same
deal — you get enable instructions, not an error.
- Statistics accumulate over time (MySQL: since server start; PostgreSQL:
since the last stats reset) — on a freshly restarted database the
scan/index findings need some real traffic before they mean much.
- Use a least-privilege, read-only database user for extra safety.
## Development
```bash
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest # unit tests (mocked rows, no live DB needed)
python scripts/selftest.py # MCP handshake + tool list + error paths
```
## Roadmap
- [ ] MariaDB compatibility pass (the `sys` schema and `max_execution_time` differ slightly)
- [ ] SQLite quick-check for local dev databases
- [ ] `explain_query` tool: run `EXPLAIN` on a query and interpret the plan
- [ ] Lock contention and long-running transaction checks
- [ ] Replication lag check
- [ ] A sibling project: cloud capacity/quota checks for AWS/GCP
## License
MIT
TDQS
Scored across 6 tools
Each tool targets a single, distinct diagnostic concern: missing indexes, table bloat, unused indexes, slow queries, and a snapshot overview. The one potential overlap (health_report vs. individual checks) is clearly framed as an aggregate that runs all checks in one call, so there's no confusion about when to use which.
All names are snake_case noun phrases describing the returned data (missing_indexes, table_bloat, index_usage, health_report, db_overview, slow_queries) — internally consistent rather than verb_noun. Minor singular/plural mixing (missing_indexes vs. table_bloat) keeps it just short of perfect.
Six tools is well-scoped for a DB health diagnostic server: five focused checks plus one aggregate report. Each tool earns its place with no redundancy beyond the intentional roll-up.
Covers the core health signals — index gaps, bloat, unused indexes, slow queries, and a version/size/connection snapshot — with an aggregate report. Gaps exist for lock/wait analysis, cache hit ratios, and replication lag, but these are workaround-able omissions rather than blocking holes.