Provides integration with MySQL databases, allowing AI assistants to list and read tables as resources, execute arbitrary SQL queries, and manage database connections with proper UTF-8 support.
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
- Clone the repository:
- Install dependencies using uv (recommended):
Or with pip:
Configuration
Configure your MySQL connection using environment variables:
Required Environment Variables
MYSQL_USER
: MySQL usernameMYSQL_PASSWORD
: MySQL passwordMYSQL_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:
The server will run using stdio transport and wait for MCP client connections.
Available Resources
mysql://tables
- Lists all available tables in the databasemysql://{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:
Examples
Reading Table Data
Executing SQL Queries
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
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- 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.
This server cannot be installed
hybrid server
The server is able to function both locally and remotely, depending on the configuration or use case.
Provides MySQL database integration for AI assistants and other MCP clients, allowing them to list tables, read table data, and execute SQL queries.
Related MCP Servers
- -securityFlicense-qualityFacilitates interaction with a local MySQL database via a RESTful API, supporting database queries and real-time updates with integration for Cursor MCP services.Last updated -4JavaScript
- -securityAlicense-qualityA Model Context Protocol (MCP) server that enables AI assistants to interact with MySQL databases by executing SQL queries and checking database connectivity.Last updated -TypeScriptMIT License
- -securityFlicense-qualityEnables secure interaction with MySQL databases, allowing AI assistants to list tables, read data, and execute SQL queries through a controlled interface.Last updated -Python
- AsecurityFlicenseAqualityMCP server that allows Claude AI to interact directly with MySQL databases, enabling query execution and table information retrieval through natural language.Last updated -184JavaScript