Microsoft SQL Server MCP Server

repository·main·Indexed 18 days ago

https://github.com/richardhan/mssql_mcp_server

A Model Context Protocol (MCP) server (version 0.1.0) that enables LLMs like Claude Desktop to interact securely with Microsoft SQL Server databases. It provides an `execute_sql` tool for running queries and exposes database tables as MCP resources using a `mssql://<table_name>/data` URI scheme. The server supports Basic SQL Authentication, Windows Authentication, and Azure SQL Database connections, and can be installed via uvx or pip.

Tokens
2.1K
Snippets
8
Records
12
Agent score
63%

What's inside microsoft_sql_server_mcp

  1. Security best practices for MSSQL MCP Server

    main

    When setting up the server, follow these security guidelines:

    • Principle of Least Privilege: Create a dedicated SQL user with only the minimum permissions required for your tasks.
    • Avoid Admin Accounts: Never use sa or other administrative accounts.
    • Prefer Windows Auth: Use Windows Authentication whenever possible.
    • Encryption: Enable encryption for all sensitive data connections.
  2. Install Microsoft SQL Server MCP Server for development

    main

    To install the server in editable mode for development purposes, clone the repository and use pip install -e .:

    git clone https://github.com/RichardHan/mssql_mcp_server.git
    cd mssql_mcp_server
    pip install -e .
  3. Install Microsoft SQL Server MCP Server via pip

    main

    If you prefer using pip instead of uvx, install the package directly:

    pip install microsoft_sql_server_mcp

    Then, update your claude_desktop_config.json to use the python -m execution pattern:

    {
      "mcpServers": {
        "mssql": {
          "command": "python",
          "args": ["-m", "mssql_mcp_server"],
          "env": { ... }
        }
      }
    }
  4. Install Microsoft SQL Server MCP Server with Claude Desktop

    main

    To use the Microsoft SQL Server MCP server with Claude Desktop, add the following configuration to your claude_desktop_config.json file. This method uses uvx to run the server without manual installation.

    Ensure you provide the required environment variables for your specific database connection (see Configuration for details).

    {
      "mcpServers": {
        "mssql": {
          "command": "uvx",
          "args": ["microsoft_sql_server_mcp"],
          "env": {
            "MSSQL_SERVER": "localhost",
            "MSSQL_DATABASE": "your_database",
            "MSSQL_USER": "your_username",
            "MSSQL_PASSWORD": "your_password"
          }
        }
      }
    }
  5. Configure Microsoft SQL Server MCP Server environment variables

    main

    The server is configured via environment variables. Depending on your authentication method, you will need different sets of keys.

    Basic SQL Authentication

    Required for standard SQL logins:

    • MSSQL_SERVER: The server address (e.g., localhost).
    • MSSQL_DATABASE: The name of the database.
    • MSSQL_USER: The SQL username.
    • MSSQL_PASSWORD: The SQL password.

    Windows Authentication

    To use Windows credentials instead of a username/password pair, set:

    • MSSQL_SERVER: The server address.
    • MSSQL_DATABASE: The name of the database.
    • MSSQL_WINDOWS_AUTH: Set to true.

    Azure SQL Database

    For Azure SQL, use the Azure server endpoint and standard SQL credentials. Encryption is handled automatically for Azure connections.

    • MSSQL_SERVER: Your server endpoint (e.g., your-server.database.windows.net).
    • MSSQL_DATABASE: The name of the database.
    • MSSQL_USER: Your username.
    • MSSQL_PASSWORD: Your password.

    Optional Settings

    • MSSQL_PORT: Custom port number (defaults to 1433).
    • MSSQL_ENCRYPT: Set to true to force encryption.
  6. Configure the MCP Server via Docker Compose

    main

    The mcp_server service in the docker-compose.yml connects to the SQL Server instance using the following environment variables:

    • MSSQL_SERVER: The hostname or IP of the SQL Server. Defaults to mssql.
    • MSSQL_PORT: The port the SQL Server is listening on. Defaults to 1433.
    • MSSQL_USER: The database user. Defaults to sa.
    • MSSQL_PASSWORD: The password for the database user. Defaults to StrongPassword123!.
    • MSSQL_DATABASE: The default database to connect to. Defaults to master.

    Note: The mcp_server service has a dependency on the mssql service and will only start once the SQL Server healthcheck passes.

    # Example environment variables for MCP Server
    MSSQL_SERVER=mssql
    MSSQL_PORT=1433
    MSSQL_USER=sa
    MSSQL_PASSWORD=YourSecurePassword
    MSSQL_DATABASE=my_db
  7. Configure MSSQL MCP Server via Environment Variables

    main

    The MSSQL MCP Server is configured using environment variables. These settings determine the connection details, authentication method, and encryption settings.

    Connection Settings

    • MSSQL_SERVER: The server address (e.g., localhost, an IP, or an Azure URL). Supports (localdb)\\ format for LocalDB.
    • MSSQL_PORT: The port number (defaults to 1433).
    • MSSQL_DATABASE: The name of the database to connect to.

    Authentication

    • SQL Authentication (Default): Requires MSSQL_USER and MSSQL_PASSWORD to be set.
    • Windows Authentication: Set MSSQL_WINDOWS_AUTH=true. When this is enabled, MSSQL_USER and MSSQL_PASSWORD are ignored.

    Encryption and Azure

    • MSSQL_ENCRYPT: Set to true to enable encryption.
    • If the server address contains .database.windows.net, the server automatically configures tds_version=7.4 and sets Encrypt=yes;TrustServerCertificate=no for Azure SQL compatibility.
  8. Configure SQL Server via Docker Compose

    main

    When running the SQL Server instance using the provided docker-compose.yml, you can customize the database instance using the following environment variables:

    • MSSQL_PASSWORD: Sets the sa password. Defaults to StrongPassword123! if not provided.
    • HOST_SQL_PORT: Sets the port on your host machine to map to the SQL Server port (1433). Defaults to 1434.
    • SQL_MEMORY_LIMIT: Sets the memory limit for the SQL Server container. Defaults to 2g.

    The SQL Server instance uses a persistent volume named mssql_data mapped to /var/opt/mssql to ensure data persistence.

    # Example environment variables for SQL Server
    MSSQL_PASSWORD=YourSecurePassword
    HOST_SQL_PORT=1433
    SQL_MEMORY_LIMIT=4g
  9. Use the execute_sql tool

    main

    The server provides a tool to execute arbitrary SQL queries against the connected database. By default, the tool name is execute_sql, but this can be customized via the MSSQL_COMMAND environment variable.

    Tool Definition

    • Name: execute_sql (or your custom MSSQL_COMMAND value)
    • Arguments:
      • query (string, required): The full SQL query to execute.

    Behavior

    • SELECT queries: Returns the results as a CSV-formatted string (columns followed by rows).
    • Information Schema queries: If querying INFORMATION_SCHEMA.TABLES, it returns a list of table names.
    • Non-SELECT queries (e.g., INSERT, UPDATE, DELETE): Executes the command, commits the transaction, and returns the number of affected rows.
    {
      "name": "execute_sql",
      "arguments": {
        "query": "SELECT * FROM users WHERE id = 1"
      }
    }
  10. Run the MSSQL MCP Server via the main() function

    main

    The main() function serves as the primary entry point for the microsoft_sql_server_mcp package. It initializes and runs the MCP server using an asynchronous event loop. This is useful if you are integrating the server into a custom Python script or running it as a module.

    from mssql_mcp_server import main
    
    if __name__ == "__main__":
        main()
  11. Access database tables as MCP Resources

    main

    The server exposes database tables as MCP resources, allowing you to inspect table data using a standardized URI scheme.

    Resource URI Format

    mssql://<table_name>/data

    Reading Data

    When you read a resource URI, the server executes a SELECT TOP 100 * FROM <table_name> query and returns the data as a CSV-formatted string.

    Note: Table names are validated to prevent SQL injection. Only alphanumeric characters, underscores, and dots (for schema.table notation) are allowed.

    mssql://my_table/data