mcp-sql
An extensible read-only MCP server for SQL databases, enabling schema exploration and safe SELECT queries via tools like list_schemas, list_tables, describe_table, and execute_query.
README
mcp-sql
An extensible, read-only Model Context Protocol
server for SQL databases. It lets an MCP client (Claude, etc.) explore schemas and
run SELECT queries safely.
- First engine: Microsoft SQL Server (via
pyodbc/ ODBC Driver 18). - Designed to extend: new engines plug in behind a
DatabaseProviderinterface; auth methods plug in behind anAuthStrategy. - Transports: local stdio and remote streamable HTTP from the same server.
- Read-only by design: every query is validated to be a single
SELECT/WITH/EXPLAIN.
Install
Requires Python 3.11+. The MS SQL engine needs the Microsoft ODBC Driver 18:
# macOS
brew tap microsoft/mssql-release https://github.com/microsoft/homebrew-mssql-release
brew trust microsoft/mssql-release # newer Homebrew requires trusting 3rd-party taps
HOMEBREW_ACCEPT_EULA=Y brew install unixodbc msodbcsql18
# Debian/Ubuntu: see https://learn.microsoft.com/sql/connect/odbc/linux-mac/
Then install the project (with the mssql extra for the SQL Server driver):
uv sync --extra mssql --extra dev
# or: pip install -e ".[mssql,dev]"
Configure
Copy .env.example to .env and edit. Key settings (env prefix MCPSQL_):
| Setting | Purpose |
|---|---|
MCPSQL_DB_TYPE |
Engine. Currently mssql. |
MCPSQL_HOST / MCPSQL_PORT / MCPSQL_DATABASE |
Connection target. |
MCPSQL_AUTH_METHOD |
sql_password | windows | azure_ad. |
MCPSQL_MAX_ROWS |
Row cap for execute_query (default 1000). |
MCPSQL_ODBC_DRIVER |
ODBC driver name (default ODBC Driver 18 for SQL Server). |
Authentication matrix (MS SQL)
MCPSQL_AUTH_METHOD |
What it does | Extra settings |
|---|---|---|
sql_password |
SQL Server login (username/password). | MCPSQL_USERNAME, MCPSQL_PASSWORD |
windows |
Integrated / trusted connection (Windows or AD-joined host). | — |
azure_ad |
OAuth2 access token via Azure AD / Entra ID. | MCPSQL_AZURE_AUTH_MODE |
azure_ad token acquisition modes (MCPSQL_AZURE_AUTH_MODE):
default—DefaultAzureCredential(env vars, managed identity, Azure CLI, …).service_principal— readsAZURE_TENANT_ID,AZURE_CLIENT_ID,AZURE_CLIENT_SECRET.managed_identity—ManagedIdentityCredential(for Azure-hosted workloads).
Security note: the read-only validator is a guard, not a boundary. For real protection, connect with a least-privilege principal (e.g. a login mapped to
db_datareader). Then even a validator bypass cannot write.
Quickstart with the bundled demo database
A docker-compose.yml spins up SQL Server 2022 and seeds a small AppDemo
database (sales.customers / orders / order_items + a view and FKs):
docker compose up -d # start + seed (first run pulls the image)
docker compose logs seed # look for "Seed complete"
.env.example's defaults already point at this database, so:
cp .env.example .env
uv run mcp-sql # or inspect it (see below)
When you're done: docker compose down -v.
The seed also creates a least-privilege
mcp_readerlogin (passwordReader!Pass1). PointMCPSQL_USERNAME/MCPSQL_PASSWORDat it to run mcp-sql with read-only database permissions — the recommended setup.On Apple Silicon the SQL Server image runs under amd64 emulation; first start takes a minute or two.
Run
# Local stdio (default) — how MCP clients usually launch it
uv run mcp-sql
# Remote streamable HTTP
uv run mcp-sql --transport http --host 0.0.0.0 --port 8000
Endpoint authentication is not done here. This server is designed to sit behind an MCP gateway that authenticates callers. Run it only on a network reachable through that gateway (private network / mTLS); for defense in depth bind to loopback and rely on FastMCP's DNS-rebinding (allowed-hosts) protection. The gateway should also strip/overwrite client-supplied connection headers it sets.
Remote: one server, many databases
For a remote deployment the server is not pinned to a single database. The
caller (or gateway) names the database in a request header; the server
resolves that name to a connection. The wire only ever carries the name — never
credentials. Backend chosen by MCPSQL_CONNECTION_BACKEND:
| Backend | Resolves a name via | Use for |
|---|---|---|
static (default) |
the single connection in the settings above | stdio / local / single DB |
map |
MCPSQL_CONNECTIONS JSON ({name: "<ODBC string>"}) |
local/dev multi-DB (no Azure) |
keyvault |
Azure Key Vault secret <prefix><name> → ODBC string |
production |
Request header (default X-MCP-Connection) selects the connection per request:
X-MCP-Connection: analytics → secret "mcpsql-conn-analytics" in Key Vault
Key Vault path (production):
- Store each database's ODBC connection string as a secret named
mcpsql-conn-<name>. Prefer connection strings that useazure_ad/ managed identity so the vault holds no SQL password. - The server authenticates to Key Vault with
DefaultAzureCredential(managed identity in Azure) — setMCPSQL_KEYVAULT_URL. - The connection name is validated against
[A-Za-z0-9-]{1,120}and the fixed prefix, so a header can never address an arbitrary vault secret. AddMCPSQL_CONNECTION_ALLOWLISTto restrict further. - Resolved connections are cached for
MCPSQL_CONNECTION_CACHE_TTLseconds (rotation is picked up on expiry).
Resolved connections are pooled (one connection per operation, via pyodbc's driver-level pool), so a single process serves many databases and many concurrent callers safely.
Use with an MCP client (stdio)
{
"mcpServers": {
"sql": {
"command": "uv",
"args": ["run", "mcp-sql"],
"cwd": "/path/to/mcp-sql"
}
}
}
Inspect manually
npx @modelcontextprotocol/inspector uv run mcp-sql
Runnable examples
See examples/ for working client scripts:
examples/stdio_client.py— local stdio (spawns the server).examples/http_client.py— remote streamable HTTP, selecting the database per request with theX-MCP-Connectionheader.
Tools
| Tool | Description |
|---|---|
list_schemas() |
Schemas in the database. |
list_tables(schema?) |
Tables and views. |
describe_table(table, schema?) |
Columns, types, PK, FKs, indexes. |
list_relationships(schema?) |
Foreign-key relationships. |
execute_query(sql, max_rows?) |
Run a validated read-only query. |
Extending to another database
- Add a provider module implementing
DatabaseProvider(src/mcp_sql/providers/base.py). - Register it in
src/mcp_sql/providers/registry.py(one line). - Reuse the existing
AuthStrategytypes, or add new ones undersrc/mcp_sql/auth/. - Add the driver as a new optional extra in
pyproject.toml.
The MCP tool layer and the read-only validator are engine-agnostic and need no changes.
Develop
uv run pytest
The test_safety.py and test_config_auth.py suites need no database.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。