SQL MCP Server
This project provides a Python-based Model Context Protocol (MCP) server that allows a Large Language Model (LLM) to securely interact with SQL Server or PostgreSQL databases in a read-only capacity.
Features
- Connects to PostgreSQL or SQL Server databases.
- Provides MCP tools to:
get_schema: Fetch database schema (all tables or a specific table).query_data: Execute read-onlySELECTqueries.
- Enforces read-only access through query validation.
Setup
-
Environment: This project uses
uvfor environment and dependency management. Ensure you haveuvinstalled (pip install uv). -
Dependencies: The necessary dependencies have already been installed into the
.venvdirectory usinguv add. If you need to reinstall:uv sync -
Configuration:
- Copy the example environment file:
cp .env.example .env - Edit the
.envfile and set theDATABASE_URLvariable to your database connection string. Examples are provided in the file for PostgreSQL and SQL Server (usingpyodbc). - Choose the appropriate SQLAlchemy driver prefix (
postgresql+asyncpg,mssql+pyodbc, etc.) based on your database and installed driver. - Ensure you have the necessary database drivers installed (e.g.,
psycopg2-binaryis included, but for SQL Server, you might needpyodbcand system-level ODBC drivers). - Configure the
TRANSPORT(e.g.,stdioorsse),HOST, andPORTas needed.
- Copy the example environment file:
Usage
Development & Testing
You can run the server locally for testing using the MCP development tools:
# Activate the virtual environment (optional, uv run handles it)
# source .venv/bin/activate # Linux/macOS
# .\\.venv\\Scripts\\activate # Windows
# Run with uv (recommended)
uv run mcp dev src/main.py
# Or run directly if environment is activated
# mcp dev src/main.py
This will start the MCP Inspector, allowing you to interact with the get_schema and query_data tools.
Running Standalone
You can also run the server directly using the configured transport (stdio or sse):
# Ensure .env is configured (especially TRANSPORT)
uv run python src/main.py
Integration with MCP Clients (e.g., Claude Desktop)
Refer to the mcp install command and the MCP client's documentation for integrating the server. You'll typically provide the command to run the server (using uv run python src/main.py or similar) and necessary environment variables.
Example Stdio Configuration (Conceptual):
{
"mcpServers": {
"sql-explorer": {
"command": "uv",
"args": ["run", "python", "c:/path/to/sql-mcp/src/main.py"],
"envFiles": ["c:/path/to/sql-mcp/.env"],
"env": {
"TRANSPORT": "stdio"
// DATABASE_URL will be picked from .env file
}
}
}
}
Example SSE Configuration (Conceptual):
Ensure TRANSPORT=sse, HOST, and PORT are set in .env.
{
"mcpServers": {
"sql-explorer": {
"transport": "sse",
"url": "http://localhost:8051/sse" // Or configured HOST/PORT
}
}
}
Recommend MCP Servers 💡
MSAdministrator/enrichment-mcp
An MCP server that enriches security observables using various security services like VirusTotal, Shodan, and Hybrid Analysis
mcp-wolframalpha
A Python-powered Model Context Protocol MCP server and client that uses Wolfram Alpha via API.
atomgit-mcp-server
AtomGit MCP Server is an implementation specifically for the Model Context Protocol (MCP) of the AtomGit open-source collaboration platform. It provides a series of methods that allow AI to manage repositories, issues, pull requests, branches, labels, etc. of the AtomGit open-source collaboration platform.
@r-huijts/ethics-vibe-check
An MCP server that turns Claude into a philosophical sparring partner who actively contradicts comfortable conversations and challenges confirmation bias.
Kiran1689/storyblok-mcp-server
MCP Server for managing Storyblok spaces, stories, components, assets, and workflows via Model Context Protocol
@offorte/mcp-server
MCP server for the Offorte API - Create & send proposals using AI