Skip to main content
Glama
dba-checkup-mcp

dba-checkup-mcp

by vickm26
README.md
# dba-checkup-mcp

[![CI](https://github.com/vickm26/dba-checkup-mcp/actions/workflows/ci.yml/badge.svg)](https://github.com/vickm26/dba-checkup-mcp/actions/workflows/ci.yml)

Read-only database health checks for AI assistants, over the [Model Context Protocol](https://modelcontextprotocol.io).

Point Claude (or any MCP client) at a PostgreSQL database and ask *"is anything wrong with this database?"*. Instead of letting the model write SQL against production, this server gives it a fixed set of checks a DBA would run: blocking locks, long-running and idle-in-transaction sessions, top statements, invalid and unused indexes, tables that need an index, dead rows and transaction ID wraparound, WAL archiving and replication slots. Every check explains what it found in plain words, most serious first.

Built by a DBA with 20+ years of Oracle, SQL Server and PostgreSQL production support.

## Why fixed checks instead of free-form SQL

- **Nothing can change.** Every statement runs inside a `READ ONLY` transaction, on a session that defaults to read-only, with a statement timeout. A test proves writes are refused, even with a superuser login.
- **No surprise queries.** The model picks from known checks; it never writes SQL against your database. The optional `explain_query` tool only *plans* a single SELECT (EXPLAIN without ANALYZE) and is off unless you turn it on.
- **Customer data stays out of the chat.** Statement text is masked by default: every string and number literal becomes `?`, so `WHERE email = 'ana@example.com'` shows as `WHERE email = ?`.
- **Credentials never pass through the AI.** The connection comes from the environment only.
- **Database text is treated as data.** Results are fenced and labeled, and the server tells the model never to follow instructions that appear inside query text or application names.
- **Everything is audited.** Each call is appended to a local JSONL log with timing and outcome.
- **Least privilege.** It is designed for a login with PostgreSQL's built-in `pg_monitor` role, which can read monitoring views but not your tables. `overview` warns if you connect as a superuser.

## Tools

| Tool | What it checks |
|---|---|
| `health_report` | Runs every check and lists what needs attention, most serious first. Start here. |
| `overview` | Version, uptime, connections vs `max_connections`, cache hit ratio, database sizes, login safety. |
| `active_sessions` | Work running now, longest first; long statements and sessions idle inside an open transaction. |
| `blocking_locks` | Who is blocking whom, how long they have waited, and the root blockers. |
| `top_queries` | Statements using the most time (needs `pg_stat_statements`). |
| `storage` | Largest tables and materialized views, with index size. |
| `index_health` | Invalid indexes, unused indexes, large tables read mostly by full scans. |
| `maintenance` | Autovacuum status, dead rows, last vacuum/analyze, transaction ID wraparound age. |
| `replication_backup` | WAL archiving failures, replica lag, replication slots holding back WAL. |
| `explain_query` | *(off by default)* The plan for one SELECT, without running it; flags large full scans. |

## Quick start

1. Create a monitoring login (as a superuser) with [`sql/create_monitor_role.postgres.sql`](sql/create_monitor_role.postgres.sql):

   ```sql
   CREATE ROLE dba_checkup LOGIN PASSWORD 'choose-a-strong-password';
   GRANT pg_monitor TO dba_checkup;
   GRANT CONNECT ON DATABASE your_database TO dba_checkup;
   ```

2. Add it to your MCP client.

   **Claude Desktop** (`claude_desktop_config.json`):

   ```json
   {
     "mcpServers": {
       "dba-checkup": {
         "command": "npx",
         "args": ["-y", "dba-checkup-mcp"],
         "env": { "DBA_CHECKUP_DATABASE_URL": "postgres://dba_checkup:PASSWORD@host:5432/your_database" }
       }
     }
   }
   ```

   **Claude Code**:

   ```
   claude mcp add dba-checkup -e DBA_CHECKUP_DATABASE_URL=postgres://dba_checkup:PASSWORD@host:5432/your_database -- npx -y dba-checkup-mcp
   ```

3. Ask: *"Run a health report on this database and tell me what to fix first."*

It also works without an AI, for cron or a quick look:

```
DBA_CHECKUP_DATABASE_URL=postgres://... npx -y dba-checkup-mcp --report
```

The exit code is 2 if anything is critical, 1 for warnings, 0 when healthy.

## Settings

| Variable | Default | Meaning |
|---|---|---|
| `DBA_CHECKUP_DATABASE_URL` | (required) | Connection string; the standard `PG*` variables also work. |
| `DBA_CHECKUP_QUERY_TEXT` | `masked` | `masked`, `full` or `off`: how much statement text to show. |
| `DBA_CHECKUP_ALLOW_EXPLAIN` | off | `1` registers `explain_query`. Planning needs `SELECT` on the tables involved; grant it only on tables you want explainable. |
| `DBA_CHECKUP_MAX_ROWS` | `25` | Rows per check (max 200). |
| `DBA_CHECKUP_TIMEOUT_MS` | `10000` | Statement timeout. |
| `DBA_CHECKUP_AUDIT_LOG` | `~/.dba-checkup-mcp/audit.jsonl` | Audit file, or `off`. |
| `DBA_CHECKUP_LONG_QUERY_SECONDS` | `300` | Warning threshold for a running statement (`..._VERY_LONG_...` = 1800 for critical). |
| `DBA_CHECKUP_IDLE_IN_TX_SECONDS` | `300` | Warning threshold for idle-in-transaction sessions. |
| `DBA_CHECKUP_XID_AGE_WARN` / `_CRITICAL` | 1.0 / 1.5 billion | Transaction ID age thresholds. |

## What it does not see

Free disk space and backups taken by external tools (pgBackRest, Barman, snapshots) are not visible from SQL; the report says so instead of guessing.

## Roadmap

- Oracle Database (tested on Oracle Autonomous Database), with the same checks.
- SQL Server.

## Development

```
npm install
npm run build
TEST_DATABASE_URL=postgres://postgres:postgres@localhost:5432/postgres npm test
```

The tests build a database with real problems (a blocked session, a long-running statement, an idle transaction, an invalid index, an unused index, a table read by full scans, dead rows) and check that each one is found, that literals are masked, and that writes are refused. CI runs them on Node 20, 22 and 24 against PostgreSQL 16 and 17.

## License

MIT