- Explore MCP Servers
- mysql_mcp
Mysql Mcp
What is Mysql Mcp
mysql_mcp is an AI-powered SQL agent that enables users to interact with MySQL databases using natural language queries. It translates English questions into SQL queries, executes them, and presents the results in a user-friendly format.
Use cases
Use cases for mysql_mcp include generating reports on customer purchases, checking product stock levels, analyzing order values, identifying employees without sales, and counting orders by status.
How to use
To use mysql_mcp, clone the repository, run the setup script, configure the .env file with your OpenRouter API key and MySQL credentials, activate the virtual environment, and run the application. You can then start asking questions about your database in natural language.
Key features
Key features of mysql_mcp include a natural language interface for querying databases, AI-powered translation using OpenRouter API, schema awareness for generating accurate queries, safety checks to prevent dangerous operations, and formatted results for easy readability.
Where to use
mysql_mcp can be used in various fields such as data analysis, business intelligence, customer relationship management, and any application that requires interaction with MySQL databases using natural language.
Clients Supporting MCP
The following are the main client software that supports the Model Context Protocol. Click the link to visit the official website for more information.
Overview
What is Mysql Mcp
mysql_mcp is an AI-powered SQL agent that enables users to interact with MySQL databases using natural language queries. It translates English questions into SQL queries, executes them, and presents the results in a user-friendly format.
Use cases
Use cases for mysql_mcp include generating reports on customer purchases, checking product stock levels, analyzing order values, identifying employees without sales, and counting orders by status.
How to use
To use mysql_mcp, clone the repository, run the setup script, configure the .env file with your OpenRouter API key and MySQL credentials, activate the virtual environment, and run the application. You can then start asking questions about your database in natural language.
Key features
Key features of mysql_mcp include a natural language interface for querying databases, AI-powered translation using OpenRouter API, schema awareness for generating accurate queries, safety checks to prevent dangerous operations, and formatted results for easy readability.
Where to use
mysql_mcp can be used in various fields such as data analysis, business intelligence, customer relationship management, and any application that requires interaction with MySQL databases using natural language.
Clients Supporting MCP
The following are the main client software that supports the Model Context Protocol. Click the link to visit the official website for more information.
Content
AI SQL Agent
An AI-powered SQL agent that allows you to interact with MySQL databases using natural language queries. This tool translates English questions into SQL queries, executes them, and returns the results in a user-friendly format.
Features
- 🗣️ Natural Language Interface: Query your database using plain English
- 🤖 AI-Powered Translation: Uses OpenRouter API to convert natural language to SQL
- 🔍 Schema-Aware: Takes database structure into account when generating queries
- 🛡️ Safety Checks: Prevents potentially dangerous operations
- 📊 Formatted Results: Clean display of query results
- 🌐 REST API: Access via HTTP endpoints using FastAPI
Requirements
- Python 3.7+
- MySQL Database
- OpenRouter API key (Get one here)
Installation
- Clone this repository
git clone https://github.com/yashasraj2324/mysql_mcp.git cd mysql_mcp - Run the setup script
chmod +x setup.sh ./setup.sh - Edit the
.envfile with your OpenRouter API key and MySQL credentialsOPENROUTER_API_KEY=your_key_here MYSQL_HOST=localhost MYSQL_PORT=3306 MYSQL_USER=root MYSQL_PASSWORD=your_password MYSQL_DATABASE=your_database
Usage
Command Line Interface
- Activate the virtual environment
source venv/bin/activate - Run the command line application
python mcp_client.py - Start asking questions about your database in natural language!
REST API
- Activate the virtual environment
source venv/bin/activate - Run the API server
uvicorn api:app --reload - Access the API at
http://localhost:8000- Swagger documentation:
http://localhost:8000/docs - ReDoc documentation:
http://localhost:8000/redoc
- Swagger documentation:
API Endpoints
- GET / - Check if API is running
- POST /chat - Submit a natural language query
- Request body:
{"question": "Your question about the database"} - Response:
{"sql": "Generated SQL", "explanation": "Plain English explanation", "result": "Query results"}
- Request body:
Example Queries
- “Show me the top 5 customers by purchase amount”
- “List all products that are out of stock”
- “What’s the average order value in the last month?”
- “Find employees who haven’t made a sale yet”
- “Count orders by status”
How It Works
The system uses a client-server architecture with three main components:
-
MCP Client (mcp_client.py)
- Manages the chat interface
- Uses OpenRouter API to translate natural language to SQL
- Communicates with the MCP server to execute queries
-
MCP Server (mcp_server.py)
- Connects to your MySQL database
- Provides tools for database operations
- Executes SQL queries and returns results
-
API Server (api.py)
- Provides HTTP endpoints for remote access
- Communicates with the MCP server
- Returns results in JSON format
The communication flow is:
User Question → OpenRouter NL→SQL Translation → SQL Query → MySQL Database → Results → User
Customization
Changing the AI Model
You can change the OpenRouter model by editing the OPENROUTER_MODEL variable in your .env file. Available options include:
anthropic/claude-3-opus-20240229(default)openai/gpt-4-turboanthropic/claude-3-5-sonnet-20240620anthropic/claude-3-haiku-20240307
For the API, you can change the model in the OpenRouterAgent class in api.py.
Adjusting System Prompts
You can modify the system prompt in mcp_client.py or api.py to change how the AI generates SQL queries.
Deployment
For production deployment:
-
Run with Gunicorn
gunicorn -w 4 -k uvicorn.workers.UvicornWorker api:app -
Or deploy with Docker (see Dockerfile in repo)
docker build -t ai-sql-agent . docker run -p 8000:8000 ai-sql-agent
Troubleshooting
Connection Issues
- Make sure your MySQL credentials are correct in the
.envfile - Check that your MySQL server is running and accessible
API Key Issues
- Verify your OpenRouter API key is correct
- Ensure you have sufficient credits in your OpenRouter account
SQL Generation Issues
- Try to be more specific in your questions
- Include table names if the query involves multiple tables
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a pull request.
Acknowledgements
- FastMCP - The MCP communication framework
- OpenRouter - AI model provider
- FastAPI - API framework
Dev Tools Supporting MCP
The following are the main code editors that support the Model Context Protocol. Click the link to visit the official website for more information.










