umairahmed786/mcp-sql-server
If you are the rightful owner of mcp-sql-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
MCP SQL Server is a lightweight server that connects to a MySQL database and exposes its schema and query capabilities to LLM-based tools.
π§ MCP SQL Server
MCP SQL Server is a lightweight Model Context Protocol (MCP) server that connects to a MySQL database and exposes database schema and query capabilities to compatible LLM-based tools (like the MCP Inspector or OpenAIβs Code Interpreter extensions).
It supports:
- Running locally via Python
- Deployment via Docker
π Project Structure
MCP-SQL-SERVER/
βββ mcp_sql_server/
β βββ __init__.py
β βββ db.py
β βββ server.py
βββ requirements.txt
βββ Dockerfile
βοΈ Requirements
- Python 3.11+
- MySQL Server (accessible via network)
pip
andvirtualenv
(optional for local dev)- Docker (optional, for containerized deployment)
π§© Installation (Local Setup)
1οΈβ£ Clone the repository
git clone https://github.com/umairahmed786/mcp-sql-server.git
cd mcp-sql-server
3οΈβ£ Install dependencies
pip install -r requirements.txt
π Run Locally
You can start the MCP SQL Server directly using Python:
python -m mcp_sql_server.server --host localhost --user root --password "your_password" --db your_database_name --transport stdio
Example:
python -m mcp_sql_server.server --host localhost --user root --password "123" --db abc
π³ Run with Docker
1οΈβ£ Build the Docker image
docker build -t mcp-sql-server .
2οΈβ£ Run the Docker container
docker run --rm -it mcp-sql-server --host host.docker.internal --user root --password "123" --db abc
π‘ Note:
host.docker.internal
allows Docker to access your host machineβs MySQL server (works on macOS & Windows).
π§ MCP Tools Available
Tool Name | Description |
---|---|
list_tables() | Lists all tables in the connected database |
get_table_schema(table) | Retrieves schema for a specific table |
get_database_schema() | Returns a full schema overview with columns, keys, and relationships |
run_query(sql) | Executes read-only SELECT queries |
explain_query(sql) | Returns the MySQL EXPLAIN plan for a given query |
π§± MCP Inspector Configuration
Setting | Value |
---|---|
Transport Type | STDIO |
Command | python |
Arguments | -m mcp_sql_server.server --host localhost --user root --password 123 --db abc |
If you are using Docker, replace
python
with the Docker run command above.
β‘ Environment Variables (Optional)
You can simplify your command by setting environment variables:
export DB_HOST=localhost
export DB_USER=root
export DB_PASS="123"
export DB_NAME=abc
Then run:
python -m mcp_sql_server.server --host $DB_HOST --user $DB_USER --password $DB_PASS --db $DB_NAME
π§° Troubleshooting
Issue | Possible Fix |
---|---|
ModuleNotFoundError: No module named 'mcp' | Ensure mcp is listed in requirements.txt or run pip install mcp |
Connection Error in MCP Inspector | Check if MySQL is running and credentials are correct |
Docker cannot connect to MySQL | Use host.docker.internal instead of localhost |
π‘ Use Case: LLM Database Context Provider
Once connected, your MCP SQL Server acts as a context provider β allowing AI systems to:
- Automatically analyze schema
- Generate valid SQL queries
- Understand foreign key relationships
- Provide context-aware completions
This enables smarter and safer AI-assisted database operations.
π¬ Contact & Support
Please feel free to use, extend, and customize this project for your own use cases.
If you need help, collaboration, or guidance, you can reach out to:
π§ Email: umairahmedpaki7@gmail.com