mcp-db-server
Allows querying PostgreSQL databases with read-only, validated SQL, enforcing row caps and statement timeouts.
Allows querying SQLite databases with read-only, validated SQL, enforcing row caps and statement timeouts.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@mcp-db-serverWhat are the top 10 customers by total spending?"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
mcp-db-server
A production-grade MCP server that lets any LLM client (Claude Desktop, Cursor, …) query a SQL database in natural language — read-only, AST-validated, and capped.
The LLM writes the SQL. This server never trusts it. Every query is parsed to an abstract syntax tree and forced through a safety gate before it is allowed near a read-only database connection.
Why this exists
Letting an LLM run SQL against your database is powerful and terrifying in equal measure. The hard part isn't generating SQL — clients already do that well. The hard part is guaranteeing the generated SQL can't read what it shouldn't, can't write, and can't run away with your database. That guarantee is this project.
Related MCP server: PostgreSQL MCP Server
Safety guarantees
Every run_query call must pass the Safety Core before execution:
Read-only — only
SELECT/WITH … SELECTsurvive; all DML/DDL is rejected at the AST level (not by keyword regex, so comment and casing tricks don't help).Single statement — stacked queries (
SELECT …; DROP …) are rejected.Table access control — allow-list and deny-list enforced against the parsed tables.
Row caps — a
LIMITis injected/enforced atMCP_DB_MAX_ROWS.Statement timeout — long queries are aborted.
Audit log — every attempt (allowed or blocked) is logged.
These aren't aspirations — each is pinned by an adversarial test in
tests/test_safety.py (casing tricks, comment injection,
stacked statements, SELECT … INTO, PRAGMA/ATTACH, and more). A second,
independent layer is proven in tests/test_engine.py: even a
write that somehow reached the engine is rejected by the read-only connection.
Tools
Tool | Purpose |
| Tables visible under the access policy |
| Columns, types, keys |
| Explains a query (and engine plan) without returning rows |
| Validated, capped, read-only result set |
Quickstart
git clone https://github.com/rudraraval4/mcp-db-server
cd mcp-db-server
pip install -e ".[dev]"
python scripts/seed_db.py # creates demo.db (e-commerce sample data)Try it in 30 seconds (no MCP client needed)
The bundled mcp-db-demo CLI drives the exact same service the MCP server
exposes — so what you see here is what an LLM client gets:
mcp-db-demo tables
mcp-db-demo describe customers
mcp-db-demo query "SELECT country, COUNT(*) FROM customers GROUP BY country ORDER BY 2 DESC"
# the safety layer in action — every one of these is refused:
mcp-db-demo query "UPDATE products SET price = 0"
mcp-db-demo query "SELECT * FROM customers; DROP TABLE customers"Use it in Claude Desktop
Add the server to your claude_desktop_config.json, then fully restart Claude Desktop.
macOS:
~/Library/Application Support/Claude/claude_desktop_config.jsonWindows:
%APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"db": {
"command": "mcp-db-server",
"env": {
"MCP_DB_DATABASE_URL": "sqlite:///absolute/path/to/demo.db",
"MCP_DB_MAX_ROWS": "1000"
}
}
}
}
commandmust resolve to the installedmcp-db-serverexecutable. If it isn't on Claude Desktop'sPATH, use the absolute path (e.g. inside your virtualenv:.../.venv/Scripts/mcp-db-server.exeon Windows,.../.venv/bin/mcp-db-serveron macOS/Linux).
Then just ask, in plain English:
"What tables are in the database?" · "Which five customers spent the most?" · "Delete the orders table" → politely refused.
Configuration
All knobs are environment variables (prefix MCP_DB_):
Variable | Default | Meaning |
|
| SQLAlchemy URL (SQLite or Postgres) |
|
| Hard row cap |
|
| Abort slow queries |
| (empty) | Allow-list; if set, only these tables |
| (empty) | Deny-list |
|
| Audit log location |
Architecture
The client LLM does the natural-language → SQL reasoning. The server contributes the thing clients can't safely do themselves: a hard, enforced boundary around what that SQL is allowed to do. Two independent layers stand between a query and your data — the Safety Core (refuses to emit anything but a capped, read-only SELECT) and the engine (refuses to execute a write, regardless).
Project layout
src/mcp_db_server/
config.py # env-driven settings — every safety knob
safety.py # Safety Core: sqlglot AST validation (the heart)
engine.py # SQLAlchemy read-only access + introspection
service.py # shared logic behind both front-ends
server.py # MCP server (FastMCP, stdio)
cli.py # mcp-db-demo: same service, no MCP client needed
formatting.py # result rendering
audit.py # JSONL audit log
scripts/seed_db.py # reproducible demo database
tests/ # 73 tests, 97% coverageDevelopment
pip install -e ".[dev]"
pytest # 73 tests
pytest --cov=mcp_db_server # coverageOptional Postgres support: pip install -e ".[postgres]" and point
MCP_DB_DATABASE_URL at a postgresql://… URL.
How I built this
A short write-up of the design and the key decision (NL→SQL belongs in the client, not the server) is in WRITEUP.md.
License
MIT — see LICENSE.
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Latest Blog Posts
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/rudraraval4/mcp-db-server'
If you have feedback or need assistance with the MCP directory API, please join our Discord server