A comprehensive Model Context Protocol (MCP) server implementation for Microsoft SQL Server using .NET 9, providing both traditional stdio transport and modern HTTP streaming capabilities.
- Dual Transport Support: Both stdio (traditional MCP) and HTTP streaming
- Comprehensive SQL Operations: Execute queries, manage tables, retrieve metadata
- Dapper Integration: High-performance SQL data access
- Server-Sent Events: Real-time streaming capabilities for HTTP clients
- Robust Error Handling: Detailed error messages and logging
- Cross-Platform: Runs on Windows, macOS, and Linux
├── Controllers/
│ └── McpController.cs # HTTP transport and JSON-RPC endpoints
├── Models/
│ └── DatabaseModels.cs # Data models and DTOs
├── Services/
│ └── SqlServerService.cs # Database operations using Dapper
├── Tools/
│ └── SqlServerTools.cs # MCP tool implementations
└── Program.cs # Dual-mode server configuration
- .NET 9 SDK or later
- SQL Server instance (local or remote)
- Connection string to your database
-
Clone the repository:
git clone <repository-url> cd mcp-mssql/SqlServerMcpServer
-
Build the project:
dotnet build
-
Run in stdio mode (traditional MCP):
dotnet run
-
Run in HTTP mode:
dotnet run -- --http
For use with MCP clients that expect stdio transport:
dotnet runThe server will communicate via stdin/stdout using the MCP protocol.
For HTTP clients and Server-Sent Events:
dotnet run -- --httpServer will start on http://localhost:5000 (or configured port).
GET /- Server information and available toolsPOST /mcp- JSON-RPC 2.0 MCP callsGET /mcp/sse- Server-Sent Events streamingGET /health- Health check endpoint
Lists all tables in the SQL Server database.
Parameters:
connectionString(required): SQL Server connection stringschema(optional): Schema name to filter tables
Example:
{
"jsonrpc": "2.0",
"id": "1",
"method": "tools/call",
"params": {
"name": "ListTables",
"arguments": {
"connectionString": "Server=localhost;Database=MyDB;Integrated Security=true;",
"schema": "dbo"
}
}
}Executes SQL queries to read data.
Parameters:
connectionString(required): SQL Server connection stringsql(required): SQL SELECT query
Example:
{
"jsonrpc": "2.0",
"id": "2",
"method": "tools/call",
"params": {
"name": "ReadData",
"arguments": {
"connectionString": "Server=localhost;Database=MyDB;Integrated Security=true;",
"sql": "SELECT TOP 10 * FROM Users WHERE Active = 1"
}
}
}Executes INSERT statements.
Parameters:
connectionString(required): SQL Server connection stringsql(required): INSERT SQL statement
Executes UPDATE statements.
Parameters:
connectionString(required): SQL Server connection stringsql(required): UPDATE SQL statement
Returns detailed table schema information.
Parameters:
connectionString(required): SQL Server connection stringname(required): Table nameschema(optional): Schema name (defaults to "dbo")
Creates new tables using CREATE TABLE statements.
Parameters:
connectionString(required): SQL Server connection stringsql(required): CREATE TABLE SQL statement
Drops tables using DROP TABLE statements.
Parameters:
connectionString(required): SQL Server connection stringsql(required): DROP TABLE SQL statement
Tests database connectivity.
Parameters:
connectionString(required): SQL Server connection string
Retrieves comprehensive database metadata including tables, views, and stored procedures.
Parameters:
connectionString(required): SQL Server connection string
The server accepts standard SQL Server connection strings:
// Windows Authentication
"Server=localhost;Database=MyDatabase;Integrated Security=true;"
// SQL Server Authentication
"Server=localhost;Database=MyDatabase;User Id=sa;Password=YourPassword;"
// Azure SQL Database
"Server=tcp:myserver.database.windows.net,1433;Database=MyDatabase;User Id=myuser;Password=mypassword;Encrypt=True;"MCP_HTTP_MODE=true: Force HTTP modeASPNETCORE_URLS: Configure HTTP binding (default: http://localhost:5000)ASPNETCORE_ENVIRONMENT: Set to "Development" for detailed logging
-
List available tools:
curl -X POST http://localhost:5000/mcp \ -H "Content-Type: application/json" \ -d '{ "jsonrpc": "2.0", "id": "1", "method": "tools/list", "params": {} }'
-
Execute a query:
curl -X POST http://localhost:5000/mcp \ -H "Content-Type: application/json" \ -d '{ "jsonrpc": "2.0", "id": "2", "method": "tools/call", "params": { "name": "ReadData", "arguments": { "connectionString": "Server=localhost;Database=master;Integrated Security=true;", "sql": "SELECT name FROM sys.databases" } } }'
Connect to the SSE endpoint for real-time updates:
curl -N http://localhost:5000/mcp/sseThe server provides comprehensive error handling with detailed messages:
{
"jsonrpc": "2.0",
"id": "1",
"error": {
"code": -32000,
"message": "Connection failed: A network-related or instance-specific error occurred...",
"data": {
"code": "connection_error"
}
}
}- Connection Strings: Never log connection strings containing credentials
- SQL Injection: All queries use parameterized statements via Dapper
- Network Security: Use encrypted connections for production databases
- Authentication: Consider implementing API key authentication for HTTP mode
git clone <repository-url>
cd mcp-mssql/SqlServerMcpServer
dotnet restore
dotnet builddotnet test- Add methods to
SqlServerTools.cswith[McpServerTool]attribute - Implement the corresponding service methods in
SqlServerService.cs - Add HTTP endpoint handling in
McpController.cs
- Connection Failures: Verify SQL Server is running and accessible
- Authentication Issues: Check Windows Authentication vs SQL Server Authentication
- Firewall: Ensure SQL Server port (default 1433) is open
- Stdio Mode: Ensure stdout is not being captured by other processes
The server logs to stderr for MCP compatibility. Set log level in appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"SqlServerMcpServer": "Debug"
}
}
}- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For issues and questions:
- Create an issue in the GitHub repository
- Check the troubleshooting section above
- Review SQL Server connection documentation