MySQL MCP Server

MySQL MCP Server

Provides MySQL database integration for AI assistants and other MCP clients, allowing them to list tables, read table data, and execute SQL queries.

Category
访问服务器

README

MySQL MCP Server

A Model Context Protocol (MCP) server that provides MySQL database integration for AI assistants and other MCP clients.

Features

  • Database Resource Management: List and read MySQL tables as MCP resources
  • SQL Query Execution: Execute arbitrary SQL queries through MCP tools
  • Environment-based Configuration: Flexible database connection setup
  • UTF-8 Support: Full Unicode support with proper charset handling
  • Error Handling: Comprehensive error logging and exception management

Installation

Prerequisites

  • Python 3.12 or higher
  • MySQL server (5.7+ or 8.0+)
  • Access to a MySQL database

Setup

  1. Clone the repository:
git clone https://github.com/slg1119/mysql-mcp.git
cd mysql-mcp
  1. Install dependencies using uv (recommended):
uv sync

Or with pip:

pip install -e .

Configuration

Configure your MySQL connection using environment variables:

export MYSQL_HOST=localhost          # Default: localhost
export MYSQL_PORT=3306              # Default: 3306
export MYSQL_USER=your_username      # Required
export MYSQL_PASSWORD=your_password  # Required
export MYSQL_DATABASE=your_database  # Required
export MYSQL_CHARSET=utf8mb4        # Default: utf8mb4
export MYSQL_COLLATION=utf8mb4_unicode_ci  # Default: utf8mb4_unicode_ci
export MYSQL_SQL_MODE=TRADITIONAL    # Default: TRADITIONAL

Required Environment Variables

  • MYSQL_USER: MySQL username
  • MYSQL_PASSWORD: MySQL password
  • MYSQL_DATABASE: Target database name

Optional Environment Variables

  • MYSQL_HOST: MySQL server host (default: localhost)
  • MYSQL_PORT: MySQL server port (default: 3306)
  • MYSQL_CHARSET: Character set (default: utf8mb4)
  • MYSQL_COLLATION: Collation (default: utf8mb4_unicode_ci)
  • MYSQL_SQL_MODE: SQL mode (default: TRADITIONAL)

Usage

Running the Server

Start the MCP server:

python server.py

The server will run using stdio transport and wait for MCP client connections.

Available Resources

  • mysql://tables - Lists all available tables in the database
  • mysql://{table_name} - Returns the first 100 rows of the specified table

Available Tools

  • execute_sql(query: str) - Executes SQL queries and returns formatted results

MCP Client Integration

To use this server with an MCP client, configure it in your client's settings:

{
  "mcpServers": {
    "mysql": {
      "command": "python",
      "args": ["/path/to/mysql-mcp/server.py"],
      "env": {
        "MYSQL_HOST": "localhost",
        "MYSQL_USER": "your_username",
        "MYSQL_PASSWORD": "your_password",
        "MYSQL_DATABASE": "your_database"
      }
    }
  }
}

Examples

Reading Table Data

# List all tables
tables = await client.list_resources("mysql://tables")

# Read specific table
table_data = await client.read_resource("mysql://users")

Executing SQL Queries

# Select query
result = await client.call_tool("execute_sql", {
    "query": "SELECT * FROM users WHERE active = 1 LIMIT 10"
})

# Insert query
result = await client.call_tool("execute_sql", {
    "query": "INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')"
})

Security Considerations

  • Store database credentials securely using environment variables
  • Use database users with minimal required privileges
  • Validate and sanitize SQL inputs in production environments
  • Consider using connection pooling for high-traffic scenarios

Dependencies

  • mcp[cli]: Model Context Protocol implementation
  • pymysql: Pure Python MySQL client library

Development

Project Structure

mysql-mcp/
├── server.py          # Main MCP server implementation
├── pyproject.toml     # Project configuration and dependencies
├── README.md          # This file
├── .python-version    # Python version specification
├── .gitignore         # Git ignore rules
└── uv.lock           # Dependency lock file

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

Troubleshooting

Common Issues

Connection Failed: Verify your MySQL server is running and credentials are correct.

Charset Issues: Ensure your MySQL server supports utf8mb4 charset. For older MySQL versions, you may need to adjust MYSQL_CHARSET and MYSQL_COLLATION.

Permission Denied: Check that your MySQL user has the necessary privileges for the target database.

Logging

The server provides detailed logging. Check the console output for connection status and error messages.

License

This project is available under the MIT License. See the LICENSE file for more details.

Support

For issues and questions, please open an issue on the GitHub repository.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选