mssql-mcp-server-python

mssql-mcp-server-python

A read-only MCP server for MS SQL Server, written in Python. Lets AI agents query your database safely — no writes, no stored procedures, no shell access.

Category
访问服务器

README

mssql-mcp-server-python

A read-only MCP (Model Context Protocol) server for MS SQL Server, written in Python. Lets AI agents query your database safely — no writes, no stored procedures, no shell access.

Features

  • Read-only — only SELECT queries allowed
  • Two modes — stdio (local) or HTTP + JWT (remote)
  • Official driver — uses pyodbc with system ODBC driver
  • No third-party auth libraries — JWT verified with Python built-in hmac + hashlib
  • Query safety — blocks dangerous keywords, requires TOP or WHERE

Tools exposed to AI agents

Tool Description
list_tables List all tables in the database
describe_table Show columns and types for a table
query Run a read-only SELECT query

Requirements

  • Python >= 3.11
  • ODBC Driver for SQL Server installed on the machine

Check available ODBC drivers:

python -c "import pyodbc; print(pyodbc.drivers())"

If none installed, download from: https://learn.microsoft.com/sql/connect/odbc/download-odbc-driver-for-sql-server

Installation

Step 1 — Clone the repo:

git clone https://github.com/GarfieldHuang/mssql-mcp-server-python.git
cd mssql-mcp-server-python

Step 2 — Install Python dependencies:

pip install -r requirements.txt

Step 3 — Set up environment variables:

copy .env.example .env

Open .env and fill in your DB credentials and API_KEY.

Step 4 — Generate API_KEY:

python -c "import secrets; print(secrets.token_hex(32))"

Paste the output into .env as API_KEY=....

Usage

Stdio mode — 本機 AI agent 使用

Stdio 模式不需要手動啟動 server。AI agent(如 Claude Code)會在需要時自動啟動 server.py,透過 stdin/stdout 溝通,用完自動關閉。

claude_desktop_config.json 登記一次即可:

{
  "mcpServers": {
    "mssql": {
      "command": "python",
      "args": ["C:\\path\\to\\server.py"]
    }
  }
}

登記後重啟 Claude Code,即可直接用自然語言查詢資料庫,無需手動執行任何指令。

HTTP mode — 遠端 AI agent 使用

如果 AI agent 在其他機器上,需要改用 HTTP 模式。在能連到 MSSQL 的機器上執行:

python server.py --http

Server 會持續在背景執行(保持 cmd 視窗開著),監聽 HTTP_PORT(預設 3000)。
遠端 agent 透過 HTTPS + JWT token 連線:

POST http://your-server:3000/mcp
Authorization: Bearer <jwt-token>

Testing

Three test JSON files are included. Before running, edit each file and replace your-api-key with the value from your .env.

Step 1 — Check server starts and lists tools:

python server.py < test-list-tools.json

Expected output: JSON with query, list_tables, describe_table.

Step 2 — List tables in your database:

python server.py < test-list-tables.json

Expected output: JSON array of table names.

Step 3 — Run a query:

Edit test-query.json and replace your_table with a real table name, then:

python server.py < test-query.json

Expected output: JSON array of rows.

Windows tip: If < redirection doesn't work in cmd, use PowerShell:

Get-Content test-query.json | python server.py

Environment variables

Variable Required Description
DB_SERVER Yes MSSQL hostname or IP
DB_PORT No Default: 1433
DB_DATABASE Yes Database name
DB_USER Yes DB username (use a read-only account)
DB_PASSWORD Yes DB password
DB_DRIVER No ODBC driver name, default: ODBC Driver 18 for SQL Server
API_KEY Yes Secret key agents must pass in tool calls
HTTP_PORT No HTTP mode port, default: 3000
JWT_SECRET HTTP only Secret for JWT verification

Create a read-only DB user (MSSQL)

CREATE LOGIN mcp_agent WITH PASSWORD = 'strong-password';
CREATE USER mcp_agent FOR LOGIN mcp_agent;
EXEC sp_addrolemember 'db_datareader', 'mcp_agent';

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选