返回市场
MCP查询语言服务器

MCP查询语言服务器

作者:hendrickcastro11 星标更新:2025-09-26

项目介绍

MCPQL - SQL Server MCP

License: MIT Node.js Version TypeScript npm version Downloads GitHub stars GitHub issues GitHub forks Build Status Coverage Status SQL Server Azure SQL MCP Protocol Claude Desktop Cursor IDE Trae AI Docker Security Maintenance

一个全面的模型上下文协议(MCP)服务器,用于SQL Server数据库操作。此服务器通过MCP协议提供了10个强大的工具,用于数据库分析、对象发现和数据操作。

🚀 快速开始

先决条件

  • Node.js 18+ 和 npm
  • 带有适当连接凭证的SQL Server数据库
  • 兼容MCP的客户端(如Claude Desktop、Cursor IDE或任何MCP客户端)

安装与配置

方案1:从GitHub使用npx(推荐)

无需安装!只需配置您的MCP客户端:

对于Claude Desktop (claude_desktop_config.json):

{
  "mcpServers": {
    "mcpql": {
      "command": "npx",
      "args": ["-y", "hendrickcastro/mcpql"],
      "env": {
        "DB_AUTHENTICATION_TYPE": "sql",
        "DB_SERVER": "your_server",
        "DB_NAME": "your_database",
        "DB_USER": "your_username",
        "DB_PASSWORD": "your_password",
        "DB_PORT": "1433",
        "DB_ENCRYPT": "false",
        "DB_TRUST_SERVER_CERTIFICATE": "true"
      }
    }
  }
}

对于Cursor IDE:

{
  "mcpServers": {
    "mcpql": {
      "command": "npx",
      "args": ["-y", "hendrickcastro/mcpql"],
      "env": {
        "DB_AUTHENTICATION_TYPE": "sql",
        "DB_SERVER": "your_server",
        "DB_NAME": "your_database",
        "DB_USER": "your_username",
        "DB_PASSWORD": "your_password",
        "DB_PORT": "1433",
        "DB_ENCRYPT": "false",
        "DB_TRUST_SERVER_CERTIFICATE": "true"
      }
    }
  }
}

方案2:本地开发安装

  1. 克隆并设置:
git clone https://github.com/hendrickcastro/MCPQL.git
cd MCPQL
npm install
npm run build
  1. 配置数据库连接: 创建一个.env文件,包含您的数据库凭证:
# 基本的SQL Server连接
DB_AUTHENTICATION_TYPE=sql
DB_SERVER=localhost
DB_NAME=MyDatabase
DB_USER=sa
DB_PASSWORD=YourPassword123!
DB_PORT=1433
DB_ENCRYPT=false
DB_TRUST_SERVER_CERTIFICATE=true
  1. 使用本地路径配置MCP客户端:
{
  "mcpServers": {
    "mcpql": {
      "command": "node",
      "args": ["path/to/MCPQL/dist/server.js"]
    }
  }
}

🛠️ 可用工具

MCPQL提供了11个全面的工具,用于SQL Server数据库操作:

1. 🏗️ 表分析 - mcp_table_analysis

完整的表结构分析,包括列、键、索引和约束。

2. 📋 存储过程分析 - mcp_sp_structure

分析存储过程结构,包括参数、依赖项和源代码。

3. 👀 数据预览 - mcp_preview_data

预览表数据,可选过滤和行限制。

4. 📊 列统计 - mcp_get_column_stats

获取特定列的综合统计信息。

5. ⚙️ 执行存储过程 - mcp_execute_procedure

执行带有参数的存储过程,并返回结果。

6. 🔍 执行SQL查询 - mcp_execute_query

执行自定义SQL查询,并进行完整的错误处理。

7. ⚡ 快速数据分析 - mcp_quick_data_analysis

快速统计分析,包括行数、列分布和顶级值。

8. 🔎 全面搜索 - mcp_search_comprehensive

根据名称和定义搜索数据库对象,具有可配置的标准。

9. 🔗 对象依赖关系 - mcp_get_dependencies

获取数据库对象(表、视图、存储过程等)的依赖关系。

10. 🎯 样本值 - mcp_get_sample_values

从表中的特定列获取样本值。

11. 🔒 安全状态 - mcp_get_security_status

获取数据库操作的安全配置和状态。

📋 使用示例

分析表

// 获取完整的表结构
const analysis = await mcp_table_analysis({ 
  table_name: "dbo.Users" 
});

// 获取快速数据概览
const overview = await mcp_quick_data_analysis({ 
  table_name: "dbo.Users",
  sample_size: 500
});

// 预览带过滤器的表数据
const data = await mcp_preview_data({
  table_name: "dbo.Users",
  filters: { "Status": "Active", "Department": "IT" },
  limit: 25
});

查找数据库对象

// 查找所有包含“User”的对象
const objects = await mcp_search_comprehensive({ 
  pattern: "User",
  search_in_names: true,
  search_in_definitions: false
});

// 查找查询特定表的过程
const procedures = await mcp_search_comprehensive({ 
  pattern: "FROM Users",
  object_types: ["PROCEDURE"],
  search_in_definitions: true
});

分析存储过程

// 获取完整的存储过程分析
const spAnalysis = await mcp_sp_structure({ 
  sp_name: "dbo.usp_GetUserData" 
});

// 执行存储过程
const result = await m. mcp_execute_procedure({
  sp_name: "dbo.usp_GetUserById",
  params: { "UserId": 123, "IncludeDetails": true }
});

数据分析

// 获取列统计信息
const stats = await mcp_get_column_stats({
  table_name: "dbo.Users",
  column_name: "Age"
});

// 从列中获取样本值
const samples = await mcp_get_sample_values({
  table_name: "dbo.Users",
  column_name: "Department",
  limit: 15
});

🔧 环境变量及连接类型

MCPQL支持多种SQL Server连接类型,并提供全面的配置选项:

🔐 认证类型

设置DB_AUTHENTICATION_TYPE为以下之一:

  • sql - SQL Server认证(默认)
  • windows - Windows认证
  • azure-ad - Azure Active Directory认证

📋 完整环境变量

变量描述默认值适用于
基本连接
DB_AUTHENTICATION_TYPE认证类型(sql/windows/azure-ad)sql所有
DB_SERVERSQL Server主机名/IP-所有
DB_NAME数据库名称-所有
DB_PORTSQL Server端口1433所有
DB_TIMEOUT连接超时(毫秒)30000所有
DB_REQUEST_TIMEOUT请求超时(毫秒)30000所有
SQL Server认证
DB_USERSQL Server用户名-SQL认证
DB_PASSWORDSQL Server密码-SQL认证
Windows认证
DB_DOMAINWindows域-Windows认证
DB_USERWindows用户名当前用户Windows认证
DB_PASSWORDWindows密码-Windows认证
Azure AD认证
DB_USERAzure AD用户名-Azure AD(密码)
DB_PASSWORDAzure AD密码-Azure AD(密码)
DB_AZURE_CLIENT_IDAzure AD应用客户端ID-Azure AD(服务主体)
DB_AZURE_CLIENT_SECRETAzure AD应用客户端密钥-Azure AD(服务主体)
DB_AZURE_TENANT_IDAzure AD租户ID-Azure AD(服务主体)
SQL Server Express
DB_INSTANCE_NAME命名实例(例如,SQLEXPRESS)-Express实例
安全设置
DB_ENCRYPT启用加密false所有
DB_TRUST_SERVER_CERTIFICATE信任服务器证书false所有
DB_ENABLE_ARITH_ABORT启用算术中断true所有
DB_USE_UTC使用UTC日期true所有
连接池
DB_POOL_MAX最大连接数10所有
DB_POOL_MIN最小连接数0所有
DB_POOL_IDLE_TIMEOUT空闲超时(毫秒)30000所有
高级设置
DB_CANCEL_TIMEOUT取消超时(毫秒)5000所有
DB_PACKET_SIZE包大小(字节)4096所有
DB_CONNECTION_STRING完整连接字符串-替代单独设置
安全控制
DB_ALLOW_MODIFICATIONS允许DML/DDL操作false所有
DB_ALLOW_STORED_PROCEDURES允许存储过程执行false所有

🔧 连接配置示例

1. 🏠 本地SQL Server(SQL认证)

{
  "mcpServers": {
    "mcpql": {
      "command": "npx",
      "args": ["-y", "hendrickcastro/mcpql"],
      "env": {
        "DB_AUTHENTICATION_TYPE": "sql",
        "DB_SERVER": "localhost",
        "DB_NAME": "MyDatabase",
        "DB_USER": "sa",
        "DB_PASSWORD": "YourPassword123!",
        "DB_PORT": "1433",
        "DB_ENCRYPT": "false",
        "DB_TRUST_SERVER_CERTIFICATE": "true"
      }
    }
  }
}

2. 🏢 SQL Server Express(命名实例)

{
  "mcpServers": {
    "mcpql": {
      "command": "npx",
      "args": ["-y", "hendrickcastro/mcpql"],
      "env": {
        "DB_AUTHENTICATION_TYPE": "sql",
        "DB_SERVER": "localhost",
        "DB_INSTANCE_NAME": "SQLEXPRESS",
        "DB_NAME": "MyDatabase",
        "DB_USER": "sa",
        "DB_PASSWORD": "YourPassword123!",
        "DB_ENCRYPT": "false",
        "DB_TRUST_SERVER_CERTIFICATE": "true"
      }
    }
  }
}

3. 🪟 Windows认证

{
  "mcpServers": {
    "mcpql": {
      "command": "npx",
      "args": ["-y", "hendrickcastro/mcpql"],
      "env": {
        "DB_AUTHENTICATION_TYPE": "windows",
        "DB_SERVER": "MYSERVER",
        "DB_NAME": "MyDatabase",
        "DB_DOMAIN": "MYDOMAIN",
        "DB_USER": "myuser",
        "DB_PASSWORD": "mypassword",
        "DB_ENCRYPT": "false",
        "DB_TRUST_SERVER_CERTIFICATE": "true"
      }
    }
  }
}

4. ☁️ Azure SQL数据库(Azure AD密码)

{
  "mcpServers": {
    "mcpql": {
      "command": "npx",
      "args": ["-y", "hendrickcastro/mcpql"],
      "env": {
        "DB_AUTHENTICATION_TYPE": "azure-ad",
        "DB_SERVER": "myserver.database.windows.net",
        "DB_NAME": "MyDatabase",
        "DB_USER": "user@domain.com",
        "DB_PASSWORD": "userpassword",
        "DB_PORT": "1433",
        "DB_ENCRYPT": "true",
        "DB_TRUST_SERVER_CERTIFICATE": "false"
      }
    }
  }
}

5. 🔐 Azure SQL数据库(服务主体)

{
  "mcpServers": {
    "mcpql": {
      "command": "npx",
      "args": ["-y", "hendrickcastro/mcpql"],
      "env": {
        "DB_AUTHENTICATION_TYPE": "azure-ad",
        "DB_SERVER": "myserver.database.windows.net",
        "DB_NAME": "MyDatabase",
        "DB_AZURE_CLIENT_ID": "your-client-id",
        "DB_AZURE_CLIENT_SECRET": "your-client-secret",
        "DB_AZURE_TENANT_ID": "your-tenant-id",
        "DB_PORT": "1433",
        "DB_ENCRYPT": "true",
        "DB_TRUST_SERVER_CERTIFICATE": "false"
      }
    }
  }
}

6. 🔗 使用连接字符串

{
  "mcpServers": {
    "mcpql": {
      "command": "npx",
      "args": ["-y", "hendrickcastro/mcpql"],
      "env": {
        "DB_CONNECTION_STRING": "Server=localhost;Database=MyDatabase;User Id=sa;Password=YourPassword123!;Encrypt=false;TrustServerCertificate=true;"
      }
    }
  }
}

🔒 安全特性

MCPQL包括全面的安全控制,以防止意外的数据库修改,特别是在生产环境中尤为重要。

🛡️ 安全控制

数据库修改保护

  • DB_ALLOW_MODIFICATIONS:控制DML/DDL操作(INSERT、UPDATE、DELETE、ALTER、DROP、CREATE)
  • DB_ALLOW_STORED_PROCEDURES:控制存储过程执行
  • 默认:两个变量默认为false,以确保最大安全性

安全状态工具

使用mcp_get_security_status检查当前的安全配置:

const status = await mcp_get_security_status({});

🔧 启用操作

开发环境

{
  "mcpServers": {
    "mcpql": {
      "command": "npx",
      "args": ["-y", "hendrickcastro/mcpql"],
      "env": {
        "DB_SERVER": "localhost",
        "DB_NAME": "MyDatabase",
        "DB_USER": "sa",
        "DB_PASSWORD": "YourPassword123!",
        "DB_ALLOW_MODIFICATIONS": "true",
        "DB_ALLOW_STORED_PROCEDURES": "true"
      }
    }
  }
}

生产环境(推荐)

{
  "mcpServers": {
    "mcpql": {
      "command": "npx",
      "args": ["-y", "hendrickcastro/mcpql"],
      "env": {
        "DB_SERVER": "prod-server",
        "DB