db-conn-mcp is a self-hosted Model Context Protocol (MCP) server designed to let AI agents (such as Claude, Cursor, Windsurf, VS Code, Zed, Codex, etc.) safely explore and query databases. It focuses on doing one thing well: delegating security to the simplest primitives—a static JSON file and the database's own read-only transactions—rather than a custom authentication server or fragile SQL parsing.
Key capabilities
- Read-only guarantees: Every query that reads the database runs in a native read-only transaction, and read tools accept only a single read-only statement (SELECT / WITH / VALUES / TABLE / SHOW / EXPLAIN), preventing agents from inserting write operations or flipping the session via SET … READ WRITE.
- No secret leakage: DSNs/passwords are never logged or returned by any tool. Connection failures return redacted diagnostics (category + remediation advice) instead of raw tracebacks containing hosts and credentials.
- Layered write safety: Writes are gated server-side by four gates: schema (hard, native) → dry-run first (commit is refused unless the exact same statement was dry-run) → yolo (persistent trust per database) → user_consent (explicit approval for each operation).
- Zero-friction setup: An interactive wizard registers databases and injects the server into AI client configurations (supports 9 popular clients, each with a different format). It can also be run via
db-conn-mcp guito open a local dashboard that performs the same operations and verifies that each client can actually start the server. - Prompt injection protection: On connection, the server tells the client that everything returned by all tools is untrusted database content that may be crafted as instructions and must never be executed. Each tool's text output is wrapped in explicit markers (<<<UNTRUSTED DATABASE DATA START>>> … <<<UNTRUSTED DATABASE DATA END>>>), and markers in malicious content are neutralized into visible form.
Usage
Requires Python 3.12+. Install command:pipx install db-conn-mcp or uv tool install db-conn-mcp. After installation you get the db-conn-mcp command.
Run db-conn-mcp setup to start the wizard, which asks about: scope (global ~/.db-conn-mcp/connections.json or repository ./connections.json), connection name, DSN (e.g. postgresql://user:pass@host:5432/dbname), schema (read recommended or write), optional fallback ports, and client injection selection. The wizard writes the configuration and optionally registers it with AI clients. Tools become available after restarting/reconnecting the client.
The configuration file connections.json is resolved in order:--config parameter,./connections.json、~/.db-conn-mcp/connections.json. It supports the fallback_ports optional key, which probes additional ports in order when the primary port refuses or times out (only the listed ports, each probe capped at 5 seconds).
Each write replaces the file in one step, and on failure leaves it unchanged. Files created by the tool have permissions 0600. Unknown keys are preserved but have no effect, anddb-conn-mcp doctor will warn. A lock file prevents concurrent modifications. All configuration changes and client injections/removals are recorded in connections.json.audit.log(append-only, does not include DSN, host, user, or password).
Security Model
Writes pass through four gates:
- Mode (hard, native): If the database is not
"mode": "write", writes are rejected, and the connection is opened read-only at the PostgreSQL session level, providing double protection. - Dry-run first (server-enforced):
execute_write_querydefaults todry_run=true. Commits are rejected unless the exact same statement has been dry-run first.skip_dry_run=trueis the only escape hatch, but the server cannot verify that claim. yolo does not exempt this stage. - yolo (persistent trust): Set on the write database
yolo: true, dry-run writes are committed without prompting. - user_consent (per operation): Otherwise the agent must read the schema, show the exact SQL, obtain user consent, and then re-invoke with
user_consent=true.
Dry-run executes the statement within a transaction and always rolls back, returning the rows that would be affected. Commit permission is limited to that exact statement (same database, SQL, and parameters), expires after 10 minutes, and is consumed by a commit attempt (including failed commits).
Use a read-only role (strongest guarantee): It is recommended to create a read-only PostgreSQL role for each database and use its DSN for read connections.
When to Use
Suitable for developers and teams that need to let AI agents safely explore and query PostgreSQL databases, especially scenarios focused on data security and wanting to avoid agents accidentally writing or leaking credentials.
Limitations
- v1 supports PostgreSQL only. All database-specific code lives behind the Dialect seam; adding MySQL/SQLite in the future requires only a new file.
- Requires Python 3.12+.
- The npm package
@idle-sync/db-conn-mcpis just a thin wrapper that runs the same server via uvx, still requires uv, and adds Node on every startup. - Dry-run still executes server-side until rollback (brief locks, sequence advancement, trigger side effects).
- On Windows, link checks for lock files and audit logs run by path before opening; a link planted between the two calls will be followed, and hard links cannot be detected.
- The audit log is git-ignored next to connections.json; add it if you use your own ignore rules.
- Other feature details (such as the specific client list and dashboard feature details) are not yet fully provided.