SQL MCP Server
Provides tools for managing MySQL databases, including listing tables, getting schemas, and executing SQL queries.
Provides tools for managing PostgreSQL databases, including listing tables, getting schemas, and executing SQL queries.
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., "@SQL MCP Serverlist tables in prod-mysql"
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
动态 SQL MCP 服务,支持 MySQL 与 PostgreSQL。所有数据库连接在运行时通过 MCP tool 参数传入,无需配置文件;同时支持 stdio(适合 Claude Desktop 等本地客户端)和 sse(适合 Docker 远程部署)两种 transport。
特性
支持 MySQL 8 与 PostgreSQL 16
支持
stdio/sse双 transport运行时动态传入数据库连接(
connection对象)可选的
alias注册/释放机制,便于复用连接默认只读执行;通过
allow_write: true显式开启写入连接池自动缓存与回收
提供 5 个 MCP tools:
register_databaseunregister_databaselist_db_serverslist_database_tablesget_table_schemaquery_database
Related MCP server: mysql-mcp-zag
目录
快速开始
本地运行(SSE)
pip install -r requirements.txt
python -m src.server --transport sse --host 127.0.0.1 --port 3000Docker 运行
docker build -t sql-mcp .
docker run -p 3000:3000 -e SQL_MCP_HOST=0.0.0.0 -e LOG_LEVEL=INFO sql-mcpDocker Compose 运行
docker compose up -d服务默认监听 http://0.0.0.0:3000,SSE endpoint 为 http://localhost:3000/sse。
安装
# 使用 requirements.txt
pip install -r requirements.txt
# 或作为可编辑包安装
pip install -e .需要 Python >= 3.11。
运行方式
stdio(本地 MCP 客户端,如 Claude Desktop)
python -m src.server --transport stdioSSE(远程 / Docker)
python -m src.server --transport sse --host 0.0.0.0 --port 3000命令行参数也可通过环境变量设置:
参数 | 环境变量 | 默认值 | 说明 |
|
|
| SSE 监听地址 |
|
|
| SSE 监听端口 |
- |
|
| 日志级别(DEBUG/INFO/WARNING/ERROR) |
MCP Tools
register_database
注册一个数据库连接别名,后续可通过 alias 复用该连接。注册同名别名会覆盖旧连接,并自动释放旧连接池。
参数
参数 | 类型 | 必填 | 说明 |
|
| 是 | 别名 |
|
| 是 | 数据库连接对象 |
示例
{
"alias": "prod-mysql",
"connection": {
"type": "mysql",
"host": "localhost",
"port": 3306,
"user": "root",
"password": "secret",
"database": "mydb"
}
}返回
{ "status": "ok", "alias": "prod-mysql", "type": "mysql" }unregister_database
释放一个已注册的别名,并关闭对应的连接池。
参数
参数 | 类型 | 必填 | 说明 |
|
| 是 | 要释放的别名 |
示例
{ "alias": "prod-mysql" }返回
{ "status": "ok", "alias": "prod-mysql" }若别名不存在:
{ "status": "not_found", "alias": "prod-mysql" }list_db_servers
列出所有已注册的连接元信息(不含密码)。
返回
[
{
"alias": "prod-mysql",
"type": "mysql",
"host": "localhost",
"port": 3306,
"database": "mydb",
"ssl": false
}
]list_database_tables
列出数据库中的所有表。支持通过 alias 或完整 connection 对象定位数据库,二者只能选其一。
参数
参数 | 类型 | 必填 | 说明 |
|
| 二选一 | 已注册的别名 |
|
| 二选一 | 完整连接对象 |
示例
{ "alias": "prod-mysql" }返回
{ "tables": ["users", "orders"] }get_table_schema
获取指定表的结构信息,包括列、主键约束、索引。
参数
参数 | 类型 | 必填 | 说明 |
|
| 是 | 表名 |
|
| 二选一 | 已注册的别名 |
|
| 二选一 | 完整连接对象 |
示例
{
"alias": "prod-mysql",
"table_name": "users"
}返回
{
"columns": [
{ "name": "id", "type": "INTEGER", "nullable": false, "default": null },
{ "name": "name", "type": "VARCHAR", "nullable": true, "default": null }
],
"primary_key": { "name": "pk_users", "constrained_columns": ["id"] },
"indexes": [...]
}query_database
执行 SQL 查询。默认处于只读模式,禁止 INSERT/UPDATE/DELETE/DROP 等写入操作;传入 allow_write: true 可显式开启写入。
参数
参数 | 类型 | 必填 | 说明 |
|
| 是 | SQL 语句 |
|
| 二选一 | 已注册的别名 |
|
| 二选一 | 完整连接对象 |
|
| 否 | SQL 参数(命名参数) |
|
| 否 | 只读查询默认最大返回行数,默认 |
|
| 否 | 是否允许写入,默认 |
只读查询示例
{
"alias": "prod-mysql",
"sql": "SELECT * FROM users WHERE name = :name",
"params": { "name": "Alice" },
"limit": 100
}写入示例
{
"alias": "prod-mysql",
"sql": "INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com')",
"allow_write": true
}返回
查询语句:结果数组
写入语句:
{ "rows_affected": 1 }
连接对象 DatabaseConnection
字段 | 类型 | 必填 | 说明 |
|
| 是 | 数据库类型: |
|
| 是 | 数据库主机 |
|
| 否 | 端口;MySQL 默认 |
|
| 是 | 用户名 |
|
| 是 | 密码 |
|
| 是 | 数据库名 |
|
| 否 | 是否启用 SSL,默认 |
环境变量
变量 | 默认值 | 说明 |
|
| SSE 监听地址 |
|
| SSE 监听端口 |
|
| 日志级别 |
| - | 建议设为 |
开发与测试
项目使用 pytest 做单元测试,使用 scripts/agents/ 下的 Agent 脚本做集成测试。
启动本地测试数据库与 MCP 服务
bash scripts/start-test-infra.sh该脚本会启动 PostgreSQL、MySQL 两个 Docker 容器,并在 localhost:3000 启动 SSE 模式的 MCP 服务。
运行单元测试
python -m pytest tests/test_basic.py -v运行集成测试
python -m pytest tests/integration/test_mcp_server.py -v运行 Agent 测试
python scripts/agents/agent_postgres.py
python scripts/agents/agent_mysql.py
python scripts/agents/agent_security.py
python scripts/agents/agent_direct_connection.py
python scripts/agents/agent_extensibility.py停止测试环境
bash scripts/stop-test-infra.sh安全提示
当前版本通过 MCP tool 参数明文传入数据库凭据,请求会经过 MCP 客户端、网络传输(SSE)以及服务端日志。该方案适用于:
受控内网环境
本地开发/测试
快速原型验证
生产环境建议:
启用 SSE TLS(如通过反向代理提供 HTTPS)
限制可访问的源 IP
由服务端通过环境变量/密钥管理系统加载凭据模板,客户端 tool 参数只传
alias开启审计日志并避免在日志中记录
password与原始 SQL 参数
只读守卫基于 SQL 前缀与关键字做最佳努力检测,不能替代数据库级权限控制。建议为 MCP 服务单独配置权限受限的数据库账号。
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.
Related MCP Servers
- AlicenseAqualityCmaintenanceEnables connecting to and querying multiple database types (PostgreSQL, MySQL, SQLite) through a unified interface. Supports managing multiple concurrent database connections with connection pooling and SQL query execution through MCP tools.Last updated535MIT
- Alicense-qualityCmaintenanceEnables SQL query execution and database structure browsing via MCP tools and resources.Last updatedMIT
- Alicense-qualityDmaintenanceMCP server for connecting to databases (PostgreSQL, MySQL, SQL Server, Redis) enabling SQL queries, table exploration, and Redis key-value operations.Last updated1MIT
- AlicenseAqualityCmaintenanceA lightweight MCP server for relational databases, enabling dynamic connections to PostgreSQL and MySQL, SQL execution, and transaction control.Last updated7541MIT
Related MCP Connectors
MCP server for managing Prisma Postgres.
Analytical memory for AI agents: a real Postgres queried in plain English over MCP. One command.
MCP server for interacting with the Supabase platform
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
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/imShire/sql-mcp-server'
If you have feedback or need assistance with the MCP directory API, please join our Discord server