SqlDb-MCP-Server

SqlDb-MCP-Server

Enables AI assistants to query SQL Server databases and retrieve schema information securely through the Model Context Protocol.

Category
访问服务器

README

SQL Server MCP Server

A Model Context Protocol (MCP) server implementation that provides secure access to SQL Server databases. This server enables AI assistants and other MCP clients to query and interact with SQL Server databases through a standardized interface.

Overview

This MCP server acts as a bridge between MCP clients (like Claude Desktop or other AI assistants) and your SQL Server database, providing controlled access to execute queries and retrieve schema information. It implements the MCP protocol over stdio communication, making it suitable for integration with various AI tools and applications.

Features

Tools

The server exposes two primary tools for database interaction:

1. execute_query

  • Description: Execute any SQL query on the connected database
  • Input: SQL query string
  • Output: Query results in JSON format
  • Use Cases:
    • Data retrieval (SELECT statements)
    • Data manipulation (INSERT, UPDATE, DELETE)
    • Stored procedure execution
    • Complex joins and aggregations

2. get_schema_info

  • Description: Retrieve comprehensive database schema information
  • Input: None required
  • Output: List of all tables and stored procedures in JSON format
  • Use Cases:
    • Database exploration
    • Understanding table structures
    • Discovering available stored procedures
    • Schema documentation

Prerequisites

  • Node.js 20 or higher
  • npm 7 or higher
  • Access to a SQL Server database (Azure SQL, SQL Server, or SQL Server Express)
  • Docker Desktop (for containerized deployment)
  • Azure CLI (for Azure deployment)

Configuration

Environment Variables

The server requires the following environment variables for database connection. Create a .env file in the project root:

# Copy the example file
cp .env.example .env

Edit .env with your database credentials:

# Database Configuration
DB_USER=your_database_username
DB_PASSWORD=your_database_password
DB_SERVER=your_server.database.windows.net
DB_NAME=your_database_name
DB_ENCRYPT=true                    # Use encryption (recommended for Azure SQL)
DB_TRUST_CERT=false                 # Don't trust self-signed certificates

Security Notes

  • Never commit .env files to version control
  • Use Azure Key Vault or similar services for production deployments
  • Consider using Managed Identity for Azure deployments
  • Implement proper firewall rules for database access

Development

Install dependencies:

npm install

Build the server:

npm run build

For development with auto-rebuild:

npm run watch

Build and Testing

Local Build

  1. Install dependencies and build:
npm install
npm run build
  1. Test the build:
node build/index.js

Docker Build

  1. Build the Docker image:
docker build -t sql-server-mcp:latest .
  1. Test with Docker Compose:
docker-compose up

Deployment

Local Installation (Visual Studio Code)

To use with Visual Studio Code and MCP-enabled extensions:

  1. Install Roo Cline Extension (or another MCP-compatible extension):

    • Open VS Code
    • Go to Extensions (Ctrl+Shift+X)
    • Search for "Roo Cline" or your preferred MCP client extension
    • Click Install
  2. Configure MCP Server in VS Code settings:

    Open VS Code settings (settings.json):

    • Press Ctrl+Shift+P (or Cmd+Shift+P on Mac)
    • Type "Preferences: Open Settings (JSON)"
    • Add the MCP server configuration:
{
  "roo-cline.mcpServers": {
    "sql-server": {
      "command": "node",
      "args": ["C:/path/to/sql-server-mcp/build/index.js"],
      "env": {
        "DB_USER": "your_username",
        "DB_PASSWORD": "your_password",
        "DB_SERVER": "your_server.database.windows.net",
        "DB_NAME": "your_database",
        "DB_ENCRYPT": "true",
        "DB_TRUST_CERT": "false"
      }
    }
  }
}
  1. Alternative: Use workspace-specific configuration:

    Create .vscode/settings.json in your workspace root:

{
  "roo-cline.mcpServers": {
    "sql-server": {
      "command": "node",
      "args": ["${workspaceFolder}/sql-server-mcp/build/index.js"],
      "env": {
        "DB_USER": "${env:DB_USER}",
        "DB_PASSWORD": "${env:DB_PASSWORD}",
        "DB_SERVER": "${env:DB_SERVER}",
        "DB_NAME": "${env:DB_NAME}",
        "DB_ENCRYPT": "true",
        "DB_TRUST_CERT": "false"
      }
    }
  }
}

Note: The exact configuration key (e.g., roo-cline.mcpServers) may vary depending on your MCP client extension. Consult your extension's documentation for the correct configuration format.

Azure Deployment

This project includes comprehensive Azure deployment support with multiple options:

Option 1: Quick Deployment Script

Use the included PowerShell script for automated deployment:

# Login to Azure
az login

# Run deployment script
.\deploy-azure.ps1 `
  -ResourceGroupName "mcp-sql-rg" `
  -Location "eastus" `
  -ContainerName "mcp-sql-server"

Option 2: GitHub Actions CI/CD

  1. Fork this repository

  2. Set up GitHub Secrets:

    • AZURE_CREDENTIALS - Service principal JSON
    • DB_USER - Database username
    • DB_PASSWORD - Database password
    • DB_SERVER - Server URL
    • DB_NAME - Database name
    • DB_ENCRYPT - "true" or "false"
    • DB_TRUST_CERT - "true" or "false"
  3. Push to main branch to trigger deployment

Option 3: Manual Azure Container Instance

# Create resource group
az group create --name mcp-sql-rg --location eastus

# Create container registry
az acr create --resource-group mcp-sql-rg --name mcpsqlregistry --sku Basic

# Build and push image
az acr build --registry mcpsqlregistry --image mcp-sql-server:latest .

# Deploy container instance
az container create \
  --resource-group mcp-sql-rg \
  --name mcp-sql-server \
  --image mcpsqlregistry.azurecr.io/mcp-sql-server:latest \
  --cpu 1 --memory 1 \
  --environment-variables \
    DB_USER=$DB_USER \
    DB_SERVER=$DB_SERVER \
    DB_NAME=$DB_NAME \
    DB_ENCRYPT=true \
    DB_TRUST_CERT=false \
  --secure-environment-variables \
    DB_PASSWORD=$DB_PASSWORD

For detailed deployment instructions, see DEPLOYMENT.md.

Debugging

MCP Inspector

Use the MCP Inspector for interactive debugging:

npm run inspector

This will start the inspector and provide a URL to access debugging tools in your browser.

Container Logs

For containerized deployments:

# Docker
docker logs <container-id>

# Azure Container Instance
az container logs --resource-group mcp-sql-rg --name mcp-sql-server

Troubleshooting

Common Issues

  1. Connection Timeout

    • Check firewall rules on SQL Server
    • Verify connection string format
    • Ensure database server is accessible from deployment environment
  2. Authentication Failed

    • Verify credentials in .env file
    • Check SQL Server authentication mode
    • Ensure user has necessary permissions
  3. Build Errors

    • Ensure Node.js 20+ is installed
    • Delete node_modules and run npm install again
    • Check TypeScript compilation errors with npm run build

Security Best Practices

  1. Credential Management

    • Use environment variables, never hardcode credentials
    • Implement Azure Key Vault for production
    • Use Managed Identity when possible
  2. Network Security

    • Configure firewall rules restrictively
    • Use Private Endpoints for Azure SQL
    • Implement VNet integration for containers
  3. Access Control

    • Use least-privilege database accounts
    • Implement row-level security where needed
    • Audit database access regularly

License

MIT

Support

For issues and questions:

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选