MCP servers
mcp-database
A read-only-by-default MCP server for PostgreSQL, MySQL and SQLite.
A safe-by-default Model Context Protocol server for PostgreSQL, MySQL/MariaDB and SQLite. Let Claude (or any MCP client) explore your schema and answer questions from your data — without handing it a loaded gun.
- Read-only by default. Writes need an explicit
--read-writeflag, and theexecutetool does not even exist until you pass it. - Defence in depth. Every query is checked by a SQL guard and runs inside a read-only transaction / read-only connection, so a guard bypass still cannot write.
- Built for LLMs. Schema discovery tools, parameterised queries, row caps, statement timeouts, compact JSON output, errors that explain what to do next.
- Zero native dependencies. SQLite uses Node's built-in
node:sqlite; Postgres and MySQL use pure-JS drivers.npxjust works. - One server, three engines. The connection string decides the dialect.
Only PostgreSQL, with personal data in it? pg-guard-mcp is the stricter sibling: Postgres only, never writes, masks the columns you mark as personal data and keeps an append-only audit log.
Quick start
Use a database account that can only SELECT (see Create a read-only role).
Claude Desktop — claude_desktop_config.json:
{
"mcpServers": {
"database": {
"command": "npx",
"args": ["-y", "@aranshayan/mcp-database"],
"env": { "DATABASE_URL": "postgres://readonly:secret@localhost:5432/mydb" }
}
}
}
Claude Code:
claude mcp add database --env DATABASE_URL=postgres://readonly:secret@localhost:5432/mydb -- npx -y @aranshayan/mcp-database
Cursor / VS Code / any MCP client: run npx -y @aranshayan/mcp-database as a stdio server with DATABASE_URL in its environment.
| Engine | DATABASE_URL |
|---|---|
| PostgreSQL | postgres://user:pass@host:5432/db |
| MySQL / MariaDB | mysql://user:pass@host:3306/db |
| SQLite | sqlite:///absolute/path/app.db or just ./app.db |
Requires Node.js 22.13+.
Tools
| Tool | What it does |
|---|---|
list_schemas | Schemas / databases on the connection. |
list_tables | Tables and views with type and estimated row count. |
describe_table | Columns (type, nullability, default, primary key, comment), indexes, foreign keys. |
sample_rows | Preview the first 1–100 rows of a table. |
query | Run one read-only statement (SELECT / WITH / VALUES / SHOW / EXPLAIN) with params. |
execute | Only with --read-write. Run one INSERT / UPDATE / DELETE in a transaction. |
All read tools are annotated readOnlyHint; execute is annotated destructiveHint so clients can ask for confirmation.
Options
| Flag | Environment | Default | |
|---|---|---|---|
(url) / --url | DATABASE_URL | — | Connection string. Prefer the environment variable: command-line arguments are visible in process lists. |
--read-write | MCP_DATABASE_READ_WRITE=1 | off | Enables execute. |
--allow-ddl | MCP_DATABASE_ALLOW_DDL=1 | off | With --read-write, also permit CREATE / ALTER / DROP / TRUNCATE. |
--max-rows <n> | MCP_DATABASE_MAX_ROWS | 200 | Rows returned per query (max 5000). |
--timeout-ms <n> | MCP_DATABASE_TIMEOUT_MS | 15000 | Statement timeout. |
Safety model
Three independent layers, so no single bug is fatal:
- SQL guard (
src/sql-guard.ts). Comments, string literals and quoted identifiers are stripped per dialect (MySQL#and--␠comments, PostgreSQL dollar-quoting and nested comments, SQLite[brackets]…), then the statement must be a single statement of an allowed kind. Rejected outright: multiple statements,COPY,CALL,SET,ATTACH,SELECT … INTO OUTFILE,LOAD_FILE(),pg_read_file(),load_extension(), SQLitePRAGMAassignment, MySQL/*! … */executable comments, and more.UPDATE/DELETEwithoutWHEREis refused in read-write mode. - Engine-level read-only.
- PostgreSQL:
BEGIN READ ONLY+SET LOCAL statement_timeout, results streamed through a cursor. - MySQL:
SET SESSION TRANSACTION READ ONLY+START TRANSACTION READ ONLY+MAX_EXECUTION_TIME. (The session-level setting matters: MySQL DDL commits implicitly and would otherwise escape a plain read-only transaction. This is covered by a test.) - SQLite: the file is opened
readOnlyandPRAGMA query_only = ON. Queries run in a separate process with its own read-only connection, which is killed when the statement timeout passes.
- PostgreSQL:
- Your database account. The guard and read-only mode are defences, not a substitute for least privilege. Connect with a role that can only read what the model should see.
Errors are scrubbed of the connection string and password before they reach the model, and the server never logs to stdout (reserved for the protocol).
Create a read-only role
-- PostgreSQL
CREATE ROLE mcp_readonly LOGIN PASSWORD 'change-me';
GRANT CONNECT ON DATABASE mydb TO mcp_readonly;
GRANT USAGE ON SCHEMA public TO mcp_readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO mcp_readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO mcp_readonly;
-- MySQL
CREATE USER 'mcp_readonly'@'%' IDENTIFIED BY 'change-me';
GRANT SELECT ON mydb.* TO 'mcp_readonly'@'%';
Known limits
- SQLite timeouts stop the query process.
node:sqlitecannot interrupt a statement, soqueryandsample_rowsrun in a child process that is killed (SIGKILL) at--timeout-msand restarted for the next query. The schema tools andexecuterun in the server process without a timeout; they are short metadata reads or single writes. An in-memory database (:memory:) only exists inside the server process, so its queries have no timeout. - MySQL buffers a result set before the row cap is applied (
mysql2has no server-side cursor). Keep aLIMITin large queries.MAX_EXECUTION_TIMEbounds time. - Only the first statement type is classified; the engine-level read-only layer is what ultimately prevents writes through exotic constructs.
EXPLAIN ANALYZEactually executes the statement. It is classified by the statement it wraps, and in read-only mode a wrapped write is rejected.
What is tested
npm test runs 100+ tests, including:
- the SQL guard against comment/string/dollar-quote smuggling tricks, per dialect;
- SQLite through
node:sqlite, including a runaway recursive CTE that is stopped at the timeout and its process killed; - PostgreSQL against a real Postgres engine (PGlite behind a TCP socket) through the real
pgdriver, including cursors; - MySQL and MariaDB against real servers. CI runs the suite on MySQL 8.4, MySQL 9 and MariaDB 11; locally, set
TEST_MYSQL_URLto a scratch database (skipped otherwise); - PostgreSQL
statement_timeoutenforcement against a real PostgreSQL 17 server in CI (TEST_PG_URLlocally), including that the pool keeps working after a cancelled statement; - the built CLI over stdio, exactly as an MCP client launches it.
MariaDB-specific code paths (max_statement_time, schemas without STATISTICS.EXPRESSION) now run in CI through the MariaDB 11 job.
Development
npm install
npm test # unit + integration + e2e
npm run typecheck
npm run build # bundles to dist/index.js
# optional: also run the server suites against scratch databases
TEST_MYSQL_URL=mysql://user:pass@127.0.0.1:3306/scratch TEST_PG_URL=postgres://user:pass@127.0.0.1:5432/scratch npm test
License
From the README on GitHub, commit 95ce095, . Found a mistake? Open an issue.