mssqlmcp

Hroerkr/mssqlmcp

3.2

If you are the rightful owner of mssqlmcp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

A .NET 8 implementation of an MCP server that provides SQL Server database access to MCP-compatible AI assistants like Claude Code.

Tools
4
Resources
0
Prompts
0

SQL Server MCP (Model Context Protocol) Server

A .NET 8 implementation of an MCP server that provides SQL Server database access to MCP-compatible AI assistants like Claude Code.

Features

  • Execute SQL queries (SELECT, INSERT, UPDATE, DELETE, etc.)
  • Get SQL Server instance and database information
  • List tables and their schemas
  • Retrieve column information for specific tables
  • Supports both Windows Authentication and SQL Server Authentication
  • Built on .NET 8 with modern async/await patterns

Prerequisites

  • .NET 8.0 SDK
  • SQL Server instance (local or remote)
  • An MCP-compatible client (like Claude Code)

Installation

  1. Clone the repository:

    git clone <your-repo-url>
    cd SqlServerMCP
    
  2. Build the project:

    dotnet build
    
  3. Test the build:

    # Use the provided test scripts
    build.bat           # Basic build with package check
    full_test.bat       # Complete build and runtime test
    test_direct.bat     # Direct MCP server test
    

Configuration

Environment Variables

Configure your SQL Server connection using these environment variables:

  • MSSQL_SERVER: SQL Server instance (default: localhost)
  • MSSQL_DATABASE: Database name (default: master)
  • MSSQL_USER: SQL Server username (optional - uses Windows Auth if not provided)
  • MSSQL_PASSWORD: SQL Server password (optional - uses Windows Auth if not provided)

Example Configuration

Windows Authentication:

set MSSQL_SERVER=localhost
set MSSQL_DATABASE=MyDatabase

SQL Server Authentication:

set MSSQL_SERVER=localhost
set MSSQL_DATABASE=MyDatabase
set MSSQL_USER=sa
set MSSQL_PASSWORD=YourPassword

Claude Code Integration

To use this MCP server with Claude Code, add the following configuration to your ~/.claude.json file:

{
  "projects": {
    "your-project-path": {
      "mcpServers": {
        "sqlserver": {
          "command": "dotnet",
          "args": [
            "run",
            "--project",
            "/path/to/SqlServerMCP/SqlServerMCP.csproj",
            "--no-build"
          ],
          "env": {
            "MSSQL_SERVER": "localhost",
            "MSSQL_DATABASE": "YourDatabase",
            "MSSQL_USER": "sa",
            "MSSQL_PASSWORD": "YourPassword"
          }
        }
      }
    }
  }
}

Note: Replace /path/to/SqlServerMCP/SqlServerMCP.csproj with the actual path to your project file.

Available MCP Tools

Once connected, the following tools will be available to your AI assistant:

ExecuteSql

Execute any SQL query (SELECT, INSERT, UPDATE, DELETE, etc.)

  • Parameter: query - The SQL query to execute
  • Returns: Query results for SELECT queries or execution status for modification queries

GetServerInfo

Get information about the SQL Server instance and current database

  • Returns: Server version, current database, current user, server name, and current date/time

ListTables

List all tables in the current database

  • Returns: Table schema names, table names, and table types

GetTableSchema

Get column information for a specific table

  • Parameter: tableName - The name of the table to inspect
  • Returns: Column details including name, data type, nullability, default values, and constraints

Usage Examples

After setting up the MCP server with Claude Code, you can use natural language commands like:

  • "Show me all tables in the database"
  • "What's the schema for the Users table?"
  • "Execute this query: SELECT * FROM Products WHERE Price > 100"
  • "Get information about this SQL Server instance"

Development

Project Structure

  • Program.cs: Entry point and MCP server setup
  • SqlServerService.cs: Core SQL Server connection and query execution logic
  • SqlServerTools.cs: MCP tool definitions and implementations
  • SqlServerMCP.csproj: Project configuration and dependencies

Building and Testing

# Clean and build
dotnet clean
dotnet build

# Run the MCP server directly
dotnet run

# Test with provided batch files
full_test.bat

Dependencies

  • ModelContextProtocol (0.2.0-preview.1): MCP server framework
  • Microsoft.Extensions.Hosting (8.0.1): Dependency injection and hosting
  • Microsoft.Extensions.Logging.Console (8.0.1): Logging support
  • Microsoft.Data.SqlClient (5.2.2): SQL Server connectivity

Security Considerations

  • Connection strings are built dynamically from environment variables
  • TrustServerCertificate=true is enabled for development scenarios
  • Sensitive information should be stored in environment variables, not hardcoded
  • Consider using more secure authentication methods in production environments

Troubleshooting

  1. Connection Issues: Verify SQL Server is running and accessible
  2. Authentication Failures: Check username/password or Windows Authentication setup
  3. Port Issues: Ensure SQL Server is listening on the expected port (default 1433)
  4. Firewall: Verify firewall allows connections to SQL Server

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly
  5. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.