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
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.