Provides comprehensive MySQL database operations including cross-database queries, schema discovery, performance monitoring, health analysis, lock detection, and AI-assisted database management with role-based access control.
MySQL MCP Server
A comprehensive Model Context Protocol (MCP) server for MySQL database operations with advanced features for database analysis, health monitoring, and AI-assisted database management.
🚀 Features
MySQL MCP Server goes beyond basic CRUD operations, providing powerful database analysis capabilities and an extensible framework for custom tools.
🔌 Transport Modes
STDIO Mode: Direct integration with MCP clients like Cursor, Cline
SSE Mode: HTTP-based Server-Sent Events for web applications
🗄️ Database Operations
Cross-Database Access: Connect once, access all databases without switching connections
Multi-Statement Execution: Execute multiple SQL statements separated by ";"
Smart Table Discovery: Find tables by comments and descriptions
SQL Execution Plan Analysis: Performance optimization insights
🛡️ Security & Permissions
Role-Based Access Control: Three permission levels (readonly, writer, admin)
SQL Comment Support: Handles SQL statements with
--and/* */comments
🔍 Advanced Analysis
Health Monitoring: Connection, transaction, and lock status analysis
Performance Insights: Index usage analysis and optimization recommendations
Lock Detection: Row-level and table-level lock monitoring
Chinese Text Processing: Pinyin conversion for Chinese field names
🤖 AI Integration
Prompt Templates: Pre-built AI prompts for database analysis and querying
Context-Aware Assistance: Intelligent database operation guidance
📋 Permission Levels
Role | Permissions | Use Case |
readonly |
,
,
,
,
| Read-only access for reporting and analysis |
writer | readonly +
,
,
| Application development and data manipulation |
admin | Full database administration | Complete database management including DDL, user management, backup/recovery |
🛠️ Available Tools
Database Operations
Tool | Description |
| Execute SQL statements with role-based permission control |
| List all available databases (excluding system databases) |
Schema Discovery
Tool | Description |
| Get table structures across databases (supports
format) |
| Retrieve table indexes with cross-database support |
| Find tables by comments and descriptions |
Performance & Health
Tool | Description |
| Analyze MySQL health (connections, transactions, locks) |
| Index usage analysis with performance recommendations |
| Detect row-level and table-level locks |
Utilities
Tool | Description |
| Convert Chinese field names to pinyin initials |
🤖 AI Prompt Templates
Prompt Template | Purpose |
| Comprehensive MySQL issue analysis and troubleshooting |
| Intelligent table data querying with AI assistance |
🚀 Quick Start
Prerequisites
Python 3.10+
UV package manager
MySQL server
Installation
📦 Installing UV Package Manager
Windows:
Linux/macOS:
🔽 Project Setup
Clone and install dependencies:
Windows (PowerShell):
git clone <repository-url> cd mcp_mysql uv syncLinux/macOS:
git clone <repository-url> cd mcp_mysql uv syncConfigure database connection:
Create a
.envfile in the project root directory:Windows (PowerShell):
# Create .env file using PowerShell @" # MySQL Database Configuration MYSQL_HOST=localhost MYSQL_PORT=3306 MYSQL_USER=your_username MYSQL_PASSWORD=your_password # MYSQL_DATABASE=specific_db # Optional: leave empty for cross-database access MYSQL_ROLE=admin # Options: readonly, writer, admin "@ | Out-File -FilePath ".env" -Encoding utf8Or manually create
# MySQL Database Configuration MYSQL_HOST=localhost MYSQL_PORT=3306 MYSQL_USER=your_username MYSQL_PASSWORD=your_password # MYSQL_DATABASE=specific_db # Optional: leave empty for cross-database access MYSQL_ROLE=admin # Options: readonly, writer, adminFor remote MySQL server (example):
MYSQL_HOST=192.168.1.100 MYSQL_PORT=3306 MYSQL_USER=root MYSQL_PASSWORD=your_secure_password MYSQL_ROLE=admin
Running the Server
SSE Mode (Web-based)
Windows (PowerShell):
Linux/macOS:
MCP Client Configuration (SSE):
STDIO Mode (Direct integration)
Windows (PowerShell):
Linux/macOS:
MCP Client Configuration (STDIO):
Windows:
Linux/macOS:
🛠️ Windows-Specific Configuration Notes
Common Issues and Solutions
PowerShell Execution Policy: If you encounter execution policy errors, run:
Path Separators:
Use double backslashes
\\or forward slashes/in JSON configurationExample:
"C:\\Users\\YourName\\mcp_mysql"or"C:/Users/YourName/mcp_mysql"
Environment Variables in Windows:
Windows Firewall: Ensure MySQL port (default 3306) is allowed through Windows Firewall if connecting to remote MySQL server.
🔧 Extending with Custom Tools
Create a new tool class:
# src/handles/my_custom_tool.py from .base import BaseHandler from mcp.types import Tool, TextContent class MyCustomTool(BaseHandler): name = "my_custom_tool" description = "Description of what this tool does" def get_tool_description(self) -> Tool: # Define tool schema pass async def run_tool(self, arguments: dict) -> list[TextContent]: # Implement tool logic passRegister the tool:
# src/handles/__init__.py from .my_custom_tool import MyCustomTool __all__ = [..., "MyCustomTool"]
💡 Usage Examples
Cross-Database Operations
List all databases:
Cross-database queries:
Table structure analysis:
AI-Assisted Operations
Performance Analysis:
Health Monitoring:
Smart Table Discovery:
Deadlock Analysis:
📖 Documentation
Development Guide - For developers working with this codebase
API Reference - Detailed tool and prompt documentation
🤝 Contributing
Fork the repository
Create a feature branch
Add your custom tools or improvements
Submit a pull request
📄 License
This project is open source. Please check the license file for details.
Star this repository if you find it helpful! ⭐
This server cannot be installed