MySQL MCP Server

MIT License
20
  • Linux
  • Apple

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

Integrations

  • Provides environment variable management for configuring database credentials and server settings through .env files.

  • Supports containerized deployment of the MCP server through Docker, allowing configuration of database connection parameters and port mappings.

  • Includes Mermaid diagram support for visualizing the server architecture and data flow between components.

mysql-mcp-服务器

韩语 README.md

0.执行

使用 Docker 运行

根据需要更改数据库连接信息。

docker run -d --name mcp-mysql \ -e MYSQL_HOST=localhost \ -e MYSQL_PORT=3306 \ -e MYSQL_USER=root \ -e MYSQL_PASSWORD=mcpTest1234!!! \ -e MYSQL_DATABASE=mcp_test \ -e MCP_PORT=8081 \ -p 3306:3306 mineru/mcp-mysql:1.0.0

使用 Docker Compose 运行

这将按照预先配置的设置进行。

docker-compose up -d

直接用Python运行

pip install -r requirements.txt python mysql_mcp_server/main.py run

游标配置

Cursor 0.46 及以上版本提供 MCP 功能。

此外,只有 Cursor Pro 帐户用户可以访问 MCP 功能。

工具添加技巧

  • 添加工具
    • execute函数实现实际的逻辑执行(服务层)。
    • @tool装饰器帮助将工具注册到 MCP(控制器层)。
  • 解释
    • mysql_mcp_server/executors下的每个文件代表一个工具。
    • 如果添加了新工具,则必须在mysql_mcp_server/executors/__init__.py中导入并包含在__all__数组中。
    • 这可确保模块自动在TOOLS_DEFINITION变量中注册。

🚧 发展路线图🚧

  • ⚙️ 参数选项
    • [ ] 🔧 为每个工具启用/禁用开关:提供减少输入上下文成本的功能💰
    • [ ] 🔒 查询安全级别设置:提供对可能损害资产价值的功能的可选控制,例如 DROP、DELETE、UPDATE 🚫
  • ✨ 特点
    • [x] 📊 数据分析报告生成:提供针对模型优化的报告生成功能,根据用户请求适当选择各种图表📈
      • [x] 📝 规定表格的报告功能
      • [ ] 🖌️ 多样化报告模板
    • [ ] 🗄️ 扩展 Text2SQL 支持
    • [ ] 🌐 SSH 连接支持:通过 SSH 启用安全远程访问以进行高级操作 🔑
    • [ ] 📥 文件提取功能
      • [ ] 📄 CSV
      • [ ] 📑 JSON
      • [ ] 📉 Excel

1.概述

MCP MySQL Server 是一个基于 MCP(模型上下文协议)的 MySQL 数据库操作服务器应用程序。该服务器提供允许 AI 模型与 MySQL 数据库交互的工具。

2.系统配置

2.1 关键组件

  • MCP 服务器:与 AI 模型通信的 FastMCP 服务器
  • MySQL 数据库:管理和存储数据
  • 工具:执行数据库操作的执行器

2.2 技术栈

  • 语言:Python
  • 数据库:MySQL 8.0
  • 主要库
    • mcp:实现用于 AI 通信的模型上下文协议
    • PyMySQL:连接到 MySQL 并执行查询
    • pandas:处理和分析数据
    • python-dotenv:管理环境变量
    • fire:实现命令行界面

2.3 部署环境

  • 通过 Docker 和 Docker Compose 进行容器化部署
  • 端口:8081(MCP 服务器)、3306(MySQL)

3.目录结构

MCPBoilerPlate/ ├── mysql_mcp_server/ # Main application directory │ ├── executors/ # Database operation executors │ │ ├── create_table.py # Tool for creating tables │ │ ├── desc_table.py # Tool for viewing table schema │ │ ├── explain.py # Tool for query execution plans │ │ ├── insert_query.py # Tool for INSERT query execution │ │ ├── insight_starter.py # Schema verification tools for write reports │ │ ├── invoke_viz_pro.py # Tool for Visualization chart recommendation │ │ ├── select_query.py # Tool for SELECT query execution │ │ └── show_tables.py # Tool for retrieving table lists │ ├── helper/ # Utility modules │ │ ├── db_conn_helper.py # Manages database connections │ │ ├── logger_helper.py # Logging utilities │ │ └── tool_decorator.py # Tool decorator │ └── main.py # Application entry point ├── docker-compose.yml # Docker Compose configuration ├── Dockerfile # Docker image build settings ├── requirements.txt # Dependency package list └── .env.example # Example environment variables file

4.架构设计

4.1 分层结构

  1. 接口层:MCP 服务器(FastMCP)
  2. 业务逻辑层:处理程序和执行程序
  3. 数据访问层:数据库连接和查询执行

4.2 关键类和模块

  • MySQLMCPServer :初始化并运行 MCP 服务器的主服务器类
  • DatabaseManager :基于单例模式的数据库连接管理器
  • 执行器:数据库操作工具集合
    • 执行_create_table:创建表
    • 执行_desc_table:检查表模式
    • 执行解释:提供查询执行计划
    • 执行 INSETR 查询
    • 执行 SELECT 查询
    • 执行_show_tables:检索表列表

4.3 通信流程

  1. AI 模型向 MCP 服务器请求可用工具列表。
  2. 服务器返回可用的工具列表。
  3. AI模型请求执行特定的工具。
  4. 服务端调用相应的执行器执行数据库操作。
  5. 执行结果返回给AI模型。

5.可扩展性和维护

  • 添加工具:在executors目录中实现新工具,并在__init__.py中注册它们。
  • 环境配置:通过.env文件管理环境变量。
  • 日志记录:使用logger_helper确保日志记录的一致性。

6.部署和执行

6.1 本地执行

# Setup environment cp .env.example .env # Modify .env file as needed # Install dependencies pip install -r requirements.txt # Run the server python mysql_mcp_server/main.py run

6.2 Docker部署

# Start database using Docker Compose docker-compose up -d db # Build and run mysql-mcp-server with Docker Compose (including rebuilds) docker-compose up -d --build mysql-mcp-server

7.安全考虑

  • 通过环境变量管理数据库凭证。
  • 在生产环境中使用强密码。
  • 必要时考虑对数据库连接实施 SSL/TLS 加密。
ID: 6y836dz8o5