Skip to content
Back to search

mysql-mcp-server-sse

MySQL query server based on the MCP sse.Multi-level SQL risk control & injection protection Docker support for quick deployment

Safety scan

A static text scan, not a guarantee. Select the badge to see what was scanned and every finding.

Checked today
Badge for your README
findsafeskills safety rating

Markdown

[![findsafeskills safety rating](/api/listings/mangooer--mysql-mcp-server-sse/badge.svg)](/listings/mangooer--mysql-mcp-server-sse)

HTML

<a href="/listings/mangooer--mysql-mcp-server-sse"><img src="/api/listings/mangooer--mysql-mcp-server-sse/badge.svg" alt="findsafeskills safety rating" /></a>

Install

i

Install instructions from the README

As written by the repo's author. findsafeskills has not verified these steps.

Docker 方式 / Docker Method

# 拉取镜像
docker pull mangooer/mysql-mcp-server-sse:latest

# 运行容器
docker run -d \
  --name mysql-mcp-server-sse \
  -e HOST=0.0.0.0 \
  -e PORT=3000 \
  -e MYSQL_HOST=your_mysql_host \
  -e MYSQL_PORT=3306 \
  -e MYSQL_USER=your_mysql_user \
  -e MYSQL_PASSWORD=your_mysql_password \
  -e MYSQL_DATABASE=your_database \
  -p 3000:3000 \
  mangooer/mysql-mcp-server-sse:latest

Windows PowerShell 格式:

docker run -d `
  --name mysql-mcp-server-sse `
  -e HOST=0.0.0.0 `
  -e PORT=3000 `
  -e MYSQL_HOST=your_mysql_host `
  -e MYSQL_PORT=3306 `
  -e MYSQL_USER=your_mysql_user `
  -e MYSQL_PASSWORD=your_mysql_password `
  -e MYSQL_DATABASE=your_database `
  -p 3000:3000 `
  mangooer/mysql-mcp-server-sse:latest

源码方式 / Source Code Method

安装依赖 / Install Dependencies

pip install -r requirements.txt

配置环境变量 / Configure Environment Variables

复制`.env.example`为`.env`,并根据实际情况修改。 Copy `.env.example` to `.env` and modify as needed.

启动服务 / Start the Server

python -m src.server

默认监听:http://127.0.0.1:3000/sse Default endpoint: http://127.0.0.1:3000/sse

---

What was scanned

The files the safety scan read, as written by the repository's author and shown as plain text. Lines with a finding are marked.

README.md · README · 344 lines

View this file on GitHub (read at the repository's current version: the scan did not record which commit it read)

  1. # MySQL查询服务器 / MySQL Query Server
  2. ---
  3. ## 1. 项目简介 / Project Introduction
  4. 本项目是基于MCP框架的MySQL查询服务器,支持通过SSE协议进行实时数据库操作,具备完善的安全、日志、配置和敏感信息保护机制,适用于开发、测试和生产环境下的安全MySQL数据访问。
  5. This project is a MySQL query server based on the MCP framework, supporting real-time database operations via SSE protocol. It features comprehensive security, logging, configuration, and sensitive information protection mechanisms, suitable for secure MySQL data access in development, testing, and production environments.
  6. ---
  7. ## 2. 主要特性 / Key Features
  8. - 基于FastMCP框架,异步高性能
  9. - 支持高并发的数据库连接池,参数灵活可调
  10. - 支持SSE实时推送
  11. - 丰富的MySQL元数据与结构查询API
  12. - 自动事务管理与回滚
  13. - 多级SQL风险控制与注入防护
  14. - **数据库隔离安全**:防止跨数据库访问,支持三级访问控制
  15. - 敏感信息自动隐藏与自定义
  16. - 灵活的环境变量配置
  17. - 完善的日志与错误处理
  18. - Docker支持,快速部署
  19. - Built on FastMCP framework, high-performance async
  20. - Connection pool for high concurrency, with flexible parameter tuning
  21. - SSE real-time push support
  22. - Rich MySQL metadata & schema query APIs
  23. - Automatic transaction management & rollback
  24. - Multi-level SQL risk control & injection protection
  25. - **Database Isolation Security**: Prevents cross-database access with 3-level access control
  26. - Automatic and customizable sensitive info masking
  27. - Flexible environment variable configuration
  28. - Robust logging & error handling
  29. - Docker support for quick deployment
  30. ---
  31. ## 3. 快速开始 / Quick Start
  32. ### Docker 方式 / Docker Method
  33. ```bash
  34. # 拉取镜像
  35. docker pull mangooer/mysql-mcp-server-sse:latest
  36. # 运行容器
  37. docker run -d \
  38. --name mysql-mcp-server-sse \
  39. -e HOST=0.0.0.0 \
  40. -e PORT=3000 \
  41. -e MYSQL_HOST=your_mysql_host \
  42. -e MYSQL_PORT=3306 \
  43. -e MYSQL_USER=your_mysql_user \
  44. -e MYSQL_PASSWORD=your_mysql_password \
  45. -e MYSQL_DATABASE=your_database \
  46. -p 3000:3000 \
  47. mangooer/mysql-mcp-server-sse:latest
  48. ```
  49. Windows PowerShell 格式:
  50. ```powershell
  51. docker run -d `
  52. --name mysql-mcp-server-sse `
  53. -e HOST=0.0.0.0 `
  54. -e PORT=3000 `
  55. -e MYSQL_HOST=your_mysql_host `
  56. -e MYSQL_PORT=3306 `
  57. -e MYSQL_USER=your_mysql_user `
  58. -e MYSQL_PASSWORD=your_mysql_password `
  59. -e MYSQL_DATABASE=your_database `
  60. -p 3000:3000 `
  61. mangooer/mysql-mcp-server-sse:latest
  62. ```
  63. ### 源码方式 / Source Code Method
  64. #### 安装依赖 / Install Dependencies
  65. ```bash
  66. pip install -r requirements.txt
  67. ```
  68. #### 配置环境变量 / Configure Environment Variables
  69. 复制`.env.example`为`.env`,并根据实际情况修改。
  70. Copy `.env.example` to `.env` and modify as needed.
  71. #### 启动服务 / Start the Server
  72. ```bash
  73. python -m src.server
  74. ```
  75. 默认监听:http://127.0.0.1:3000/sse
  76. Default endpoint: http://127.0.0.1:3000/sse
  77. ---
  78. ## 4. 目录结构 / Project Structure
  79. ```
  80. .
  81. ├── src/
  82. │ ├── server.py # 主服务器入口 / Main server entry
  83. │ ├── config.py # 配置项定义 / Config definitions
  84. │ ├── validators.py # 参数校验 / Parameter validation
  85. │ ├── db/
  86. │ │ └── mysql_operations.py # 数据库操作 / DB operations
  87. │ ├── security/
  88. │ │ ├── interceptor.py # SQL拦截 / SQL interception
  89. │ │ ├── query_limiter.py # 风险控制 / Risk control
  90. │ │ └── sql_analyzer.py # SQL分析 / SQL analysis
  91. │ └── tools/
  92. │ ├── mysql_tool.py # 基础查询 / Basic query
  93. │ ├── mysql_metadata_tool.py # 元数据查询 / Metadata query
  94. │ ├── mysql_info_tool.py # 信息查询 / Info query
  95. │ ├── mysql_schema_tool.py # 结构查询 / Schema query
  96. │ └── metadata_base_tool.py # 工具基类 / Tool base class
  97. ├── tests/ # 测试 / Tests
  98. ├── .env.example # 环境变量示例 / Env example
  99. └── requirements.txt # 依赖 / Requirements
  100. ```
  101. ---
  102. ## 5. 环境变量与配置 / Environment Variables & Configuration
  103. | 变量名 / Variable | 说明 / Description | 默认值 / Default |
  104. |--------------------------|------------------------------------------------------|------------------|
  105. | HOST | 服务器监听地址 / Server listen address | 127.0.0.1 |
  106. | PORT | 服务器监听端口 / Server listen port | 3000 |
  107. | MYSQL_HOST | MySQL服务器地址 / MySQL server host | localhost |
  108. | MYSQL_PORT | MySQL服务器端口 / MySQL server port | 3306 |
  109. | MYSQL_USER | MySQL用户名 / MySQL username | root |
  110. | MYSQL_PASSWORD | MySQL密码 / MySQL password | (空/empty) |
  111. | MYSQL_DATABASE | 要连接的数据库名 / Database name | (空/empty) |
  112. | DB_CONNECTION_TIMEOUT | 连接超时时间(秒) / Connection timeout (seconds) | 5 |
  113. | DB_AUTH_PLUGIN | 认证插件类型 / Auth plugin type | mysql_native_password |
  114. | DB_POOL_ENABLED | 是否启用连接池 / Enable connection pool (true/false) | true |
  115. | DB_POOL_MIN_SIZE | 连接池最小连接数 / Pool min size | 5 |
  116. | DB_POOL_MAX_SIZE | 连接池最大连接数 / Pool max size | 20 |
  117. | DB_POOL_RECYCLE | 连接回收时间(秒) / Pool recycle time (seconds) | 300 |
  118. | DB_POOL_MAX_LIFETIME | 连接最大存活时间(秒, 0=不限制) / Max lifetime (sec) | 0 |
  119. | DB_POOL_ACQUIRE_TIMEOUT | 获取连接超时时间(秒) / Acquire timeout (seconds) | 10.0 |
  120. | ENV_TYPE | 环境类型(development/production) / Env type | development |
  121. | ALLOWED_RISK_LEVELS | 允许的风险等级(逗号分隔) / Allowed risk levels | LOW,MEDIUM |
  122. | ALLOW_SENSITIVE_INFO | 允许查询敏感字段 / Allow sensitive info (true/false) | false |
  123. | SENSITIVE_INFO_FIELDS | 自定义敏感字段模式(逗号分隔) / Custom sensitive fields | (空/empty) |
  124. | MAX_SQL_LENGTH | 最大SQL语句长度 / Max SQL length | 5000 |
  125. | BLOCKED_PATTERNS | 阻止的SQL模式(逗号分隔) / Blocked SQL patterns | (空/empty) |
  126. | ENABLE_QUERY_CHECK | 启用查询安全检查 / Enable query check (true/false) | true |
  127. | **ENABLE_DATABASE_ISOLATION** | **启用数据库隔离 / Enable database isolation (true/false)** | **false** |
  128. | **DATABASE_ACCESS_LEVEL** | **数据库访问级别 / Database access level (strict/restricted/permissive)** | **permissive** |
  129. | LOG_LEVEL | 日志级别(DEBUG/INFO/...) / Log level | DEBUG |
  130. > 注/Note: 部分云MySQL需指定`DB_AUTH_PLUGIN`为`mysql_native_password`。
  131. ### MySQL 8.0 认证支持 / MySQL 8.0 Authentication Support
  132. 本系统完全支持 MySQL 8.0 的认证机制。MySQL 8.0 默认使用 `caching_sha2_password` 认证插件,提供更高的安全性。
  133. This system fully supports MySQL 8.0 authentication mechanisms. MySQL 8.0 uses `caching_sha2_password` by default for enhanced security.
  134. #### 认证插件对比 / Authentication Plugin Comparison
  135. | 认证插件 / Plugin | 安全性 / Security | 兼容性 / Compatibility | 依赖要求 / Dependencies |
  136. |------------------|-------------------|------------------------|------------------------|
  137. | `mysql_native_password` | 中等 / Medium | 高 / High | 无 / None |
  138. | `caching_sha2_password` | 高 / High | 中等 / Medium | cryptography |
  139. #### 配置建议 / Configuration Recommendations
  140. **生产环境 / Production**(推荐 / Recommended):
  141. ```ini
  142. DB_AUTH_PLUGIN=caching_sha2_password
  143. ```
  144. **开发环境 / Development**(简化配置 / Simplified):
  145. ```ini
  146. DB_AUTH_PLUGIN=mysql_native_password
  147. ```
  148. #### 依赖安装 / Dependency Installation
  149. 使用 `caching_sha2_password` 时需要安装 `cryptography` 包(已包含在 requirements.txt 中):
  150. When using `caching_sha2_password`, the `cryptography` package is required (already included in requirements.txt):
  151. ```bash
  152. pip install cryptography
  153. ```
  154. ### 数据库隔离安全 / Database Isolation Security
  155. 本系统提供强大的数据库隔离功能,防止跨数据库访问,确保数据安全。
  156. This system provides robust database isolation features to prevent cross-database access and ensure data security.
  157. #### 访问级别 / Access Levels
  158. | 级别 / Level | 允许访问 / Allowed Access | 适用场景 / Use Case |
  159. |-------------|---------------------------|-------------------|
  160. | **strict** | 仅指定数据库 / Only specified database | 生产环境 / Production |
  161. | **restricted** | 指定数据库 + 系统库 / Specified + system databases | 开发环境 / Development |
  162. | **permissive** | 所有数据库 / All databases | 测试环境 / Testing |
  163. #### 启用数据库隔离 / Enable Database Isolation
  164. ```bash
  165. # Docker 启用严格模式 / Docker with strict mode
  166. docker run -d \
  167. -e MYSQL_DATABASE=your_database \
  168. -e ENABLE_DATABASE_ISOLATION=true \
  169. -e DATABASE_ACCESS_LEVEL=strict \
  170. mangooer/mysql-mcp-server-sse:latest
  171. # 生产环境自动启用 / Auto-enable in production
  172. docker run -d \
  173. -e ENV_TYPE=production \
  174. -e MYSQL_DATABASE=your_database \
  175. mangooer/mysql-mcp-server-sse:latest
  176. ```
  177. **安全效果 / Security Effects**:
  178. - ✅ 阻止 `SHOW DATABASES` / Blocks `SHOW DATABASES`
  179. - ✅ 阻止 `SELECT * FROM mysql.user` / Blocks `SELECT * FROM mysql.user`
  180. - ✅ 阻止 `SHOW TABLES FROM other_db` / Blocks `SHOW TABLES FROM other_db`
  181. - ✅ 允许当前数据库操作 / Allows current database operations
  182. > 🔒 **重要**:生产环境(`ENV_TYPE=production`)会自动启用数据库隔离,使用 `restricted` 模式。
  183. >
  184. > 🔒 **Important**: Production environment (`ENV_TYPE=production`) automatically enables database isolation with `restricted` mode.
  185. ---
  186. ## 6. 自动化与资源管理优化 / Automation & Resource Management Enhancements
  187. ### 自动化工具注册 / Automated Tool Registration
  188. - 所有MySQL相关API工具均采用自动注册机制:
  189. - 无需手动在主入口维护注册代码,新增/删除工具只需在`src/tools/`目录下实现`register_xxx_tool(s)`函数即可。
  190. - 系统启动时自动扫描并注册,极大提升可维护性和扩展性。
  191. - All MySQL-related API tools are registered automatically:
  192. - No need to manually maintain registration code in the main entry. To add or remove a tool, simply implement a `register_xxx_tool(s)` function in the `src/tools/` directory.
  193. - The system scans and registers tools automatically at startup, greatly improving maintainability and extensibility.
  194. ### 连接池自动回收与资源管理 / Connection Pool Auto-Recycling & Resource Management
  195. - 连接池采用事件循环隔离与自动回收机制:
  196. - 每个事件循环独立池,支持高并发与多环境。
  197. - 定期(默认每5分钟)自动回收无效或失效的连接池,防止资源泄漏。
  198. - 事件循环关闭时自动关闭对应连接池,确保资源彻底释放。
  199. - 支持多数据库/多租户场景扩展。
  200. - 所有资源管理操作均有详细日志,便于追踪和排查。
  201. - The connection pool uses event loop isolation and auto-recycling:
  202. - Each event loop has its own pool, supporting high concurrency and multi-environment deployment.
  203. - Unused or invalid pools are automatically recycled every 5 minutes (by default), preventing resource leaks.
  204. - When an event loop is closed, its pool is automatically closed to ensure complete resource release.
  205. - Ready for multi-database/multi-tenant scenarios.
  206. - All resource management operations are logged in detail for easy tracking and troubleshooting.
  207. ---
  208. ## 7. 安全机制 / Security Mechanisms
  209. - 多级SQL风险等级(LOW/MEDIUM/HIGH/CRITICAL)
  210. - SQL注入与危险操作拦截
  211. - WHERE子句强制检查
  212. - **数据库隔离安全**:三级访问控制(strict/restricted/permissive)
  213. - **跨数据库访问防护**:阻止未授权的数据库访问
  214. - 敏感信息自动隐藏(支持自定义字段)
  215. - 生产环境默认只允许低风险操作
  216. - **生产环境自动启用数据库隔离**
  217. - Multi-level SQL risk levels (LOW/MEDIUM/HIGH/CRITICAL)
  218. - SQL injection & dangerous operation interception
  219. - Mandatory WHERE clause check
  220. - **Database Isolation Security**: 3-level access control (strict/restricted/permissive)
  221. - **Cross-database Access Protection**: Blocks unauthorized database access
  222. - Automatic sensitive info masking (customizable fields)
  223. - Production allows only low-risk operations by default
  224. - **Auto-enable database isolation in production**
  225. ---
  226. ## 8. 日志与错误处理 / Logging & Error Handling
  227. - 日志级别可配置(LOG_LEVEL)
  228. - 控制台与文件日志输出
  229. - 详细记录运行状态与错误
  230. - 完善的异常捕获与事务回滚
  231. - Configurable log level (LOG_LEVEL)
  232. - Console & file log output
  233. - Detailed running status & error logs
  234. - Robust exception capture & transaction rollback
  235. ---
  236. ## 9. 常见问题 / FAQ
  237. ### Q: DELETE操作未执行成功?
  238. A: 检查是否有WHERE条件,无WHERE为高风险,需在ALLOWED_RISK_LEVELS中允许CRITICAL。
  239. Q: Why does DELETE not work?
  240. A: Check for WHERE clause. DELETE without WHERE is high risk (CRITICAL), must be allowed in ALLOWED_RISK_LEVELS.
  241. ### Q: 如何自定义敏感字段?
  242. A: 设置SENSITIVE_INFO_FIELDS,如SENSITIVE_INFO_FIELDS=password,token
  243. Q: How to customize sensitive fields?
  244. A: Set SENSITIVE_INFO_FIELDS, e.g. SENSITIVE_INFO_FIELDS=password,token
  245. ### Q: 如何启用数据库隔离?
  246. A: 设置ENABLE_DATABASE_ISOLATION=true和DATABASE_ACCESS_LEVEL=strict,或使用ENV_TYPE=production自动启用。
  247. Q: How to enable database isolation?
  248. A: Set ENABLE_DATABASE_ISOLATION=true and DATABASE_ACCESS_LEVEL=strict, or use ENV_TYPE=production for auto-enable.
  249. ### Q: 数据库隔离后无法查询系统表?
  250. A: strict模式禁止系统表访问,可改为restricted模式,或检查是否确实需要系统表访问权限。
  251. Q: Cannot query system tables after enabling database isolation?
  252. A: strict mode blocks system table access. Use restricted mode or verify if system table access is actually needed.
  253. ### Q: limit参数报错?
  254. A: limit必须为非负整数。
  255. Q: limit parameter error?
  256. A: limit must be a non-negative integer.
  257. ---
  258. ## 10. 贡献指南 / Contribution Guide
  259. 欢迎通过Issue和Pull Request参与改进。
  260. Contributions via Issue and Pull Request are welcome.
  261. ---
  262. ## 11. 许可证 / License
  263. MIT License
  264. 本软件按"原样"提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、特定用途的适用性和非侵权性的保证。在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是在合同诉讼、侵权行为还是其他方面,产生于、源于或与本软件有关,或与本软件的使用或其他交易有关。
  265. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Ratings and comments

Sign in to rate this skill.

Sign in

Free to use. If it’s helped, pass it on.