
MSSQL
STDIOMCP server for comprehensive Microsoft SQL Server database access and management
MCP server for comprehensive Microsoft SQL Server database access and management
A Model Context Protocol (MCP) server that provides comprehensive access to Microsoft SQL Server databases. This enhanced server enables Language Models to inspect database schemas, execute queries, manage database objects, and perform advanced database operations through a standardized interface.
Database Connection: Connect to MSSQL Server instances with flexible authentication
Schema Inspection: Complete database object exploration and management
Query Execution: Execute SELECT, INSERT, UPDATE, DELETE, and DDL queries
Stored Procedure Management: Create, modify, execute, and manage stored procedures
View Management: Create, modify, delete, and describe views
Index Management: Create, delete, and analyze indexes
Resource Access: Browse table and view data as MCP resources
Security: Read-only and write operations are properly separated and validated
🔴 CRITICAL: Limit to ONE database per MCP server instance
⚠️ IMPORTANT: File operations not supported within chat context
Clone or create the project directory:
mkdir mcp-sqlserver && cd mcp-sqlserver
Run the installation script:
chmod +x install.sh ./install.sh
Configure your database connection:
cp env.example .env # Edit .env with your database details
Create virtual environment:
python3 -m venv venv source venv/bin/activate
Install dependencies:
pip install -r requirements.txt
Install ODBC Driver (macOS):
brew tap microsoft/mssql-release brew install msodbcsql17 mssql-tools
Create a .env
file with your database configuration:
MSSQL_DRIVER={ODBC Driver 17 for SQL Server} MSSQL_SERVER=your-server-address MSSQL_DATABASE=your-database-name MSSQL_USER=your-username MSSQL_PASSWORD=your-password MSSQL_PORT=1433 TrustServerCertificate=yes
MSSQL_SERVER
: Server hostname or IP address (required)MSSQL_DATABASE
: Database name to connect to (required)MSSQL_USER
: Username for authenticationMSSQL_PASSWORD
: Password for authenticationMSSQL_PORT
: Port number (default: 1433)MSSQL_DRIVER
: ODBC driver name (default: {ODBC Driver 17 for SQL Server})TrustServerCertificate
: Trust server certificate (default: yes)Trusted_Connection
: Use Windows authentication (default: no)MCP (Model Context Protocol) servers are designed to work with AI assistants and language models. They communicate via stdin/stdout using JSON-RPC protocol, not as traditional web services.
For AI Assistant Integration:
python3 src/server.py
The server will start and wait for MCP protocol messages on stdin. This is how AI assistants like Claude Desktop or other MCP clients will communicate with it.
For Testing and Development:
Test database connection:
python3 test_connection.py
Check server status:
./status.sh
View available tables:
# The server provides tools that can be called by MCP clients # Direct testing requires an MCP client or testing framework
The enhanced server provides comprehensive database management tools:
read_query
- Execute SELECT queries to read datawrite_query
- Execute INSERT, UPDATE, DELETE, and DDL querieslist_tables
- List all tables in the databasedescribe_table
- Get schema information for a specific tablecreate_table
- Create new tablescreate_procedure
- Create new stored proceduresmodify_procedure
- Modify existing stored proceduresdelete_procedure
- Delete stored procedureslist_procedures
- List all stored procedures with metadatadescribe_procedure
- Get complete procedure definitionsexecute_procedure
- Execute procedures with parametersget_procedure_parameters
- Get detailed parameter informationcreate_view
- Create new viewsmodify_view
- Modify existing viewsdelete_view
- Delete viewslist_views
- List all views in the databasedescribe_view
- Get view definitions and schemacreate_index
- Create new indexesdelete_index
- Delete indexeslist_indexes
- List all indexes (optionally by table)describe_index
- Get detailed index informationlist_schemas
- List all schemas in the databaselist_all_objects
- List all database objects organized by schemaBoth tables and views are exposed as MCP resources with URIs like:
mssql://table_name/data
- Access table data in CSV formatmssql://view_name/data
- Access view data in CSV formatResources provide the first 100 rows of data in CSV format for quick data exploration.
# Start with schemas
list_schemas
# Get all objects in a specific schema
list_all_objects(schema_name: "dbo")
# Or get all objects across all schemas
list_all_objects()
# List all tables
list_tables
# Get detailed table information
describe_table(table_name: "YourTableName")
# Access table data as MCP resource
# URI: mssql://YourTableName/data
# List all views
list_views
# Get view definition
describe_view(view_name: "YourViewName")
# Create a new view
create_view(view_script: "CREATE VIEW MyView AS SELECT * FROM MyTable WHERE Active = 1")
# Access view data as MCP resource
# URI: mssql://YourViewName/data
# List all procedures
list_procedures
# Get complete procedure definition (handles large procedures like wmPostPurchase)
describe_procedure(procedure_name: "YourProcedureName")
# Save large procedures to file for analysis
write_file(file_path: "procedure_name.sql", content: "procedure_definition")
# Get parameter details
get_procedure_parameters(procedure_name: "YourProcedureName")
# Execute procedure
execute_procedure(procedure_name: "YourProcedureName", parameters: ["param1", "param2"])
# List all indexes
list_indexes()
# List indexes for specific table
list_indexes(table_name: "YourTableName")
# Get index details
describe_index(index_name: "IX_YourIndex", table_name: "YourTableName")
# Create new index
create_index(index_script: "CREATE INDEX IX_NewIndex ON MyTable (Column1, Column2)")
CREATE PROCEDURE GetEmployeeCount AS BEGIN SELECT COUNT(*) AS TotalEmployees FROM Employees END
CREATE PROCEDURE GetEmployeesByDepartment @DepartmentId INT, @MinSalary DECIMAL(10,2) = 0 AS BEGIN SELECT EmployeeId, FirstName, LastName, Salary, DepartmentId FROM Employees WHERE DepartmentId = @DepartmentId AND Salary >= @MinSalary ORDER BY LastName, FirstName END
CREATE PROCEDURE GetDepartmentStats @DepartmentId INT, @EmployeeCount INT OUTPUT, @AverageSalary DECIMAL(10,2) OUTPUT AS BEGIN SELECT @EmployeeCount = COUNT(*), @AverageSalary = AVG(Salary) FROM Employees WHERE DepartmentId = @DepartmentId END
ALTER PROCEDURE GetEmployeesByDepartment @DepartmentId INT, @MinSalary DECIMAL(10,2) = 0, @MaxSalary DECIMAL(10,2) = 999999.99 AS BEGIN SELECT EmployeeId, FirstName, LastName, Salary, DepartmentId, HireDate FROM Employees WHERE DepartmentId = @DepartmentId AND Salary BETWEEN @MinSalary AND @MaxSalary ORDER BY Salary DESC, LastName, FirstName END
The server efficiently handles large database objects like stored procedures:
# Describe a large procedure (gets complete definition)
describe_procedure(procedure_name: "wmPostPurchase")
# Works with procedures of any size (tested with 1400+ line procedures)
# Content is displayed in chat for viewing and copy-paste operations
⚠️ Important: While large procedures can be retrieved and displayed in chat, saving them to files via MCP tools is not reliable due to inference token limits. For bulk data extraction:
Add this server to your Claude Desktop configuration:
{ "mcpServers": { "mssql": { "command": "python3", "args": ["/path/to/mcp-sqlserver/src/server.py"], "cwd": "/path/to/mcp-sqlserver", "env": { "MSSQL_SERVER": "your-server", "MSSQL_DATABASE": "your-database", "MSSQL_USER": "your-username", "MSSQL_PASSWORD": "your-password" } } } }
The server follows the standard MCP protocol and should work with any compliant MCP client.
mcp-sqlserver/
├── src/
│ └── server.py # Main MCP server implementation with chunking system
├── tests/
│ └── test_server.py # Unit tests
├── requirements.txt # Python dependencies
├── .env # Database configuration (create from env.example)
├── env.example # Configuration template
├── install.sh # Installation script
├── start.sh # Server startup script (for development)
├── stop.sh # Server shutdown script
├── status.sh # Server status script
└── README.md # This file
Run the test suite:
python -m pytest tests/
Test database connection:
python3 test_connection.py
The server uses Python's logging module. Set the log level by modifying the logging.basicConfig()
call in src/server.py
.
Enable debug logging by setting the log level to DEBUG in src/server.py
:
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
If you encounter issues with large content:
.env
configurationwmPostPurchase
(1400+ lines, 57KB)This project is open source. See the license file for details.
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.