db-query-mcp
Enables AI assistants to safely query SQLite databases with read-only enforcement, SQL validation, path restrictions, schema inspection, and capped query results.
Click on "Deploy 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., "@db-query-mcpshow me the schema of the users table in data/app.db"
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.
db-query-mcp
让 AI 助手安全查询 SQLite 数据库的 MCP Server —— 只读强制、SQL 校验、结果封顶。
为什么做这个
给 AI 接数据库,最大的风险不是"查不到",而是三件事:
误写 —— 模型把
SELECT写成UPDATE,或者DROP TABLE出现在"顺手清理"里灌爆上下文 —— 一句
SELECT * FROM orders拉回 200 万行,对话直接崩读到不该读的 —— 路径没限制,
db_path: ../../etc/passwd之类的穿越
这个服务把这三条做成设计约束,而不是"提示词里提醒一句"。
Related MCP server: sqlite-analyst
三条安全边界
边界 | 实现 | 绕过尝试的结果 |
只读 | ① SQL 白名单前缀(SELECT/WITH/PRAGMA/EXPLAIN)② 写操作关键字黑名单(引号感知剥注释,防 | 拒绝并返回原因,不执行 |
结果封顶 | 硬上限 500 行 + 单元格超 2000 字符截断 | 返回 |
路径限制 | 库文件必须位于 | 拒绝并列出允许目录 |
只读为什么需要这么多层 —— 对抗性测试与逐层隔离实验的真实教训:
URI 注入:不转义直接拼
f"file:{path}?mode=ro"时,文件名里的#会被解析成 fragment,把?mode=ro整段丢弃 → 连接偷偷变成读写模式。 必须quote()转义路径。两层各能拦什么(逐层隔离实测,每用例全新库):
连接配置
journal_mode=WAL
user_version 写
INSERT
仅
mode=ro拦
拦
拦
仅
query_only拦不住(文件真变 WAL)
拦
拦
两层叠加(本实现)
拦
拦
拦
query_only单独拦不住journal_mode=WAL是 SQLite 的 pragma 特性, 这正是两层都留的原因;行为由test_each_layer_isolation钉住。括号传参绕过:
PRAGMA user_version(123)是 SQLite 的合法语法, 但等号检查拦不住它。只有"名字参数"类 pragma(table_info 等)才允许 带括号。注释混淆与字符串误伤:
DEL/**/ETE在 SQLite 里等于DELETE, 校验前必须剥注释;但剥注释必须引号感知 —— 字符串里的'--x--'不是注释,正则直接剥会破坏合法查询。实现用双视图扫描: 执行用 cleaned(注释删、字符串留),校验用 code_only(字符串遮蔽, 数据里的DELETE/;不会误判)。
这些点都是先被实际绕过、修复后才写进这段文档的(见 git 历史里的
fix(security) 提交和 TestSecurityRegressions 回归测试)。
安装
pip install -e .配置(以 Claude Desktop 为例)
{
"mcpServers": {
"db-query": {
"command": "db-query-mcp",
"env": {
"DB_QUERY_MCP_ROOT": "/path/to/your/databases"
}
}
}
}DB_QUERY_MCP_ROOT 支持多个目录,用 : 分隔(Linux/macOS)或 ;(Windows)。
工具
query
{
"db_path": "data/app.db",
"sql": "SELECT id, email FROM users WHERE created_at > '2026-01-01' LIMIT 50",
"max_rows": 50
}返回:
{
"columns": ["id", "email"],
"rows": [[1, "a@example.com"]],
"row_count": 1,
"truncated": false,
"max_rows": 50
}schema
不传 table:列出全部表和视图。
传 table:返回列定义(类型/非空/默认值/主键)和建表 SQL。
推荐工作流:先 schema 看结构,再写 query —— 这也是给模型的 instructions 里写明的。
会拒绝什么
[拒绝] 只允许 SELECT / WITH / PRAGMA / EXPLAIN 开头的只读查询 # UPDATE ...
[拒绝] 检测到写操作关键字: DROP(本服务只读) # SELECT 1; DROP TABLE x
[拒绝] 只允许单条语句,检测到多个分号
[拒绝] 路径不在允许的根目录内。允许: /home/me/data测试
pip install -e ".[dev]"
pytest tests/ -vLicense
MIT
This server cannot be deployed
Maintenance
Related MCP Connectors
Explore, query, and inspect SQLite databases with ease. List tables, preview results, and view det…
Safe, read-only Postgres and MySQL access for AI agents. Audit log + column-level controls.
Query 40 databases from Claude, ChatGPT, or Cursor — on any device. Read-only, encrypted, audited.
Query your Postgres from ChatGPT or Claude without exposing the database or handing over credentials. Run npx boltschema connect next to your database and it dials out over HTTPS — no inbound firewall rule, no open port, works with localhost and VPC-private databases. Read-only is enforced by a SQL guard, a Postgres READ ONLY transaction, and a scoped role generated for you.
Related MCP Servers
- AlicenseAqualityDmaintenanceEnables safe, read-only SQL access to SQLite databases for AI agents, allowing schema exploration and SELECT queries with defense-in-depth protections.3MIT
- AlicenseNot gradedqualityCmaintenanceEnables AI assistants to explore and query SQLite databases through read-only tools, with defense-in-depth sandboxing preventing any data modifications.MIT
- FlicenseAqualityCmaintenanceEnables AI agents to safely explore and query a SQLite database in read-only mode, allowing them to inspect schema and run analytical SQL queries without risking data modification.3-
- FlicenseAqualityBmaintenanceEnables AI assistants to query SQLite databases using plain language, with strict read-only enforcement and column-level access control to prevent damage or unauthorized data reads.4-