MCP SQL Server

MCP SQL Server

This is a Model Context Protocol (MCP) server for SQL Server interactions. It allows Large Language Models (LLMs) like Claude to execute SQL queries, explore database schema, and maintain persistent connections across multiple requests.

Features

  • SQL Query Execution: Run SELECT queries against SQL Server databases
  • Stored Procedure Support: Execute stored procedures with parameters
  • Schema Exploration: View database table and column definitions
  • Session Persistence: Maintains SQL connections between queries for temporary tables and multi-query operations
  • Docker Ready: Simple deployment with Docker

Quick Start

# Clone the repository git clone https://github.com/JubinSaniei/mcp-mssql cd mcp-mssql # Start the container with your SQL Server connection details ./docker-run.sh 192.168.1.100 YourSecurePassword # Test the connection and session persistence ./test-mcp.sh ./test-session-persistence.sh

For complete Docker setup instructions, see the Docker README.

Configuration

The server supports the following environment variables:

CategoryVariableDescriptionDefault
ConnectionSQL_SERVERSQL Server hostname or IP172.31.64.1
SQL_PORTSQL Server port1433
SQL_USERSQL Server usernamesa
SQL_PASSWORDSQL Server passwordRequired
SQL_DATABASEDatabase name''
SecuritySQL_ENCRYPTEnable encryptiontrue
SQL_TRUST_SERVER_CERTTrust server certificatetrue
TimeoutsSQL_CONNECTION_TIMEOUTConnection timeout (ms)30000
SQL_REQUEST_TIMEOUTRequest timeout (ms)30000
Connection PoolSQL_POOL_MAXMax connections in pool10
SQL_POOL_MINMin connections in pool0
SQL_POOL_IDLE_TIMEOUTIdle timeout for pool (ms)30000

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 queries:
    <mcp:execute_query> SELECT TOP 10 * FROM YourTable </mcp:execute_query>
  2. Execute stored procedures:
    <mcp:execute_StoredProcedure> { "procedure": "sp_tables", "parameters": [] } </mcp:execute_StoredProcedure>
  3. Explore database schema:
    <mcp:schema> YourDatabaseName </mcp:schema>

Session Persistence

This MCP server implements session persistence for SQL connections, which allows:

  • Creating and using temporary tables across multiple queries
  • Maintaining variables and session state
  • Persistent transactions (though exercise caution with long-running transactions)
  • Better performance without reconnecting

The session persistence is handled automatically - Claude will maintain the same database connection throughout a single conversation.

Development

Local Development Setup

# Install dependencies npm install # Run the server directly (requires environment variables to be set) npm start # Run with TypeScript compiler watching for changes npm run dev

Testing

# Test basic query functionality ./test-query.sh # Test session persistence (requires Docker) ./test-session-persistence.sh

Security Notes

  • The server only allows SELECT queries (no INSERT, UPDATE, DELETE, etc.)
  • System and extended stored procedures (sp_*, xp_*) are blocked
  • SQL injection protection is implemented for database names and parameters

Troubleshooting

If you encounter issues:

  1. Check container logs: docker logs mssql-mcp
  2. Verify SQL Server connection: ./test-mcp.sh
  3. Test session persistence: ./test-session-persistence.sh
  4. Ensure the SQL_PASSWORD environment variable is correctly set

For detailed troubleshooting steps, see the Docker README.

-
security - not tested
-
license - not tested
-
quality - not tested

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. Session Persistence
          1. Development
            1. Local Development Setup
            2. Testing
          2. Security Notes
            1. Troubleshooting
              ID: 6rj1s2u5o5