A secure Go-based solution for Microsoft SQL Server connectivity supporting both Claude Desktop (via MCP server) and Claude Code (via CLI tools).
This server is optimized for use with Claude Desktop and AI assistants, providing:
- β Safe production access - AI can analyze without risk of data corruption
- β Full query capabilities - AI can perform complex SELECT queries, JOINs, CTEs
- β Temporal workspace - Optional whitelist for AI to use temporary tables
- β Smart restrictions - Blocks dangerous operations while keeping AI fully functional
π See AI Usage Guide for detailed examples of what Claude can and cannot do.
- Security-first design with configurable TLS encryption for database connections
- Granular table permissions with whitelist for AI-safe database access
- SQL injection protection using prepared statements exclusively
- Multi-table query validation preventing unauthorized access via JOINs/subqueries
- Connection timeouts and resource limits with pooling
- Flexible connection support for modern and legacy SQL Server versions
- Custom connection strings for special configurations (SQL Server 2008+)
- Configurable security parameters for production and development environments
- Secure logging with automatic sensitive data sanitization
go mod tidyOption A: Environment Variables (Recommended)
# Copy the example environment file
cp .env.example .env
# Edit .env with your database credentials
# Then load the environment variables:
source .env # Linux/Mac
# or for Windows PowerShell:
# Get-Content .env | ForEach-Object { $name, $value = $_ -split '=', 2; [Environment]::SetEnvironmentVariable($name, $value) }Option B: Direct Export (Linux/Mac)
export MSSQL_SERVER="your-server.database.windows.net"
export MSSQL_DATABASE="YourDatabase"
export MSSQL_USER="your_user"
export MSSQL_PASSWORD="your_password"
export DEVELOPER_MODE="false"Option C: Claude Desktop Integration
# Use config.example.json as template for Claude Desktop
cp config.example.json config.json
# Edit config.json with your database credentials- Build and Run
# Quick build (Windows) build.bat # Manual build go build -o mcp-go-mssql.exe # Development mode (detailed errors) go run main.go # Production build (optimized) go build -ldflags "-w -s" -o mcp-go-mssql-secure.exe
AI-Safe Production Configuration (RECOMMENDED for AI Assistants):
{
"mcpServers": {
"production-db-ai-safe": {
"command": "C:\\path\\to\\mcp-go-mssql.exe",
"args": [],
"env": {
"MSSQL_SERVER": "your-server.database.windows.net",
"MSSQL_DATABASE": "YourDatabase",
"MSSQL_USER": "ai_user",
"MSSQL_PASSWORD": "secure_password",
"MSSQL_PORT": "1433",
"MSSQL_READ_ONLY": "true",
"MSSQL_WHITELIST_TABLES": "temp_ai,v_temp_ia",
"DEVELOPER_MODE": "false"
}
}
}
}Standard Production Configuration:
{
"mcpServers": {
"production-db": {
"command": "C:\\path\\to\\mcp-go-mssql.exe",
"args": [],
"env": {
"MSSQL_SERVER": "your-server.database.windows.net",
"MSSQL_DATABASE": "YourDatabase",
"MSSQL_AUTH": "sql",
"MSSQL_USER": "user",
"MSSQL_PASSWORD": "password",
"MSSQL_PORT": "1433",
"DEVELOPER_MODE": "false"
}
}
}
}Windows Integrated Authentication (SSPI - Named Pipes):
Option 1: Access a specific database:
{
"mcpServers": {
"production-db-windows-auth": {
"command": "C:\\path\\to\\mcp-go-mssql.exe",
"args": [],
"env": {
"MSSQL_SERVER": ".",
"MSSQL_DATABASE": "YourDatabase",
"MSSQL_AUTH": "integrated",
"DEVELOPER_MODE": "false"
}
}
}
}Option 2: Access all databases (no database specified):
{
"mcpServers": {
"production-db-windows-auth-all": {
"command": "C:\\path\\to\\mcp-go-mssql.exe",
"args": [],
"env": {
"MSSQL_SERVER": ".",
"MSSQL_AUTH": "integrated",
"DEVELOPER_MODE": "false"
}
}
}
}βΉοΈ Windows Auth Note: Uses current Windows user credentials automatically (no passwords needed).
MSSQL_SERVER="."for local server,"localhost", or server hostname for remote servers.MSSQL_DATABASEis optional - if omitted, connects to user's default database. Works with Active Directory and local Windows accounts. See Windows Authentication Guide for detailed setup and troubleshooting.
Legacy SQL Server (Custom Connection String):
{
"mcpServers": {
"legacy-db": {
"command": "C:\\path\\to\\mcp-go-mssql.exe",
"args": [],
"env": {
"MSSQL_CONNECTION_STRING": "sqlserver://sa:YourPassword@legacy-server:1433?database=LegacyDB&encrypt=disable&trustservercertificate=true",
"DEVELOPER_MODE": "true"
}
}
}
}All database connections use environment variables for security. See .env.example for complete configuration examples.
Required Variables (when not using custom connection string):
MSSQL_SERVER: SQL Server hostname or IP addressMSSQL_AUTH: Authentication mode (sql,integrated/windows, orazure)
For SQL Server Authentication (MSSQL_AUTH=sql or not set):
MSSQL_DATABASE: Database name to connect to (required)MSSQL_USER: Username for SQL Server authentication (required)MSSQL_PASSWORD: Password for SQL Server authentication (required)
For Windows Integrated Authentication (MSSQL_AUTH=integrated or windows):
MSSQL_DATABASE: Database name (optional - if omitted, connects to default database for the Windows user)- No
MSSQL_USERorMSSQL_PASSWORDneeded - uses Windows credentials automatically
Optional Variables:
MSSQL_PORT: SQL Server port (default: 1433)MSSQL_ENCRYPT: Override encryption setting ("true"or"false")MSSQL_CONNECTION_STRING: Complete custom connection string (overrides all other MSSQL_* settings)MSSQL_AUTH: Authentication mode for connecting to SQL Server. Supported values:sql(default) - SQL Server authentication usingMSSQL_USERandMSSQL_PASSWORD. RequiresMSSQL_DATABASE.integratedorwindows- Windows Integrated Authentication (SSPI). Only supported on Windows; the process runs under the current Windows user's credentials and must have proper DB permissions.MSSQL_DATABASEis optional - if omitted, connects to the user's default database. Key benefits: No passwords in config files, uses Active Directory/Windows security, seamless single sign-on.azure- Azure Active Directory authentication (advanced; may require additional config and is not fully implemented by default).
MSSQL_READ_ONLY: Security restriction ("true"allows only SELECT queries,"false"allows all operations)MSSQL_AUTOPILOT: Autonomous AI mode (default:"false")."true"skips schema validation β AI can query tables that don't exist yet without interruption. ComplementsREAD_ONLY: when both are enabled, modifications are restricted to whitelist tables while allowing flexible development without schema interruptions. Does NOT skip whitelist protection or destructive operation confirmation.- Example:
READ_ONLY=true+AUTOPILOT=true+WHITELIST_TABLES=temp_aiβ AI can modifytemp_aiand query non-existent tables freely, but cannot touch other tables
- Example:
MSSQL_WHITELIST_TABLES: Granular permissions (comma-separated list of tables/views allowed for modification whenMSSQL_READ_ONLY=true)- Example:
"temp_ai,v_temp_ia" - Enables AI to modify specific tables while protecting production data
- Validates ALL tables in queries (including JOINs, subqueries, CTEs)
- See WHITELIST_SECURITY.md for details
- Example:
DEVELOPER_MODE:"true": Development mode (detailed errors, allows self-signed certificates, disables encryption by default)"false": Production mode (generic errors, strict certificate validation, forces encryption)
π§ Custom Connection String Priority:
When MSSQL_CONNECTION_STRING is set, all other MSSQL_* variables are ignored except DEVELOPER_MODE.
Environment Setup Examples:
# Azure SQL Database (Production)
MSSQL_SERVER=your-server.database.windows.net
MSSQL_DATABASE=YourAzureDB
MSSQL_USER=your_user@your-server
MSSQL_PASSWORD=your_secure_password
DEVELOPER_MODE=false
# Local Development (No Encryption)
MSSQL_SERVER=localhost
MSSQL_DATABASE=DevDB
MSSQL_USER=dev_user
MSSQL_PASSWORD=dev_password
DEVELOPER_MODE=true
# Local Development (Force Encryption)
MSSQL_SERVER=localhost
MSSQL_DATABASE=DevDB
MSSQL_USER=dev_user
MSSQL_PASSWORD=dev_password
MSSQL_ENCRYPT=true
DEVELOPER_MODE=true
# Legacy SQL Server (e.g., SQL Server 2008) - Custom Connection String
MSSQL_CONNECTION_STRING=sqlserver://sa:YourPassword@legacy-server:1433?database=LegacyDB&encrypt=disable&trustservercertificate=true
DEVELOPER_MODE=true
# Read-Only Mode (Security Restricted)
MSSQL_SERVER=server.example.com
MSSQL_DATABASE=MyDatabase
MSSQL_USER=readonly_user
MSSQL_PASSWORD=readonly_password
MSSQL_READ_ONLY=true
MSSQL_MAX_QUERY_SIZE=2097152
DEVELOPER_MODE=false
# Windows Integrated Authentication (SSPI) - runs under the current Windows user
# Example 1: Connect to specific database
MSSQL_AUTH=integrated
MSSQL_SERVER=localhost
MSSQL_DATABASE=YourDatabase
DEVELOPER_MODE=false
# Example 2: Connect to default database (database name optional)
MSSQL_AUTH=integrated
MSSQL_SERVER=.
DEVELOPER_MODE=true
# Example 3: Remote server with domain authentication
MSSQL_AUTH=integrated
MSSQL_SERVER=SQL-SERVER.company.local
MSSQL_DATABASE=ProductionDB
DEVELOPER_MODE=false
# AI-Safe Mode with Whitelist (RECOMMENDED for AI Assistants)
MSSQL_SERVER=prod-server.database.windows.net
MSSQL_DATABASE=ProductionDB
MSSQL_USER=ai_user
MSSQL_PASSWORD=secure_password
MSSQL_READ_ONLY=true
MSSQL_WHITELIST_TABLES=temp_ai,v_temp_ia
DEVELOPER_MODE=false
# Dynamic Multi-Database Mode (single server, multiple connections)
MSSQL_SERVER=localhost
MSSQL_DATABASE=DevDB
MSSQL_USER=dev_user
MSSQL_PASSWORD=dev_password
DEVELOPER_MODE=true
MSSQL_DYNAMIC_MODE=true
MSSQL_DYNAMIC_MAX_CONNECTIONS=10
# Use tools: dynamic_connect, dynamic_list, dynamic_disconnect
# In query_database, use parameter: connection=<alias>When MSSQL_DYNAMIC_MODE=true is enabled, the server can connect to multiple databases from a single MCP instance. Connections are pre-configured in .env with credentials, and the AI only sees safe aliases β no sensitive data exposed.
Available Tools:
dynamic_connect- Activate a pre-configured connection by aliasdynamic_list- List all available dynamic connections (shows alias, server, database β NO passwords)dynamic_disconnect- Close a named dynamic connection
How it works:
- Connections are defined in
.envwith prefixMSSQL_DYNAMIC_<ALIAS>_ - AI calls
dynamic_connectwith just an alias (no credentials in params) - Server reads credentials from environment variables
- AI sees only server/database names, not passwords or users
Example .env configuration:
# Default connection (always available)
MSSQL_SERVER=10.203.3.10
MSSQL_DATABASE=JJP_CRM
MSSQL_USER=sa
MSSQL_PASSWORD=secret123
# Dynamic connections (AI sees only: identity, ferratge, crm)
MSSQL_DYNAMIC_IDENTITY_SERVER=10.203.3.11
MSSQL_DYNAMIC_IDENTITY_DATABASE=JJP_CRM_IDENTITY
MSSQL_DYNAMIC_IDENTITY_USER=ppp
MSSQL_DYNAMIC_IDENTITY_PASSWORD=ppppp
MSSQL_DYNAMIC_FERRATGE_SERVER=10.203.3.12
MSSQL_DYNAMIC_FERRATGE_DATABASE=JJP_Ferratge_PROD
MSSQL_DYNAMIC_FERRATGE_USER=ferratge_user
MSSQL_DYNAMIC_FERRATGE_PASSWORD=otra_passwordUsage:
// 1. List available connections (AI sees alias, server, database only)
tool: dynamic_list
// 2. Activate a connection by alias (no credentials exposed)
tool: dynamic_connect
params: {"alias": "identity"}
// 3. Query using the connection
tool: query_database
params: {"sql": "SELECT TOP 10 * FROM customers", "connection": "identity"}
// 4. Disconnect when done
tool: dynamic_disconnect
params: {"alias": "identity"}Claude Desktop Configuration for Dynamic Mode:
{
"mcpServers": {
"mssql-multi": {
"command": "C:\\MCPs\\clone\\mcp-go-mssql\\build\\mcp-go-mssql.exe",
"args": [],
"env": {
"DEVELOPER_MODE": "true",
"MSSQL_DYNAMIC_MODE": "true",
"MSSQL_DYNAMIC_MAX_CONNECTIONS": "10"
}
}
}
}Note: All credentials are stored in .env, NOT in Claude Desktop config. The MCP server env only needs MSSQL_DYNAMIC_MODE=true β no database credentials in the JSON config.
- Granular table permissions with whitelist system:
- Validate ALL tables in queries (FROM, JOIN, subqueries, CTEs)
- Block unauthorized access even through complex SQL patterns
- Perfect for AI assistants accessing production databases
- Example:
DELETE temp_ai FROM temp_ai JOIN usersβ BLOCKED ifusersnot whitelisted - See WHITELIST_SECURITY.md for complete guide
- Read-only mode for query-only access
- Prepared statements to prevent SQL injection
- Input validation and sanitization
- Configurable TLS encryption for database connections:
- Production: Forces encryption (
encrypt=true) - Development: Allows disabling encryption for local SQL Server instances
- Production: Forces encryption (
- Flexible certificate validation:
- Production: Strict certificate validation (
trustservercertificate=false) - Development: Allows self-signed certificates (
trustservercertificate=true)
- Production: Strict certificate validation (
- Connection pooling with resource limits
- Secure error handling with production/development modes
- Go 1.26+
- Microsoft SQL Server with TLS support
- Network access to SQL Server (port 1433)
TLS Certificate Issues:
Error: "certificate signed by unknown authority"
Solution: Set DEVELOPER_MODE=true for self-signed certificates
Encryption Issues:
Error: "SSL Provider: No credentials are available in the security package"
Solution: Set DEVELOPER_MODE=true to disable encryption for local SQL Server
Force No Encryption (Development):
# For local SQL Server without TLS
DEVELOPER_MODE=true
# This automatically sets encrypt=false for developmentTLS Handshake Issues (Legacy SQL Server):
Error: "TLS Handshake failed: tls: server selected unsupported protocol version"
Solution: Use custom connection string with URL format for SQL Server 2008/2012
Connection String Formats:
Standard Format (Modern SQL Server 2014+):
# Automatically used when individual variables are set
MSSQL_SERVER=server.example.com
MSSQL_DATABASE=MyDatabase
MSSQL_USER=username
MSSQL_PASSWORD=password
DEVELOPER_MODE=trueURL Format (Legacy SQL Server 2008-2012):
# Use this for older SQL Server versions
MSSQL_CONNECTION_STRING=sqlserver://username:password@server:1433?database=dbname&encrypt=disable&trustservercertificate=true
DEVELOPER_MODE=trueNo Encryption (Development):
# For local SQL Server without TLS
DEVELOPER_MODE=true
# This automatically sets encrypt=false for development# Make sure environment variables are set first
cd test
go run test-connection.go
# For debugging connection issues
cd debug
go run debug-connection.goβ οΈ Never commit.envorconfig.jsonfiles with real credentials- β Always use environment variables for sensitive data
- π Use strong passwords and enable TLS encryption
- π’ For production: Set
DEVELOPER_MODE=falseand use valid certificates
Use main.go as an MCP server with Claude Desktop:
Available Tools:
| Tool | Description |
|---|---|
get_database_info |
Check connection status, encryption, and access mode |
query_database |
Execute SQL queries securely (prepared statements) |
list_tables |
List all tables and views in the database |
describe_table |
Get column structure (supports schema.table format) |
list_databases |
List all user databases on the server |
get_indexes |
Get indexes for a specific table |
get_foreign_keys |
Get FK relationships (incoming and outgoing) |
list_stored_procedures |
List all stored procedures |
execute_procedure |
Execute whitelisted stored procedures |
Environment Variables for New Features:
# Whitelist stored procedures for execute_procedure tool
MSSQL_WHITELIST_PROCEDURES="sp_GetCustomerOrders,sp_GenerateReport"Use claude-code/db-connector.go directly with Claude Code:
cd claude-code
go run db-connector.go test # Test connection
go run db-connector.go tables # List tables
go run db-connector.go query "SELECT ..." # Execute queriesSee claude-code/README.md for detailed Claude Code integration.
- AI Usage Guide - How Claude/AI works with security restrictions
- Windows Authentication Guide - Setup and troubleshooting for Windows Integrated Auth (SSPI)
- Whitelist Security - Configure granular table permissions
- README (this file) - Installation and configuration
- Security Analysis - Comprehensive security assessment
- Security Audit Report - Detailed audit findings
- Claude Code Documentation - Using with Claude Code
- Test Results - Test coverage and results
mcp-go-mssql/
βββ main.go # MCP server for Claude Desktop
βββ build.bat # Windows build script
βββ docs/ # Documentation
β βββ AI_USAGE_GUIDE.md # How to use with Claude/AI β
β βββ SECURITY_ANALYSIS.md # Security assessment
β βββ SECURITY_AUDIT_REPORT.md # Audit report
β βββ SECURITY_SUMMARY.md # Security summary
β βββ UPDATE_GO.md # Go version upgrade guide
β βββ WHITELIST_SECURITY.md # Table whitelist guide
β βββ ... # Other documentation
βββ scripts/ # Utility scripts
β βββ security-check.ps1 # Automated security validation
β βββ test-mcp-server.ps1 # Server testing
β βββ ... # Other scripts
βββ test/ # Tests
β βββ security/ # Security test suite
β β βββ cves_test.go # CVE checks
β β βββ security_tests.go # Security validation
β βββ test-connection.go # Connection testing
βββ claude-code/ # Claude Code integration
β βββ db-connector.go # CLI database tool
β βββ README.md # Claude Code documentation
βββ .env.example # Environment variables template
βββ config.example.json # Claude Desktop config template
βββ CLAUDE.md # Claude Code project documentation
βββ README.md # This file
This project is designed for secure database connectivity in critical environments.