- SQL Server MCP Server for Claude Code
SQL Server MCP Server for Claude Code
SQL Server MCP Server for Claude Code
A Machine Comprehension Protocol (MCP) server that enables Claude Code to directly query Microsoft SQL Server databases.
Credits
This project is based on mssql_mcp_server by Richard Han. It has been adapted to use FreeTDS for improved cross-platform compatibility.
Prerequisites
- Python 3.8 or higher
- FreeTDS (installed by the setup script)
- SQL Server instance accessible from your machine
- One of the following operating systems:
- Linux (Ubuntu, Debian, RedHat, CentOS, etc.)
- macOS
- Windows (via WSL - Windows Subsystem for Linux)
Installation
-
Clone or download this repository:
git clone https://github.com/your-username/mcp-server-for-sql.git cd mcp-server-for-sql -
Run the installation script:
./install_mssql_mcp_clean.shThis script will:
- Detect your operating system
- Check Python installation
- Install FreeTDS
- Create necessary MCP server scripts
- Set up configuration files
-
Configure SQL Server connection:
./configure_mcp.shYou'll be prompted to enter:
- SQL Server hostname/IP and port (e.g., "myserver.database.azure.com,1433")
- Database name
- Username
- Password
-
Test the connection:
./test_tsql.shThis will verify that your connection details are correct and FreeTDS is working properly.
Usage with Claude Code
Once installed and configured, you can use the SQL Server MCP integration in Claude Code with:
mcp__mssql__execute_sql(query="SELECT * FROM YourTable")
Examples:
- List all tables:
mcp__mssql__execute_sql(query="SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'") - Count records:
mcp__mssql__execute_sql(query="SELECT COUNT(*) AS Count FROM YourTable") - Query with filtering:
mcp__mssql__execute_sql(query="SELECT * FROM Customers WHERE City='London'")
How It Works Behind the Scenes
MCP Architecture
-
Configuration Setup:
- The
.mcp.jsonfile defines the MCP server configuration - It specifies how Claude Code should launch and communicate with the server
- Contains connection details for your SQL Server
- The
-
Claude Code Integration:
- When you start Claude Code in the project directory, it detects the
.mcp.jsonfile - It registers the
mcp__mssql__execute_sqltool for use in your session
- When you start Claude Code in the project directory, it detects the
-
Query Execution Flow:
When you run a query:
-
You type:
mcp__mssql__execute_sql(query="SELECT * FROM Customers") -
Claude sends a request to the MCP server:
{ "requestId": "123", "type": "CallToolRequest", "toolName": "execute_sql", "callId": "456", "parameters": { "query": "SELECT * FROM Customers" } } -
The MCP server:
- Creates a temporary SQL file with your query
- Executes it using FreeTDS's
tsqlutility - Parses the results into a structured format
- Cleans up temporary files
-
The server returns results to Claude:
{ "requestId": "123", "type": "CallToolResponse", "callId": "456", "result": { "rows": [ {"CustomerID": "1", "Name": "John", "...": "..."}, {"CustomerID": "2", "Name": "Jane", "...": "..."} ] } } -
Claude formats and displays the results in your conversation
-
Technical Implementation
- FreeTDS Integration: Uses the FreeTDS
tsqlcommand-line tool to connect to SQL Server, providing broad compatibility across platforms - Security: Database credentials are stored only in your local configuration file, never sent as part of prompts
- Error Handling: SQL errors are captured and returned as structured responses
- Temporary File Management: Creates and cleans up temporary SQL files for each query
File Structure
install_mssql_mcp_clean.sh- Main installation scriptmssql_mcp_server.py- Core MCP server implementationrun_mssql_mcp.py- Launcher scriptconfigure_mcp.sh- Connection configuration scripttest_tsql.sh- Connection testing script.mcp.json- MCP configuration file (created during setup)
Troubleshooting
Connection Issues
-
Check FreeTDS installation:
tsql -CShould display FreeTDS version information.
-
Verify SQL Server accessibility:
- Ensure your firewall allows connections to the SQL Server port (typically 1433)
- Check that the SQL Server allows remote connections
- Verify your username and password are correct
-
Check connection configuration:
- Run
./configure_mcp.shto update your connection details - Make sure server address includes the port (e.g., "server,1433")
- Run
MCP Server Issues
-
Check log output: When running Claude Code, check the terminal for any error messages from the MCP server.
-
Verify
.mcp.jsonfile: Ensure it contains the correct paths and environment variables. -
Test direct execution:
python3 run_mssql_mcp.pyThe server should start without errors.
WSL-Specific Issues (Windows)
-
Python and FreeTDS setup:
sudo apt update sudo apt install -y python3-dev python3-pip freetds-bin freetds-dev -
Connecting to Windows SQL Server from WSL:
- Use
host.docker.internalor your Windows IP address as the server address - Ensure Windows Firewall allows connections on port 1433
- Use
-
Temporary file issues: If you experience problems with temporary files in WSL, make sure the temp directory exists and is writable.
Advanced Configuration
Manual Editing of .mcp.json
You can directly edit the .mcp.json file to:
- Change the Python executable path
- Modify environment variables
- Add additional configuration options
Example:
{
"mcpServers": {
"mssql": {
"type": "stdio",
"command": "python3",
"args": ["/path/to/run_mssql_mcp.py"],
"env": {
"MSSQL_SERVER": "myserver.database.windows.net,1433",
"MSSQL_DATABASE": "mydatabase",
"MSSQL_USER": "myusername",
"MSSQL_PASSWORD": "mypassword"
}
}
}
}
Security Considerations
- Use a database account with minimal necessary permissions
- Consider using integrated security if available in your environment
- Keep your
.mcp.jsonfile secure as it contains database credentials - Regularly review and update credentials
License
This project is open-source and freely available for use and modification.
Contributions
Contributions, bug reports, and feature requests are welcome! Please feel free to submit issues or pull requests to the original repository by Richard Han: mssql_mcp_server.