Purpose
SafeDB MCP is a secure Model Context Protocol (MCP) server that lets AI agents access databases in a safe, read-only way. It is aimed directly at teams that need to give agents useful database information but are unwilling to hand over unrestricted production database credentials. Providing database credentials directly to agents is very dangerous: a single bad prompt, tool call, or generated SQL statement could tamper with data, steal sensitive columns, or execute expensive queries. SafeDB MCP places a policy layer between the agent and the database, exposing only configured schemas and tables, parsing and validating SQL before execution, limiting the number of returned rows, masking results, and auditing every query attempt.
Key Capabilities
- MCP tools: list_schemas, list_tables, describe_table, run_readonly_query, explain_query, get_safedb_policy.
- Database support: Postgres via pg; MySQL and MariaDB via mysql2; SQLite files via sql.js.
- Configuration: YAML or JSON configuration with environment variable expansion.
- Read-only SQL protection: AST-based validation of SELECT, WITH ... SELECT, UNION, and EXPLAIN SELECT.
- Table detection: able to identify the real tables involved in joins, CTEs, nested subqueries, aliases, and unions.
- Column projection checks: prevent selecting masked fields via aliases or expressions.
- Policy controls: configurable table allowlists, denylists, row limits, and statement timeouts.
- PII masking: supports redact, email, partial, and deterministic hash.
- Auditing: JSONL audit logs that do not record raw result data.
- Command line: provides the safedb-mcp CLI binary.
- Tech stack: TypeScript, Vitest, ESLint, Prettier.
Usage
It is recommended to use a dedicated role with read-only permissions for the database; SafeDB MCP is a defense-in-depth layer and cannot replace least-privilege database credentials. The project provides a Docker image that packages SafeDB MCP together with Node.js and itsProduction dependency packaging, can run in the same way on any host with Docker installed. You can build the image locally, mount the configuration file to run the MCP server, and pass CLI commands after the image name. For MySQL or MariaDB, set database.type and use the database name as the access mode. For SQLite, set database.type to sqlite, point database.path to the .db file, and use main as the access mode. MCP client configuration examples include Claude Desktop and Cursor or Hermes-style MCP configurations.
Applicable Scenarios
Suitable for teams that need to let AI agents inspect and query databases in a read-only manner, while also wanting to limit visible tables and sensitive fields, control query costs, and retain audit records.
Security Guarantees and Limitations
Security guarantees include: only configured schemas and tables can be inspected or queried through MCP tools; SQL is parsed before execution, and mutating statements or multiple statements are blocked; table access policies are checked against the real tables found in joins, CTEs, nested subqueries, aliases, and unions; masked columns cannot be selected through aliases or expressions to bypass response masking; queries are executed in read-only transactions, and local statement timeouts are used when the driver supports it; the number of returned rows is limited by an outer LIMIT; configured PII fields are masked before tool responses are returned; audit logs record attempts, decisions, detected tables, row counts, and duration, but do not record raw result rows; passwords and secrets are not intentionally logged. In terms of limitations: this project is an MVP, tends to produce false positives and block queries rather than allow unsafe access, and does not claim perfect SQL security; it does not provide a formal proof of query safety; it does not support every dialect-specific read-only SQL construct; it does not support write operations, migrations, stored procedure execution, or COPY; it does not provide per-tool and per-table rate limiting; it does not provide optional OpenTelemetry tracing; it does not provide signed audit logs; and it does not currently provide a published Docker image or Helm chart.