Skip to main content
Glama

MySQL MCP Server

by caicongyang

MySQL MCP 服务器

English |中文

概述

模型上下文协议 (MCP) 服务器实现,通过 MySQL 提供数据库交互功能。该服务器支持运行 SQL 查询、创建表以及探索数据库架构信息。

成分

工具

该服务器提供五种核心工具:

查询工具
  • read_query
    • 执行 SELECT 查询以从数据库读取数据
    • 输入:
      * query (字符串):要执行的 SELECT SQL 查询
    • 返回:查询结果作为对象数组
  • write_query
    • 执行 INSERT、UPDATE 或 DELETE 查询
    • 输入:
      * query (字符串):SQL 修改查询
    • 返回: { affected_rows: number }
  • create_table
    • 在数据库中创建新表
    • 输入:
      * query (字符串):CREATE TABLE SQL 语句
    • 返回:表创建确认
架构工具
  • list_tables
    • 获取数据库中所有表的列表
    • 无需输入
    • 返回:表名称数组
  • describe_table
    • 查看特定表的架构信息
    • 输入:
      * table_name (字符串): 要描述的表的名称
    • 返回:具有名称和类型的列定义数组

安装

先决条件

  • Python 3.10+
  • MySQL 数据库
  • 所需的 Python 包:
    • mcp (模型上下文协议)
    • sqlalchemy
    • pymysql (或其他 MySQL 驱动程序)
    • python-dotenv
    • uvicorn (用于 HTTP 传输)

使用 Conda 进行设置

首先,创建并激活 conda 环境:

# Create environment conda create --name mcp-demo python=3.12 # Activate environment conda activate mcp-demo

然后,安装所需的依赖项:

# Method 1: Using pip pip install "mcp[cli]>=0.1.0" "pymysql>=1.1.0" "sqlalchemy>=2.0.0" "python-dotenv>=1.0.0" "uvicorn>=0.27.0" # Method 2: Using uv uv pip install "mcp[cli]>=0.1.0" "pymysql>=1.1.0" "sqlalchemy>=2.0.0" "python-dotenv>=1.0.0" "uvicorn>=0.27.0"

配置

您可以使用以下方式配置服务器:

环境变量文件(.env)

  1. 复制.env.template文件并将其重命名为.env
cp .env.template .env
  1. 使用您的配置编辑.env文件:
# Database configuration DB_URL=mysql+pymysql://username:password@localhost:3306/dbname

命令行参数

您还可以使用命令行参数覆盖配置:

python src/mysql/server.py --db-url mysql+pymysql://username:password@localhost:3306/dbname

用法

启动服务器

# Using .env file configuration python src/mysql/server.py # Using command line arguments python src/mysql/server.py --db-url mysql+pymysql://username:password@localhost:3306/dbname --transport http

使用 MCP Inspector 进行测试

您可以使用 MCP Inspector 工具测试服务器:

npx @modelcontextprotocol/inspector uv run /Users/caicongyang/IdeaProjects/tom/mcp-demo/src/mysql/server.py

这将启动服务器并允许您以交互方式测试可用的工具。

示例工作流程

  1. 使用 MySQL 数据库连接启动服务器
  2. 使用 MCP 客户端将 AI 模型连接到服务器
  3. 使用list_tables工具查看可用的表
  4. 如果需要,使用create_table创建表
  5. 使用write_query插入数据
  6. 使用read_query查询数据

与 Claude Desktop 一起使用

紫外线

将服务器添加到您的claude_desktop_config.json

"mcpServers": { "mysql": { "command": "uv", "args": [ "--directory", "path_to_mcp_demo", "run", "python", "src/mysql/server.py", "--db-url", "mysql+pymysql://username:password@localhost/dbname" ] } }

Docker

将服务器添加到您的claude_desktop_config.json

"mcpServers": { "mysql": { "command": "docker", "args": [ "run", "--rm", "-i", "-v", "mcp-mysql:/mcp", "mcp/mysql", "--db-url", "mysql+pymysql://username:password@localhost/dbname" ] } }

软件包安装

您还可以使用 pip 安装该包:

# Install in development mode pip install -e . # Run using the installed package mcp-mysql --db-url mysql+pymysql://username:password@localhost/dbname

在 Cursor IDE 中

Cursor是一款 AI 辅助 IDE。您可以将此 MCP 服务器与 Cursor 集成,以便在编码过程中直接查询 MySQL 数据库。

在光标中设置

  1. 启动 MCP 服务器
    python src/mysql/server.py
  2. 在光标设置中配置 MCP添加您的 MCP 服务器 URL:
    http://localhost:8000
  3. 使用光标命令访问 MCP在游标编辑器中,使用:
    /mcp mysql-query {"query": "SELECT * FROM users LIMIT 5"}
    对于参数化查询:
    /mcp mysql-query {"query": "SELECT * FROM users WHERE age > :min_age", "params": {"min_age": 30}}

API 参考

输入格式

{ "query": "SELECT * FROM users WHERE age > :min_age", "params": { "min_age": 30 } }

输出格式

{ "results": [ {"id": 1, "name": "John", "age": 35}, {"id": 2, "name": "Jane", "age": 42} ], "columns": ["id", "name", "age"], "rowcount": 2 }

安全注意事项

  • 该服务器应在受信任的环境中运行,因为它允许任意 SQL 查询
  • 在生产中,实施适当的访问控制和输入验证
  • 考虑限制可以执行的 SQL 命令类型
  • 重要提示:不要将包含敏感信息的.env文件提交到版本控制

发展

项目结构

  • src/mysql/server.py :主服务器实现
  • pyproject.toml :包配置
  • README.md :本文档

添加新功能

要使用新功能扩展服务器:

  1. 使用@mcp.tool()装饰器添加新工具
  2. 使用MySQLDatabase类实现工具逻辑
  3. 更新文档以反映新功能

执照

此 MCP 服务器遵循 MIT 许可证。这意味着您可以自由使用、修改和分发该软件,但须遵守 MIT 许可证的条款和条件。

Related MCP Servers

  • -
    security
    -
    license
    -
    quality
    A Model Context Protocol server that enables AI models to interact with MySQL databases, providing tools for querying, executing statements, listing tables, and describing table structures.
    Last updated -
    MIT License
  • -
    security
    -
    license
    -
    quality
    A Model Context Protocol server that enables AI models to interact with MySQL databases through a standardized interface, providing tools for querying, executing commands, and managing database schemas.
    Last updated -
    JavaScript
  • A
    security
    A
    license
    A
    quality
    A Model Context Protocol server that enables AI models to interact with both MySQL and MongoDB databases through a standardized interface, supporting comprehensive database operations including queries, schema management, and CRUD operations.
    Last updated -
    14
    6
    JavaScript
    MIT License
  • A
    security
    F
    license
    A
    quality
    A Model Context Protocol server that allows AI agents to execute SQL queries against a MySQL database, supporting operations like reading data, creating tables, inserting, updating, and deleting records.
    Last updated -
    6
    419
    1
    JavaScript
    • Apple

View all related MCP servers

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/caicongyang/mcp-demo'

If you have feedback or need assistance with the MCP directory API, please join our Discord server