SQL MCP Server
Enables connecting to MySQL databases, optionally through an SSH tunnel, then inspecting schema and running allowed query, insert, and delete operations with table allowlists and row limits.
Enables connecting to SQLite database files, inspecting schema, and running allowed query, insert, and delete operations with table allowlists and row limits.
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., "@SQL MCP Server Show me the schema of the users table and find any duplicate emails"
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.
SQL MCP Server
面向 Claude Desktop / Claude Code 的 MCP 数据库服务,支持 MySQL 和 SQLite,内置表白名单、操作限制和 SSH 隧道。
目录
Related MCP server: MCP Database Server
✨ 亮点
零安装 —
npx即可直接运行,无需全局安装双数据库 — 同时支持 MySQL 和 SQLite
SSH 隧道 — 内置 MySQL + SSH 隧道支持,轻松连接远程数据库
权限控制 — 支持表白名单与操作白名单,双重防护
开箱即用 — 兼容 Claude Desktop 配置格式
使用场景
1. 本地开发,批量插入测试数据
让 LLM 通过 db_insert 往开发库或 SQLite 文件里批量写入测试数据,快速准备联调环境、演示数据或回归测试样本。
2. 联调时快速理解数据库结构
让 LLM 先执行 db_connect 和 db_describe_schema,快速了解有哪些表、字段类型和约束,减少手动翻表结构的时间。
3. 排查脏数据或线上问题
在只开放 connect,schema,query 的前提下,让 LLM 帮你查询异常记录、比对状态字段、定位重复数据或缺失数据。
4. 做项目统计、报表和运营分析
适合统计提交人数、重复提交、逾期提交、订单汇总、用户活跃度等场景,让 LLM 把查询结果整理成可读报告。
5. 通过 SSH 隧道安全访问远程 MySQL
当数据库只能从堡垒机或跳板机访问时,可以结合 SSH 隧道能力,把远程数据库以受控方式接给 Claude 使用。
6. 分权限开放数据库能力
你可以按环境控制能力范围:开发环境开放 insert/delete,生产环境只开放 connect/schema/query,降低误操作风险。
效果预览
下面两张图分别展示了典型使用流程和最终输出效果:
工具调用过程
LLM 先按 MCP 协议连接数据库、识别表结构,再分步发起查询。
统计结果输出
查询完成后,LLM 会基于工具返回结果生成结构化报告。
🔍 工作原理
ALLOWED_METHODS 双层校验
ALLOWED_METHODS 通过以下机制控制 LLM 可调用的操作:
工具隐藏 — 服务启动时解析白名单,
ListTools阶段只向 LLM 暴露被允许的工具执行拦截 —
CallTool执行时再次校验,未授权方法直接返回错误,不执行任何数据库操作
# 示例:LLM 只能查询,不能写入
ALLOWED_METHODS='connect,schema,query' npx -y @kk-2004/sql-mcp-server \
--mode=sqlite \
--db-path=/path/to/database.db \
--tables='*'LLM 如何操作数据库?
LLM 本身不会直接连接数据库,而是通过 MCP 工具间接操作:
用户提问
└─▶ LLM 决策(调用哪个工具、传什么参数)
└─▶ MCP 客户端发起工具调用
└─▶ 服务端:权限检查 → 参数校验 → 执行 SQL
└─▶ 返回 JSON 结果给 LLM
└─▶ LLM 生成最终回复服务端返回给 LLM 的格式示例:
{
"content": [
{
"type": "text",
"text": "{\"rowCount\": 2, \"rows\": [...]}"
}
]
}🚀 快速开始
方式一:npx(推荐)
SQLite
npx -y @kk-2004/sql-mcp-server \
--mode=sqlite \
--db-path=/path/to/database.db \
--tables='*'SQLite + 环境变量
DB_MODE=sqlite \
SQLITE_DB_PATH=/path/to/database.db \
ALLOWED_TABLES='users,orders' \
ALLOWED_METHODS='connect,schema,query' \
DEFAULT_LIMIT=50 \
MAX_LIMIT=500 \
npx -y @kk-2004/sql-mcp-serverMySQL
npx -y @kk-2004/sql-mcp-server \
--mysql-host=127.0.0.1 \
--mysql-port=3306 \
--mysql-user=root \
--mysql-password=password \
--mysql-database=mydb \
--tables='*'MySQL + 连接串
npx -y @kk-2004/sql-mcp-server \
--mysql-url='mysql://root:password@127.0.0.1:3306/mydb' \
--tables='users,orders' \
--methods='connect,schema,query,insert'MySQL + SSH 隧道(私钥路径)
npx -y @kk-2004/sql-mcp-server \
--ssh-enabled=true \
--ssh-host=jump.example.com \
--ssh-port=22 \
--ssh-user=ubuntu \
--ssh-private-key-path=/Users/you/.ssh/id_rsa \
--mysql-host=127.0.0.1 \
--mysql-port=3306 \
--mysql-user=root \
--mysql-password=password \
--mysql-database=mydb \
--tables='*'MySQL + SSH 隧道(私钥内容)
npx -y @kk-2004/sql-mcp-server \
--ssh-enabled=true \
--ssh-host=jump.example.com \
--ssh-user=ubuntu \
--ssh-private-key='-----BEGIN OPENSSH PRIVATE KEY-----\n...\n-----END OPENSSH PRIVATE KEY-----' \
--ssh-passphrase='your-passphrase' \
--mysql-host=127.0.0.1 \
--mysql-user=root \
--mysql-password=db-password \
--mysql-database=mydb \
--tables='*'方式二:本地安装
# npm
npm install @kk-2004/sql-mcp-server
# pnpm
pnpm add @kk-2004/sql-mcp-server
# yarn
yarn add @kk-2004/sql-mcp-server
# bun
bun add @kk-2004/sql-mcp-server安装后运行:
# 通过本地二进制
./node_modules/.bin/sql-mcp-server --mode=sqlite --db-path=/path/to/database.db --tables='*'
# 通过 npm exec
npm exec sql-mcp-server -- --mode=sqlite --db-path=/path/to/database.db --tables='*'
# 通过 node
node ./node_modules/@kk-2004/sql-mcp-server/server.js --mode=sqlite --db-path=/path/to/database.db --tables='*'🔌 集成
以下配置可直接放入 Claude Desktop 配置文件。
macOS / Linux 可以直接使用
npx。 Windows 原生环境建议使用cmd /c npx ...,避免 Claude 启动 MCP 服务时找不到npx。
SQLite
{
"mcpServers": {
"sql-mcp": {
"command": "npx",
"args": [
"-y",
"@kk-2004/sql-mcp-server",
"--mode=sqlite",
"--db-path=/path/to/database.db",
"--tables=*"
]
}
}
}Windows(npx 启动示例)
{
"mcpServers": {
"sql-mcp": {
"command": "cmd",
"args": [
"/c",
"npx",
"-y",
"@kk-2004/sql-mcp-server",
"--mode=sqlite",
"--db-path=C:\\path\\to\\database.db",
"--tables=*"
]
}
}
}MySQL
{
"mcpServers": {
"sql-mcp": {
"command": "npx",
"args": [
"-y",
"@kk-2004/sql-mcp-server",
"--mysql-host=127.0.0.1",
"--mysql-port=3306",
"--mysql-user=root",
"--mysql-password=password",
"--mysql-database=mydb",
"--tables=users,orders"
]
}
}
}MySQL + SSH 隧道
{
"mcpServers": {
"sql-mcp": {
"command": "npx",
"args": [
"-y",
"@kk-2004/sql-mcp-server",
"--ssh-enabled=true",
"--ssh-host=jump.example.com",
"--ssh-port=22",
"--ssh-user=ubuntu",
"--ssh-private-key-path=/Users/you/.ssh/id_rsa",
"--mysql-host=127.0.0.1",
"--mysql-port=3306",
"--mysql-user=root",
"--mysql-password=password",
"--mysql-database=mydb",
"--tables=*"
]
}
}
}使用环境变量(推荐用于敏感信息)
{
"mcpServers": {
"sql-mcp": {
"command": "npx",
"args": ["-y", "@kk-2004/sql-mcp-server"],
"env": {
"DB_MODE": "mysql",
"MYSQL_HOST": "127.0.0.1",
"MYSQL_PORT": "3306",
"MYSQL_USER": "root",
"MYSQL_PASSWORD": "your-password",
"MYSQL_DATABASE": "mydb",
"ALLOWED_TABLES": "users,orders",
"ALLOWED_METHODS": "connect,schema,query,insert,delete",
"DEFAULT_LIMIT": "100",
"MAX_LIMIT": "1000",
"ALLOW_EMPTY_DELETE": "false"
}
}
}
}本地安装版配置
方式 A:直接调用本地二进制
{
"mcpServers": {
"sql-mcp": {
"command": "/Users/you/project/node_modules/.bin/sql-mcp-server",
"args": ["--mode=sqlite", "--db-path=/path/to/database.db", "--tables=*"]
}
}
}方式 B:通过 node 调用入口文件
{
"mcpServers": {
"sql-mcp": {
"command": "node",
"args": [
"/Users/you/project/node_modules/@kk-2004/sql-mcp-server/server.js",
"--mode=mysql",
"--mysql-host=127.0.0.1",
"--mysql-user=root",
"--mysql-database=mydb",
"--tables=users,orders"
]
}
}
}⚙️ 配置参考
优先级:CLI 参数 > 环境变量
提示:使用MYSQL_URL时,建议将数据库名写入连接串,如mysql://root:password@127.0.0.1:3306/mydb
通用参数
参数 | 环境变量 | 命令行参数 | 必填 | 默认值 | 说明 |
数据库模式 |
|
| 否 |
| 支持 |
表白名单 |
|
| 是 | — | 允许访问的表,逗号分隔, |
允许方法 |
|
| 否 |
| 允许的操作,逗号分隔 |
默认查询限制 |
|
| 否 |
| 默认最大返回行数 |
最大查询限制 |
|
| 否 |
| 返回行数上限 |
允许无条件删除 |
|
| 否 |
| 是否允许不带 |
MySQL 参数
参数 | 环境变量 | 命令行参数 | 必填 | 默认值 | 说明 |
连接字符串 |
|
| 否 | — | 完整的 MySQL 连接字符串 |
主机 |
|
| 否 |
| MySQL 服务器地址 |
端口 |
|
| 否 |
| MySQL 服务器端口 |
用户名 |
|
| 否 |
| MySQL 用户名 |
密码 |
|
| 否 | — | MySQL 密码 |
数据库 |
|
| 是 | — | 数据库名称 |
SQLite 参数
参数 | 环境变量 | 命令行参数 | 必填 | 默认值 | 说明 |
模式 |
|
| 否 |
| 设为 |
数据库路径 |
|
| 是(SQLite 模式) | — | SQLite 数据库文件路径 |
SSH 隧道参数
参数 | 环境变量 | 命令行参数 | 必填 | 默认值 | 说明 |
启用 SSH |
|
| 否 |
| 是否启用 SSH 隧道 |
SSH 主机 |
|
| 否 | — | SSH 跳板机地址 |
SSH 端口 |
|
| 否 |
| SSH 端口 |
SSH 用户 |
|
| 否 | — | SSH 用户名 |
私钥内容 |
|
| 否 | — | 直接传入私钥内容 |
私钥路径 |
|
| 否 | — | 私钥文件路径 |
私钥密码 |
|
| 否 | — | 私钥密码(如有) |
SSH 密码 |
|
| 否 | — | SSH 登录密码(替代私钥) |
本地绑定主机 |
|
| 否 |
| SSH 隧道本地监听地址 |
本地绑定端口 |
|
| 否 |
| 本地监听端口, |
目标主机 |
|
| 否 | 自动推断 | SSH 隧道转发目标主机 |
目标端口 |
|
| 否 | 自动推断 | SSH 隧道转发目标端口 |
🛠️ 支持的工具
工具 | 描述 |
| 连接数据库并验证连接 |
| 查看表结构 |
| 查询数据(SELECT) |
| 插入数据(INSERT) |
| 删除数据(DELETE) |
📖 使用示例
db_connect
请求
{}响应
{
"connected": true,
"database": "mydb",
"tables": ["users", "orders"]
}db_describe_schema
查询单表
{ "table": "users" }查询所有允许的表
{}db_query
{
"table": "users",
"columns": ["id", "name", "email"],
"where": { "status": "active" },
"orderBy": { "column": "created_at", "direction": "DESC" },
"limit": 10
}db_insert
{
"table": "users",
"data": {
"name": "John Doe",
"email": "john@example.com"
}
}db_delete
{
"table": "users",
"where": { "id": 123 }
}🔒 安全注意事项
标识符校验 — 表名和字段名均通过合法性校验,防止注入
参数化查询 — 所有条件值使用参数化查询,杜绝 SQL 注入
强制 WHERE —
DELETE默认要求携带WHERE条件,可通过ALLOW_EMPTY_DELETE=true关闭结果限制 — 查询结果有最大行数限制,防止大量数据泄露
🧑💻 本地开发
git clone https://github.com/kK-2004/sql-mcp.git
cd sql-mcp
npm install
node server.js --mode=sqlite --db-path=/path/to/database.db --tables='*'Available Tools
5 toolsdb_connectB
Connect to database and verify credentials/database access.
Inputs: no named parameters.
db_deleteC
Delete rows from an allowed table (where required by default).
Inputs: table (required), where.
db_describe_schemaB
Show table schema in current database (allowed tables only).
Inputs: table.
db_insertC
Insert one row into an allowed table.
Inputs: data (required), table (required).
db_queryC
Query rows from an allowed table.
Inputs: table (required), limit, where, columns, +1 more.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 5 tools
Each tool targets a distinct database operation: connection, schema inspection, row querying, row insertion, and row deletion. There is no meaningful overlap between them, and an agent can easily select the right tool.
All tool names use a consistent `db_` prefix followed by snake_case action-oriented names. The pattern is predictable throughout the set.
Five tools is a well-scoped surface for a safe SQL access server. Each tool has a clear role and the count is neither bloated nor overly thin.
The set covers connect, inspect, read, insert, and delete, but notably lacks an update operation, which is a core SQL lifecycle action. Agents would need workarounds such as delete-and-reinsert to modify rows.
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.
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
- FlicenseNot gradedqualityDmaintenanceProvides Claude Desktop with secure access to multiple database connections, allowing users to query MySQL, PostgreSQL, SQLite, and SQL Server databases directly through natural language.-
- AlicenseNot gradedqualityDmaintenanceEnables Claude to connect to and interact with SQLite, SQL Server, PostgreSQL, and MySQL databases through natural language. Supports executing queries, managing tables, exporting data, and storing business insights with authentication options including AWS IAM.MIT
- AlicenseAqualityDmaintenanceEnables LLMs to interact with MySQL databases through standardized protocol, supporting database management, table operations, data queries, and modifications with configurable permission controls.1539 npm1MIT
- AlicenseNot gradedqualityDmaintenanceEnables AI assistants to securely interact with MySQL databases for schema discovery, data querying, and record management with configurable access controls. It provides specialized tools for listing tables, describing structures, and performing CRUD operations within environments like Claude and VS Code.MIT