SQL Server MCP
Provides secure, read-only access to Microsoft SQL Server with multi-layer protection, enabling safe query execution, schema discovery, and SQL script analysis through natural language.
README
SQL Server MCP
A secure Model Context Protocol (MCP) server for Microsoft SQL Server that provides safe, read-only database access with comprehensive protection layers.
Features
- Read-Only Query Execution - Execute SELECT queries with automatic pagination support
- SQL Script Review - Comprehensive analysis with 42 automated best practice checks and risk scoring
- Schema Discovery - Get database metadata (tables, columns, types) in token-efficient format
- Execution Plan Analysis - Retrieve and analyze SQL Server query execution plans
- Multi-Environment Support - Switch between Int, Stg, and Prd environments
- Best Practices Engine - Access 50 DBA-defined SQL Server best practice rules
How the MCP Protects the Database
The MCP implements multi-layer protection at both the SQL Server (database) level and the MCP (application) level:
Database-Level Protections (SQL Server Query Hints)
These protections are enforced directly by SQL Server through query hints automatically injected into every query:
-
CPU Control (MAXDOP) - Limits CPU parallelism per query:
- Production:
MAXDOP 1(single-threaded, uses 1 CPU core) - Staging:
MAXDOP 2(limited parallelism) - Development:
MAXDOP 4(allows parallelism) - Prevents: A single query from consuming all CPU cores
- Production:
-
Memory Control (MAX_GRANT_PERCENT) - Limits memory allocation per query:
- Production:
MAX_GRANT_PERCENT = 10(10% of available query memory) - Staging:
MAX_GRANT_PERCENT = 25(25% of available query memory) - Development:
MAX_GRANT_PERCENT = 50(50% of available query memory) - Prevents: A single query from consuming excessive SQL Server memory
- Production:
-
Lock Prevention (NOLOCK) - Prevents read blocking in production:
- Automatically adds
WITH (NOLOCK)hints to all table references - Production only: Prevents queries from blocking write operations
- Configurable: Can be enabled/disabled per environment
- Automatically adds
-
Query Execution Timeout - Enforces maximum query execution time:
- Default: 60s (Int: 120s, Stg: 90s, Prd: 30s, Max: 300s)
- Set via ODBC connection timeout (MCP sets the value)
- SQL Server automatically cancels queries that exceed timeout
- Prevents: Long-running queries from consuming resources indefinitely
How it works:
- Query hints (MAXDOP, MAX_GRANT_PERCENT, NOLOCK): The MCP automatically appends
OPTION (MAXDOP n, MAX_GRANT_PERCENT = x)orWITH (NOLOCK)to queries. SQL Server enforces these limits during query execution. - Query timeout: The MCP sets the timeout value via ODBC connection properties. SQL Server enforces it by automatically canceling queries that exceed the timeout.
MCP-Level Protections (Application-Level Limits)
These protections are enforced by the MCP application before and during query execution:
-
Concurrency Throttling - Limits concurrent queries per environment and user:
- Max 5 concurrent queries per environment
- Max 2 concurrent queries per user
- Prevents: Resource exhaustion from too many simultaneous queries
-
AST Validation - Strict read-only enforcement using SQL parsing:
- Blocks INSERT, UPDATE, DELETE, DDL operations
- Blocks multi-statement batches
- Prevents: Accidental or malicious write operations
-
Query Cost Checking - Blocks expensive queries before execution:
- Analyzes SQL Server execution plan cost (estimates CPU, I/O, memory)
- Default threshold: 50 (Int: 100, Stg: 50, Prd: 10)
- Prevents: CPU-intensive queries from running
-
Result Set Size Limits - Prevents large result sets:
- Payload Size Limit: Default 1MB per query result (configurable)
- Row Limit: Default 1000 rows (Int: 10000, Stg: 5000, Prd: 500)
- Batch Fetching: Uses
fetchmanyinstead offetchallto control memory - Automatic Truncation: Large text values (>1000 chars) are truncated
- Prevents: Excessive memory consumption in the MCP application
-
Allowed Databases - Optional whitelist of databases that can be queried
- Prevents: Access to unauthorized databases
Additional MCP Protections:
- Linked Server Blocking - Detects and blocks queries using OPENQUERY, OPENDATASOURCE, OPENROWSET, and four-part names
- Connection Pooling - Efficient connection management with configurable pool size
- Structured Logging - All operations logged in JSON format for audit and monitoring
Protection Summary
| Protection Type | Level | What It Controls | How It Works |
|---|---|---|---|
| MAXDOP | Database | CPU cores per query | SQL Server query hint (OPTION (MAXDOP n)) |
| MAX_GRANT_PERCENT | Database | Memory per query | SQL Server query hint (OPTION (MAX_GRANT_PERCENT = x)) |
| NOLOCK | Database | Read locks | SQL Server table hint (WITH (NOLOCK)) |
| Query Timeout | Database | Query execution time | ODBC timeout (MCP sets value, SQL Server enforces cancellation) |
| Concurrency Throttling | MCP | Concurrent queries | Application-level semaphore |
| Query Cost Check | MCP | Query complexity | Pre-execution plan analysis |
| Result Size Limits | MCP | Result set size | Application-level validation |
| AST Validation | MCP | Query type (read-only) | Pre-execution SQL parsing |
How to Use
Quick Start with Docker
-
Start the services:
docker-compose up -d -
Configure Cursor MCP:
- Open Cursor Settings → Features → MCP
- Add new MCP server:
- Command:
docker - Args:
exec -i mcp-sql-server python server.py
- Command:
-
Use the MCP tools:
- Query data: "What tables exist in the database?"
- Review SQL: "Review this query: SELECT * FROM Users"
- Get schema: "Show me the database schema"
MCP Tools
query_readonly - Execute Safe Queries
query_readonly(
query="SELECT id, username FROM dbo.Users WHERE is_active = 1",
env="Int", # Optional: Int, Stg, or Prd
database="MyAppDB", # Optional
page_size=10, # Optional: pagination
page=1 # Optional: page number
)
review_sql_script - Analyze SQL Scripts
review_sql_script(
script="SELECT * FROM Users WHERE YEAR(created_date) = 2024",
env="Int"
)
# Returns: risk_score, findings, best_practice_warnings
schema_summary - Get Database Schema
schema_summary(
env="Int",
search_term="user" # Optional: filter tables
)
explain - Get Execution Plan
explain(
query="SELECT * FROM dbo.Users WHERE id = 1",
env="Int"
)
get_best_practices - List All Rules
get_best_practices()
# Returns: Complete list of 50 best practice rules
config_info - Get Server Configuration
config_info()
# Returns: Current MCP server settings
Configuration
Environment Variables (Required):
DB_CONNECTION_STRING_INT="Driver={ODBC Driver 18...};Server=...;Database=...;Uid=...;Pwd=..."
DB_CONNECTION_STRING_STG="..." # Optional
DB_CONNECTION_STRING_PRD="..." # Optional
Config File: config/config.yaml - Defines limits, timeouts, and environment-specific settings.
Database User Permissions
The database user should have ONLY:
db_datareaderon target databasesVIEW SERVER STATEfor execution plan analysis
Never grant: db_datawriter, db_ddladmin, or sysadmin roles.
Requirements: Python 3.11+, Docker (for local testing), ODBC Driver 18 for SQL Server
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。