MsSqlMCP

by jdlemes

6 455 downloads Not rated yet Apache-2.0

About

MCP to query SQL Server database schema, such as tables, columns, and relationships

Details

License
Apache-2.0

Explore

- Read-only access: All queries are validated to prevent data modification (INSERT, UPDATE, DELETE, DROP, EXEC, etc. are blocked)
- Schema discovery: Tables, columns, relationships, and stored procedures
- SQL execution: Safe SELECT queries only
- Dual transport: Supports both stdio and HTTP/SSE protocols
- Windows Service: Can run as a Windows Service for production deployments
- MCP Protocol: Compatible with VS Code Copilot, Claude Desktop, and other MCP clients

- .NET 10 (or .NET 9 with minor adjustments)
- SQL Server

Edit C:\Services\MsSqlMCP\appsettings.json with your SQL Server connection string:

{
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=YOUR_SQL_SERVER;Initial Catalog=YOUR_DATABASE;Encrypt=False;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Urls": "http://localhost:5000"
}

Open PowerShell as Administrator and run:


powershell

net stop MsSqlMCP
sc.exe delete MsSqlMCP


powershell

GetTables

Get all table names in the database

GetColumns

Get columns (fields) for a specific table

GetRelationships

Get foreign key relationships between tables

GetStoreProcedure

Get stored procedure definition

ExecuteSql

Execute a read-only SELECT query

| Tool | Description | Required Parameters |
|------|-------------|---------------------|
| GetTables | Get all table names in the database | None |
| GetColumns | Get columns (fields) for a specific table | tableName |
| GetRelationships | Get foreign key relationships between tables | None |
| GetStoreProcedure | Get stored procedure definition | spName |
| ExecuteSql | Execute a read-only SELECT query | sqlQuery |

All tools accept an optional databaseName parameter to query different databases in the same SQL Server instance.

MCP Server for SQL Server database schema inspection and read-only query execution.

Features

- Read-only access: All queries are validated to prevent data modification (INSERT, UPDATE, DELETE, DROP, EXEC, etc. are blocked)
- Schema discovery: Tables, columns, relationships, and stored procedures
- SQL execution: Safe SELECT queries only
- Dual transport: Supports both stdio and HTTP/SSE protocols
- Windows Service: Can run as a Windows Service for production deployments
- MCP Protocol: Compatible with VS Code Copilot, Claude Desktop, and other MCP clients

Prerequisites

- .NET 10 (or .NET 9 with minor adjustments)
- SQL Server

Architecture

The project follows SOLID principles with dependency injection:

MsSqlMCP/
├── Program.cs                    # Entry point with DI and dual transport
├── SchemaTool.cs                 # MCP tool definitions
├── Interfaces/
│   ├── IConnectionFactory.cs     # SQL connection abstraction
│   ├── IQueryExecutor.cs         # Query execution abstraction
│   ├── ISchemaRepository.cs      # Schema queries abstraction
│   └── ISqlQueryValidator.cs     # Query validation abstraction
├── Services/
│   ├── SqlConnectionFactory.cs   # Connection management
│   ├── SafeQueryExecutor.cs      # Validated query execution
│   ├── SchemaRepository.cs       # Schema query implementation
│   └── ReadOnlySqlQueryValidator.cs # Security validation (27 blocked keywords)
└── Tests/
    └── ReadOnlySqlQueryValidatorTests.cs # 42 security tests

Configuration

Connection String

Edit appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=(local);Initial Catalog=YourDatabase;Encrypt=False;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Urls": "http://localhost:5000"
}

Running

Console Mode (Development)

```bash

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.