mcp-sql-server

umairahmed786/mcp-sql-server

3.2

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.

Tools
5
Resources
0
Prompts
0

🧠 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 and virtualenv (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 NameDescription
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

SettingValue
Transport TypeSTDIO
Commandpython
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

IssuePossible Fix
ModuleNotFoundError: No module named 'mcp'Ensure mcp is listed in requirements.txt or run pip install mcp
Connection Error in MCP InspectorCheck if MySQL is running and credentials are correct
Docker cannot connect to MySQLUse 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