Skip to main content
Glama
kyle66889

mcp-sqlserver

by kyle66889

MCP SQL Server

一个用于 Microsoft SQL Server 的只读 Model Context Protocol (MCP) 服务器,使 AI 代理能够安全地探索和查询 SQL Server 数据库。

快速开始

步骤 1:安装软件包

# Global installation (recommended)
npm install -g @bilims/mcp-sqlserver

# Verify installation
mcp-sqlserver --version

步骤 2:配置你的 SQL Server 连接

选择你的 SQL Server 类型并按照配置进行操作:

Azure SQL Database

export SQLSERVER_HOST="your-server.database.windows.net"
export SQLSERVER_USER="your-username"
export SQLSERVER_PASSWORD="your-password"
export SQLSERVER_DATABASE="your-database"
export SQLSERVER_ENCRYPT="true"
export SQLSERVER_TRUST_CERT="false"

本地 SQL Server

export SQLSERVER_HOST="your-sql-server.company.com"
export SQLSERVER_USER="your-username"
export SQLSERVER_PASSWORD="your-password"
export SQLSERVER_DATABASE="your-database"
export SQLSERVER_ENCRYPT="true"
export SQLSERVER_TRUST_CERT="true"  # For self-signed certificates

本地 SQL Server Express

export SQLSERVER_HOST="localhost\\SQLEXPRESS"
export SQLSERVER_USER="sa"
export SQLSERVER_PASSWORD="your-password"
export SQLSERVER_DATABASE="master"
export SQLSERVER_ENCRYPT="false"
export SQLSERVER_TRUST_CERT="true"

步骤 3:测试连接

# Test your configuration
mcp-sqlserver --help

# Quick connection test (press Ctrl+C to exit)
mcp-sqlserver

步骤 4:添加到 Claude Desktop

  1. 找到你的 Claude Desktop 配置文件:

    • macOS~/Library/Application Support/Claude/claude_desktop_config.json

    • Windows%APPDATA%\Claude\claude_desktop_config.json

  2. 添加 MCP 服务器配置:

{
  "mcpServers": {
    "sqlserver": {
      "command": "mcp-sqlserver",
      "env": {
        "SQLSERVER_HOST": "your-server.database.windows.net",
        "SQLSERVER_USER": "your-username",
        "SQLSERVER_PASSWORD": "your-password",
        "SQLSERVER_DATABASE": "your-database",
        "SQLSERVER_ENCRYPT": "true",
        "SQLSERVER_TRUST_CERT": "false"
      }
    }
  }
}
  1. 重启 Claude Desktop

步骤 5:开始探索!

在 Claude Desktop 中尝试以下命令:

  • "Test the SQL Server connection"

  • "List all databases on the server"

  • "Show me the tables in [database name]"

  • "Describe the structure of the Users table"

  • "Show me foreign key relationships"

Related MCP server: SQL Server MCP

功能特性

  • 🔒 只读操作:仅允许 SELECT 查询,并带有全面的安全验证

  • 🗄️ 架构发现:探索数据库、表、视图、关系和元数据

  • 📊 数据探索:执行带有内置限制和超时设置的安全查询

  • 🔐 企业级就绪:为生产环境提供带有证书信任的加密连接

  • 🛡️ 安全优先:查询验证、SQL 注入防护和访问控制

可用工具

架构发现

  • list_databases - 列出 SQL Server 实例上的所有数据库

  • list_tables - 列出数据库或架构中的表

  • list_views - 列出数据库或架构中的视图

  • describe_table - 获取详细的表架构,包括列、数据类型和约束

关系分析

  • get_foreign_keys - 获取表的外键关系

  • get_table_stats - 获取表统计信息,包括行数和大小信息

数据探索

  • execute_query - 执行带有安全验证的只读 SELECT 查询

  • get_server_info - 获取 SQL Server 版本、版本类型和配置详细信息

Claude Desktop 的常用命令

配置好 MCP 服务器后,尝试以下自然语言命令:

入门命令

"Test the SQL Server connection"
"Show me server information"
"List all databases on this server"
"What tables are in the [database name] database?"

数据库探索

"Describe the structure of the Users table"
"Show me foreign key relationships in this database"
"What are the largest tables by row count?"
"Give me a sample of data from the Orders table"

高级分析

"Help me understand the relationship between Orders and Customers"
"Show me all lookup tables in this database"
"What columns contain date/time information?"
"Find tables that might contain user authentication data"

自定义查询

"Run this query: SELECT TOP 10 * FROM Products WHERE Price > 100"
"Show me all customers created in the last 30 days"
"What are the different product categories in the database?"

安装

npm install
npm run build

配置

服务器使用环境变量进行配置:

必需项

  • SQLSERVER_USER - SQL Server 用户名

  • SQLSERVER_PASSWORD - SQL Server 密码

可选项

  • SQLSERVER_HOST - 服务器主机名(默认值:localhost)

  • SQLSERVER_DATABASE - 默认数据库名称

  • SQLSERVER_PORT - 端口号(默认值:1433)

  • SQLSERVER_ENCRYPT - 启用加密(默认值:true)

  • SQLSERVER_TRUST_CERT - 信任服务器证书(默认值:true)

  • SQLSERVER_CONNECTION_TIMEOUT - 连接超时时间(毫秒)(默认值:30000)

  • SQLSERVER_REQUEST_TIMEOUT - 请求超时时间(毫秒)(默认值:60000)

  • SQLSERVER_MAX_ROWS - 每次查询的最大行数(默认值:1000)

使用方法

环境变量

export SQLSERVER_HOST="your-server.database.windows.net"
export SQLSERVER_USER="your-username"
export SQLSERVER_PASSWORD="your-password"
export SQLSERVER_DATABASE="your-database"
export SQLSERVER_ENCRYPT="true"
export SQLSERVER_TRUST_CERT="true"

运行服务器

npm start

安装选项

选项 1:全局安装(推荐)

npm install -g @bilims/mcp-sqlserver

选项 2:本地安装

npm install @bilims/mcp-sqlserver
npx mcp-sqlserver

选项 3:使用 npx 运行(无需安装)

npx @bilims/mcp-sqlserver

集成示例

Claude Desktop

添加到你的 Claude Desktop 配置(claude_desktop_config.json)中:

{
  "mcpServers": {
    "sqlserver": {
      "command": "mcp-sqlserver",
      "env": {
        "SQLSERVER_HOST": "your-server.database.windows.net",
        "SQLSERVER_USER": "your-username",
        "SQLSERVER_PASSWORD": "your-password",
        "SQLSERVER_DATABASE": "your-database"
      }
    }
  }
}

Claude Code CLI

# Set environment variables
export SQLSERVER_HOST="your-server"
export SQLSERVER_USER="your-username"
export SQLSERVER_PASSWORD="your-password"

# Use with Claude Code
claude mcp add sqlserver mcp-sqlserver

带有 MCP 扩展的 VSCode

为 VSCode 安装 MCP 扩展并添加服务器配置。

安全特性

查询验证

  • 仅允许 SELECT、WITH、SHOW、DESCRIBE 和 EXPLAIN 语句

  • 危险关键字(INSERT、UPDATE、DELETE、DROP 等)的全面黑名单

  • SQL 注入模式检测

  • 自动查询清理

连接安全

  • 默认启用 TLS/SSL 加密

  • 适用于企业环境的服务器证书信任选项

  • 带有超时控制的连接池

  • 可配置的请求超时

结果限制

  • 每次查询的最大行数限制(可配置)

  • 为 SELECT 查询自动注入 TOP 子句

  • 查询执行时间跟踪

  • 内存使用保护

示例查询

连接后,你可以通过 MCP 客户端使用这些工具:

// List all databases
await callTool("list_databases", {});

// List tables in a specific schema
await callTool("list_tables", { schema: "dbo" });

// Get table schema details
await callTool("describe_table", { 
  table_name: "Users", 
  schema: "dbo" 
});

// Execute a read-only query
await callTool("execute_query", { 
  query: "SELECT TOP 10 * FROM Users WHERE active = 1",
  limit: 10
});

// Get foreign key relationships
await callTool("get_foreign_keys", { 
  table_name: "Orders" 
});

开发

# Install dependencies
npm install

# Build the project
npm run build

# Run in development mode
npm run dev

# Run linting
npm run lint

# Run tests
npm test

故障排除

连接问题

  1. 验证服务器主机名和端口

  2. 检查加密/证书设置是否与你的 SQL Server 配置匹配

  3. 确保用户具有适当的读取权限

  4. 首先使用 SQL Server Management Studio 测试连接

权限问题

用户账户至少需要:

  • 对数据库的 CONNECT 权限

  • 对要查询的表/视图的 SELECT 权限

  • 对系统视图的访问权限,用于元数据查询

常见的 SQL Server 配置

Azure SQL Database

export SQLSERVER_HOST="your-server.database.windows.net"
export SQLSERVER_ENCRYPT="true"
export SQLSERVER_TRUST_CERT="false"

带有自签名证书的本地 SQL Server

export SQLSERVER_HOST="sql-server.company.com"
export SQLSERVER_ENCRYPT="true" 
export SQLSERVER_TRUST_CERT="true"

许可证

MIT

贡献

  1. Fork 仓库

  2. 创建功能分支

  3. 进行你的更改

  4. 如适用,添加测试

  5. 提交拉取请求

支持

如有问题和疑问:

  • 查看上面的故障排除部分

  • 查阅 SQL Server 连接文档

  • 确保 MCP 客户端兼容性


使用 Model Context Protocol SDK 构建,实现无缝 AI 集成。

A
license - permissive license
Not graded
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • A
    license
    B
    quality
    D
    maintenance
    An MCP server that connects AI assistants to Microsoft SQL Server databases, enabling schema exploration and read-only queries safely.
    49
    23
    4
    MIT
  • A
    license
    Not graded
    quality
    C
    maintenance
    Read-only SQL Server MCP server enabling safe database queries, table listing, and schema inspection with built-in security protections.
    MIT
  • A
    license
    Not graded
    quality
    C
    maintenance
    A read-only MCP server for exploring on-premises, multi-instance Microsoft SQL Server estates from AI clients, with read-only enforcement and Windows authentication support.
    Apache 2.0

View all related MCP servers

Related MCP Connectors

  • Official Microsoft MCP Server to query Microsoft Entra data using natural language

  • MCP server for secureFlows: token-free URL builders and integration-linting tools for AI agents.

  • GibsonAI MCP server: manage your databases with natural language

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/kyle66889/mcp-sqlserver'

If you have feedback or need assistance with the MCP directory API, please join our Discord server