MCP SQL Server

by JubinSaniei

Integrations

  • Supports configuration through environment variables loaded from .env files, allowing customization of connection settings, security parameters, timeouts, and caching behavior.

  • Enables deployment of the MCP server using Docker containers, with configuration via environment variables and docker-compose for simplified setup and management.

  • Provides access to the repository for cloning and installation, with documentation and setup instructions accessible through GitHub.

MCP MSSQL Server

This is a Model Context Protocol (MCP) server for SQL Server interactions. It allows Large Language Models (LLMs) to execute SQL queries, run stored procedures, and explore database schemas with enhanced security and robustness.

Features

  • Secure SQL Query Execution: Run SELECT queries against SQL Server databases. All queries are parsed and validated to ensure only SELECT statements are executed.
  • Stored Procedure Support: Execute stored procedures with parameterized inputs.
  • Schema Exploration: View database table and column definitions, with results cached for performance.
  • Robust Connection Management: Utilizes a global connection pool for efficient reuse of database connections, with configurable retry logic and timeouts.
  • Enhanced Security:
    • SQL query parsing and SELECT-only whitelist.
    • SQL_ALLOWED_DATABASES environment variable to whitelist accessible databases.
    • Protection against common SQL injection patterns for database context switching.
    • Blocks execution of potentially harmful system procedures or commands in direct queries.
  • Configurable Caching: Database schema information is cached with a configurable Time-To-Live (TTL).
  • Structured Logging: Integrated pino logger for detailed and structured application logs.
  • Docker Ready: Simple deployment with Docker.

Quick Start

# Clone the repository (if you haven't already) git clone https://github.com/JubinSaniei/mcp-mssql # cd mcp-mssql # Copy example configuration and edit with your settings cp .env.example .env nano .env # Edit with your SQL Server details and other configurations # Start the Docker container docker-compose up -d

For complete Docker setup instructions, see the Docker README.

Configuration

The server is configured using environment variables. Create a .env file in the root directory (you can copy .env.example) to set these values.

For a detailed guide on all configuration options and how to set them up, please see CONFIG.

CategoryVariableDescriptionDefault (from config.js)
ConnectionSQL_SERVERSQL Server hostname or IPlocalhost
SQL_PORTSQL Server port1433
SQL_USERSQL Server usernamesa
SQL_PASSWORDSQL Server passwordRequired
SQL_DATABASEDefault database name to connect tomaster
SecuritySQL_ENCRYPTEnable encryption (set to false to disable)true
SQL_TRUST_SERVER_CERTTrust server certificate (set to false to disable)true
SQL_ALLOWED_DATABASESComma-separated list of databases the server is allowed to access. If empty, access is less restricted (relies on DB user permissions).[] (empty list)
Timeouts & RetriesSQL_CONNECTION_TIMEOUTConnection timeout (ms)30000
SQL_REQUEST_TIMEOUTRequest timeout for queries (ms)30000
SQL_MAX_RETRIESMax number of retries for initial connection attempts3
SQL_INITIAL_RETRY_DELAYInitial delay (ms) before retrying a failed connection1000
SQL_MAX_RETRY_DELAYMaximum delay (ms) for connection retries (uses exponential backoff)30000
Connection PoolSQL_POOL_MAXMax connections in pool10
SQL_POOL_MINMin connections in pool0
SQL_POOL_IDLE_TIMEOUTIdle timeout for connections in pool (ms)30000
CachingSQL_SCHEMA_CACHE_TTLTime-To-Live for schema cache (ms)300000 (5 minutes)
MCP ServerMCP_SERVER_NAMEName of the MCP serverMSSQL Server
MCP_SERVER_VERSIONVersion of the MCP server1.0.0
LoggingLOG_LEVELLog level for pino logger (e.g., fatal, error, warn, info, debug, trace, silent). This is read directly from process.env in server.ts, not part of config.js.info

Using with Claude

To add this MCP server to Claude CLI:

# Add the MCP server using the config file claude mcp add-json mssql-mcp "$(cat claude-mcp-config.json)" # To add it globally claude mcp add-json -s user mssql-mcp "$(cat claude-mcp-config.json)" # Start a conversation with Claude using this MCP claude mcp mssql-mcp

In the Claude conversation, you can:

  1. Execute SELECT queries:
    <mcp:execute_query database="YourDatabaseName"> SELECT TOP 10 * FROM YourTable </mcp:execute_query>
    (The database attribute is optional if operating on the default SQL_DATABASE or if SQL_ALLOWED_DATABASES implies a single choice.)
  2. Execute stored procedures:
    <mcp:execute_StoredProcedure database="YourDatabaseName"> { "procedure": "YourSchema.YourProcedureName", "parameters": [ {"name": "Param1", "type": "NVarChar", "value": "SomeValue"}, {"name": "Param2", "type": "Int", "value": 123} ] } </mcp:execute_StoredProcedure>
  3. Explore database schema:
    <mcp:schema> YourDatabaseName </mcp:schema>
    (If YourDatabaseName is omitted, it defaults to the SQL_DATABASE specified in the environment variables.)

Connection Handling

This MCP server utilizes a global, robust connection pool (mssql library's built-in pooling) managed by the DatabaseService.

  • Efficiency: Connections are reused, reducing the overhead of establishing a new connection for each request.
  • Resilience: Implements retry logic with exponential backoff for initial connection establishment.
  • No Session State Across Calls: Unlike a session-per-user model, this server does not guarantee that subsequent MCP calls (e.g., two separate execute_query calls) from the LLM will use the exact same underlying database connection. Therefore, session-specific state like temporary tables or session variables created in one call may not be available in another. Each call should be considered atomic from a session state perspective. The USE [database] command is issued within each operation if the target database differs from the pool's default, ensuring context for that specific operation.

Development

Local Development Setup

# Install dependencies npm install # Create and configure your .env file cp .env.example .env nano .env # Run the server directly (requires environment variables to be set) npm start # Run with TypeScript compiler watching for changes npm run dev

Security Notes

  • SELECT Only: The server strictly enforces that only SELECT queries can be run via the execute_query tool, using SQL parsing. DML (INSERT, UPDATE, DELETE) and DDL statements are blocked.
  • Stored Procedure Execution: While stored procedures can perform any action their permissions allow, their execution is managed separately.
  • Database Whitelisting: Use the SQL_ALLOWED_DATABASES environment variable to restrict which databases the server can interact with. For a detailed explanation of this feature and how it interacts with SQL_DATABASE, please see DATABASE_WHITELISTING.md.
  • System Procedure Blocking: Direct execution of common system procedures (e.g., sp_, xp_) and commands like RECONFIGURE or WAITFOR DELAY via execute_query is blocked. Stored procedures should be used for legitimate system interactions.
  • Input Validation: Database names for context switching and stored procedure names undergo format validation. SQL parsing provides an additional layer of validation for queries.
  • Parameterized Inputs: Stored procedure parameters are handled by the mssql library, which typically parameterizes them to prevent SQL injection.

Troubleshooting

If you encounter issues:

  1. Check container logs: docker logs mssql-mcp (if using Docker).
  2. Check the server's console output for pino logs if running locally.
  3. Verify all required environment variables in your .env file are correctly set, especially SQL_PASSWORD, SQL_SERVER, SQL_USER, and SQL_DATABASE.
  4. Ensure the database(s) you are trying to access are listed in SQL_ALLOWED_DATABASES if you have set this variable.
  5. Confirm network connectivity to your SQL Server instance from where the MCP server is running.
  6. The test scripts (test-mcp.sh, test-session-persistence.sh) might need review/updates.

For detailed Docker troubleshooting, see the Docker README.

-
security - not tested
F
license - not found
-
quality - not tested

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

A Model Context Protocol server that allows Large Language Models like Claude to execute SQL queries, explore database schemas, and maintain persistent connections to SQL Server databases.

  1. Features
    1. Quick Start
      1. Using Docker (Recommended)
    2. Configuration
      1. Using with Claude
        1. Connection Handling
          1. Development
            1. Local Development Setup
          2. Security Notes
            1. Troubleshooting

              Related MCP Servers

              • -
                security
                A
                license
                -
                quality
                A Model Context Protocol server that enables Large Language Models to seamlessly interact with ClickHouse databases, supporting resource listing, schema retrieval, and query execution.
                Last updated -
                1
                Python
                MIT License
                • Linux
                • Apple
              • A
                security
                A
                license
                A
                quality
                A Model Context Protocol server that allows Large Language Models to interact with Astra DB databases, providing tools for managing collections and records through natural language commands.
                Last updated -
                10
                115
                12
                TypeScript
                Apache 2.0
                • Apple
              • -
                security
                -
                license
                -
                quality
                A Model Context Protocol server that provides tools for connecting to and interacting with various database systems (SQLite, PostgreSQL, MySQL/MariaDB, SQL Server) through a unified interface.
                Last updated -
                Python
              • -
                security
                F
                license
                -
                quality
                A Model Context Protocol server that enables Large Language Models to access and interact with database connections, including viewing schemas and performing CRUD operations on connected databases.
                Last updated -
                • Apple

              View all related MCP servers

              ID: 6rj1s2u5o5