db-readonly-mcp
Provides read-only SQL query and schema inspection tools for MySQL databases, including listing tables, describing table schemas, sampling rows, and executing guarded SELECT queries with enforced read-only sessions and row limits.
Provides read-only SQL query and schema inspection tools for PostgreSQL databases, including listing tables, describing table schemas, sampling rows, and executing guarded SELECT queries with enforced read-only sessions and statement timeouts.
Provides read-only access to local SQLite database files or the built-in demo database, with tools to list tables, describe schemas, sample rows, and run guarded SELECT queries without writing back to disk.
This server does not currently have a deployable release on Glama. Follow the repository's setup instructions to install and configure it for your MCP client, including any required credentials and local dependencies.
db-readonly-mcp
通用只读数据库 MCP Server(支持 SQLite / MySQL / PostgreSQL),内置单语句 SQL 安全沙箱(SQL Guard)、行数硬上限控制、数据库引擎级只读会话保护与 JSONL 审计日志。
A clean-room, production-ready Read-Only Database MCP (Model Context Protocol) Server for SQLite, MySQL, and PostgreSQL, featuring a strict single-statement SQL security guard, automatic row-limit enforcement, engine-level read-only sessions, and structured JSONL audit logging.
架构概览 / Architecture
flowchart LR
Client["AI Client (Claude Desktop / Cursor / Antigravity)"] -->|MCP stdio JSON-RPC| Server["db-readonly-mcp Server"]
Server --> Tools["MCP Tools: list_tables / describe_table / query_readonly / sample_rows"]
Tools --> Guard["SQL Guard (Single-Statement Sandbox + Row Cap)"]
Guard -->|Allowed SELECT/WITH/EXPLAIN/SHOW/DESCRIBE| Adapters["Database Adapters"]
Guard -->|Blocked DML/DDL/File Functions| Audit["Audit Logger (Ring Buffer + JSONL)"]
Adapters --> SQLite["SQLite (sql.js WASM + query_only=ON)"]
Adapters --> MySQL["MySQL (mysql2 + READ ONLY + Timeout)"]
Adapters --> Postgres["PostgreSQL (pg + READ ONLY + statement_timeout)"]
Adapters --> AuditRelated MCP server: PgSafe MCP
核心特性 / Features
多数据库统一只读接入 / Multi-Database Support:
SQLite(基于纯 WASM 的
sql.js,零 C++ 原生编译依赖,内置--demo电商供应链演示库,支持加载本地.sqlite/.db文件且绝不回写磁盘,并开启PRAGMA query_only = ON)。MySQL(基于
mysql2/promise,会话强制SET SESSION TRANSACTION READ ONLY与查询超时控制)。PostgreSQL(基于
pg,会话强制SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY与statement_timeout)。
多层 SQL 安全沙箱 / SQL Security Guard (
src/guard/sqlGuard.ts):仅允许单条
SELECT、WITH、EXPLAIN、SHOW、DESCRIBE语句。单遍词法扫描剥离
--行注释、/* ... */块注释与'...'单引号字面量,同时保留"..."和`...`标识符内容(防止通过"FILE_READ"('/etc/passwd')等引号标识符绕过检测)。拦截多语句
;注入、MySQL 可执行条件注释/*! ... */、未闭合引号/注释、超长查询(上限8000字符)。拦截 DML/DDL 关键字(
INSERT,UPDATE,DELETE,DROP,ALTER,CREATE,TRUNCATE,REPLACE,MERGE,GRANT,REVOKE,CALL,EXEC,EXECUTE,ATTACH,DETACH,COPY,VACUUM,REINDEX,SHUTDOWN,RUNSCRIPT,SCRIPT)。拦截写操作
PRAGMA ... =以及危险文件/系统函数与子句(LOAD_FILE,INTO OUTFILE,INTO DUMPFILE,pg_read_file,pg_read_binary_file,pg_ls_dir,pg_stat_file,FILE_READ,FILE_WRITE,CSVREAD,CSVWRITE,LINK_SCHEMA)。自动强制行数上限(默认
200行,硬上限1000行)。
结构化审计日志 / JSONL Audit Logging (
src/audit/auditLogger.ts):内存环形缓冲区 + 可选
--audit-log <path>JSONL 文件持久化,记录timestamp,tool,dialect,status,rowCount,durationMs,sqlPreview,blockedReason。
MCP 工具列表 / Exposed MCP Tools
Tool Name | Description | Parameters |
| 列出所有表与视图(含行数统计) / List tables & views | None |
| 查看表结构、字段类型、主键与可空性 / Inspect table schema |
|
| 执行单条只读 SQL 查询并强制限制返回行数 / Execute guarded read-only SQL |
|
| 安全采样指定表前 N 行数据 / Safely sample rows from a table |
|
快速开始 / Quick Start
安装与构建 / Install & Build
git clone https://github.com/calmjade/db-readonly-mcp.git
cd db-readonly-mcp
pnpm install
pnpm buildCLI 参数说明 / CLI Usage
# 1. 内置电商与供应链演示库(无需外部数据库)
node dist/cli.js --demo
# 2. 连接本地 SQLite 文件(并开启 JSONL 审计日志)
node dist/cli.js --sqlite ./data/app.sqlite --max-rows 200 --audit-log ./logs/audit.jsonl
# 3. 连接 MySQL
node dist/cli.js --mysql "mysql://readonly_user:password@127.0.0.1:3306/app_db"
# 4. 连接 PostgreSQL
node dist/cli.js --postgres "postgres://readonly_user:password@127.0.0.1:5432/app_db"
# 查看帮助
node dist/cli.js --helpFlag | Description |
| 启动内置内存 SQLite 演示库( |
| 以只读模式加载本地 |
| 连接 MySQL 数据库 |
| 连接 PostgreSQL 数据库 |
| 默认最大返回行数(默认 |
| 将每次工具调用与拦截记录写入 JSONL 审计日志 |
| 显示命令行帮助 |
客户端配置示例 / MCP Client Configuration
以下配置适用于 Claude Desktop、Cursor 及 Antigravity(mcp_config.json / .cursor/mcp.json / claude_desktop_config.json):
使用演示库 / Demo Mode
{
"mcpServers": {
"db-readonly-demo": {
"command": "node",
"args": [
"/absolute/path/to/db-readonly-mcp/dist/cli.js",
"--demo",
"--max-rows",
"200",
"--audit-log",
"/absolute/path/to/audit.jsonl"
]
}
}
}连接 PostgreSQL / MySQL / SQLite
{
"mcpServers": {
"db-readonly-postgres": {
"command": "node",
"args": [
"/absolute/path/to/db-readonly-mcp/dist/cli.js",
"--postgres",
"postgres://readonly_user:password@localhost:5432/production_db",
"--max-rows",
"200"
]
}
}
}安全规则摘要 / Security Rules Summary
允许的语句类型: 仅限
SELECT、WITH、EXPLAIN、SHOW、DESCRIBE(允许末尾至多一个;)。禁止的语法与函数:
多语句分隔符
;与 MySQL 条件执行注释/*! ... */所有 DML / DDL / 系统控制关键字(
INSERT,UPDATE,DELETE,DROP,ALTER,CREATE,TRUNCATE,REPLACE,MERGE,GRANT,REVOKE,CALL,EXEC,EXECUTE,ATTACH,DETACH,COPY,VACUUM,REINDEX,SHUTDOWN,RUNSCRIPT,SCRIPT)写
PRAGMA ... =文件读写与外部链接函数(
LOAD_FILE,INTO OUTFILE,INTO DUMPFILE,pg_read_file,pg_read_binary_file,pg_ls_dir,pg_stat_file,FILE_READ,FILE_WRITE,CSVREAD,CSVWRITE,LINK_SCHEMA)
长度与行数上限: 查询长度上限
8000字符;默认返回最多200行,硬上限1000行。最小权限建议: 即使内置多层 SQL Guard 与只读事务模式,生产环境仍建议为数据库配置独立的只读账号(Only
SELECTprivilege)。
开发与测试 / Development & Testing
pnpm typecheck # TypeScript 类型检查
pnpm build # tsup 打包构建 ESM + d.ts
pnpm test # 运行 Vitest 单元测试与集成测试License
MIT © 2026 Cao Jie (calmjade)
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
This server cannot be deployed
Maintenance
Related MCP Connectors
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 Postgres, MySQL, SQL Server, Oracle, BigQuery, ClickHouse and Redshift from your AI client.
Governed SQL access to your databases: redaction, query review and an audit log.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceLets AI clients ask natural-language questions about a SQL database with production-safe guardrails, schema grounding, and read-only enforcement.MIT
- AlicenseAqualityBmaintenanceEnables AI agents to inspect database schemas and run strictly read-only SQL queries with guardrails against destructive statements, multi-statement chaining, and unbounded scans. Supports SQLite natively and PostgreSQL optionally.430 PyPIMIT
- AlicenseNot gradedqualityCmaintenanceEnables running single, validated SELECT statements against PostgreSQL, MySQL/MariaDB, SQL Server, SQLite, and Oracle through read-only sessions with schema/table inspection, query plans, row limits, timeouts, and an audit log. Keeps the assistant strictly read-only by combining a read-only database user, SQL validation, and automatic rollback.MIT
- AlicenseNot gradedqualityBmaintenanceEnables AI assistants to explore and query PostgreSQL or SQLite databases read-only, with SQL allow-listing, automatic row limits, statement timeouts, column masking, per-tool rate limits and JSONL audit logging.MIT