mysql-mcp-server
Enables natural language interaction with MySQL databases through MCP, supporting SQL execution, schema exploration, and database management via tools, resources, and prompts.
README
MySQL MCP Server
A proper Model Context Protocol (MCP) server that enables natural language interaction with MySQL databases.
Features
- MCP Protocol Compliance: Implements the official Model Context Protocol specification <!-- - Natural Language to SQL: Convert natural language queries to SQL using Llama 3.2 -->
- Direct SQL Execution: Execute raw SQL queries safely
- Database Schema Exploration: Explore database structure and table information
- MCP Tools: Expose database operations as MCP tools
- MCP Resources: Provide database schema and data as MCP resources
- MCP Prompts: Offer helpful prompts for database analysis
- Error Handling: Comprehensive error handling and logging
Prerequisites
- Python 3.10+
- MySQL Server
- Ollama running with Llama 3.2 model
- MCP-compatible client (Claude Desktop, Windsurf, etc.)
Setup
1. Install Dependencies
pip install -r requirements.txt
<!-- ### 2. Setup Ollama
Make sure Ollama is installed and running:
# Install Ollama (if not already installed)
curl -fsSL https://ollama.com/install.sh | sh
# Start Ollama service
ollama serve
# Pull Llama 3.2 model
ollama pull llama3.2
``` -->
### 2. Configure Environment
Copy the environment template and configure your database settings:
```bash
cp .env.example .env
Edit .env with your MySQL database configuration:
# MySQL Database Configuration
DB_HOST=localhost
DB_USER=your_mysql_user
DB_PASSWORD=your_mysql_password
DB_NAME=your_database_name
DB_PORT=3306
MCP Tools
The server exposes the following MCP tools:
execute_sql_query
Execute a SQL query and return the results.
- Parameters:
query(string) - The SQL query to execute - Returns: Formatted query results
<!-- ### natural_language_query
Convert natural language to SQL and execute the query.
- Parameters:
natural_query(string) - Natural language description of the query - Returns: Query results after converting to SQL -->
list_tables
List all tables in the database.
- Parameters: None
- Returns: List of all tables
describe_table
Get detailed information about a specific table.
- Parameters:
table_name(string) - Name of the table to describe - Returns: Table structure and row count
get_table_data
Get sample data from a table.
- Parameters:
table_name(string) - Name of the tablelimit(integer, optional) - Maximum rows to return (default: 10)
- Returns: Sample data from the table
MCP Resources
The server provides the following MCP resources:
schema://database
Get the complete database schema as a resource.
schema://tables/{table_name}
Get schema information for a specific table.
data://tables/{table_name}
Get sample data from a table as a resource.
MCP Prompts
The server offers the following MCP prompts:
sql_query_assistant
Generate a prompt for helping with SQL query creation.
- Parameters:
query_description(string) - Description of what you want to query
database_analysis_task
Generate a prompt for database analysis tasks.
- Parameters:
analysis_goal(string) - What you want to analyze in the database
Running the Server
Development Mode
Run the server in development mode with MCP Inspector:
uv run mcp dev mcp_server.py
Production Mode
Run the server with stdio transport:
python mcp_server.py
Integration with MCP Clients
Claude Desktop
-
Open Claude Desktop configuration file:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Windows:
%APPDATA%\Claude\claude_desktop_config.json
- macOS:
-
Add the server configuration:
{
"mcpServers": {
"mysql": {
"command": "python",
"args": ["/path/to/your/project/mcp_server.py"],
"env": {
"DB_HOST": "localhost",
"DB_USER": "your_mysql_user",
"DB_PASSWORD": "your_mysql_password",
"DB_NAME": "your_database_name"
}
}
}
}
- Restart Claude Desktop
Windsurf Editor
- Open MCP settings in Windsurf
- Add a new MCP server with the following configuration:
- Name:
mysql - Command:
python - Args:
/path/to/your/project/mcp_server.py - Environment variables: Your database configuration
- Name:
Usage Examples
<!-- ### Natural Language Queries
Once connected to an MCP client, you can use natural language:
"Show me all users from the users table"
"Find orders placed in the last 30 days"
"Count the number of products in each category"
``` -->
### Direct SQL Queries
"Execute: SELECT * FROM users WHERE created_at > '2024-01-01'" "Run: UPDATE products SET price = price * 1.1 WHERE category = 'electronics'"
### Database Exploration
"List all tables in the database" "Describe the structure of the orders table" "Show me sample data from the customers table"
## Testing
Use the provided test script to verify the server functionality:
```bash
python test_mcp_server.py
Troubleshooting
Common Issues
- Database Connection Errors
- Verify MySQL is running
- Check database credentials in
.env - Ensure the database exists
<!-- 2. Ollama Connection Issues
- Verify Ollama is running:
ollama serve - Check if Llama 3.2 is pulled:
ollama list - Verify Ollama URL is correct -->
- MCP Server Not Detected
- Check server configuration in client settings
- Verify the server script path is correct
- Check for syntax errors in the server code
Debug Mode
Enable debug logging by setting the log level:
LOG_LEVEL=DEBUG
Security Considerations
- Never expose your
.envfile in production - Use database users with limited privileges
- Consider using connection pooling for production
- Validate all SQL queries to prevent injection attacks <!-- - Use HTTPS for Ollama connections in production -->
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
For issues and questions:
- Check the troubleshooting section
- Review MCP documentation at https://modelcontextprotocol.io
- Open an issue in the project repository
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。