# MCP SQL Server A Model Context Protocol (MCP) server that enables AI assistants like Claude to securely interact with SQL Server databases through local connections. This server provides tools for database exploration, query execution, and data analysis through a controlled interface. ## Features - 🔒 **Secure Local Database Access**: Connect to SQL Server using SQL Authentication - 🔍 **Schema Exploration**: Discover tables, columns, and relationships - 📊 **Query Execution**: Run SQL queries with proper error handling - 🛡️ **Safety Controls**: Built-in protections against harmful operations ## Prerequisites - Node.js 18.0 or higher - npm (Node package manager) - SQL Server - Claude Desktop application ## Installation ### 1. Clone the Repository ```bash git clone https://github.com/MKhan997733/mcp-server-sql.git cd mcp-server-sql ``` ### 2. Install Dependencies ```bash npm install ``` ### 3. Configure Environment Variables Create a `.env` file in the project root: ```env DB_SERVER= DB_NAME= DB_USER= DB_PASSWORD= DB_PORT= ``` ## Claude Desktop Configuration ### 1. Locate Claude Desktop Config File The configuration file is located at: - **macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json` - **Windows**: `%APPDATA%\Claude\claude_desktop_config.json` - **Linux**: `~/.config/Claude/claude_desktop_config.json` ### 2. Add MCP Server Configuration Edit the `claude_desktop_config.json` file and add your MCP server: **For SQL Authentication:** ```json { "mcpServers": { "sql-server": { "command": "node", "args": [ "/absolute/path/to/mcp-server-sql/index.js" ], "env": { "DB_SERVER": "localhost", "DB_NAME": "your_database", "DB_USER": "your_username", "DB_PASSWORD": "your_password", "DB_PORT": "port" } } } } ``` **Important**: Replace `/absolute/path/to/mcp-server-sql/` with the actual path to your cloned repository. ### 3. Restart Claude Desktop After saving the configuration file, restart Claude Desktop for the changes to take effect. ## Usage Once configured, you can interact with your SQL Server database through Claude by asking questions like: - "What tables are in my database?" - "Show me the schema of the Users table" - "Query the Orders table and show me orders from the last week" - "Find the top 10 customers by total purchase amount" - "Show me all stored procedures in the database" ### Example Interactions **Discovering Tables:** ``` You: Can you show me what tables exist in my database? Claude: I'll help you discover the tables in your SQL Server database... ``` **Querying Data:** ``` You: Show me all customers who made purchases over $1000 in the last month Claude: I'll query your database to find customers with purchases over $1000... ``` **Schema Exploration:** ``` You: What columns does the Products table have? Claude: Let me examine the structure of the Products table... ``` ## Available Tools The MCP server provides these tools to Claude: 1. **discover_tables**: List all tables in the SQL Server database 2. **table_details**: Get detailed schema information for a specific table 3. **execute_query**: Run SELECT queries (read-only by default) 4. **get_stored_procedures**: List all stored procedures 5. **get_table_indexes**: View indexes on tables 6. **get_table_constraints**: View constraints (PK, FK, etc.) ## Security Best Practices 1. **Use Dedicated SQL Server Login**: Create a specific login for the MCP server with minimal required permissions 2. **Read-Only Access**: Configure the database user with read-only access 3. **Local Connections Only**: Server only accepts connections to localhost 5. **Audit Logging**: Enable SQL Server audit logging to track all queries ## Running the Server ```bash # Run directly npm build node /build/index.js ``` (AI GENERATED, REVIEWED BY MKHAN997733)