数据库访问服务器

MCP数据库服务器为Claude提供多数据库访问能力,支持SQLite、SQL Server、PostgreSQL和MySQL等多种数据库。

  • by executeautomation
  • TypeScript
  • 本地部署
  • 388 星标
  • 更新于
在 GitHub 查看 executeautomation/mcp-database-server

使用说明

MCP数据库服务器

此MCP(模型上下文协议)服务器为Claude提供数据库访问功能,支持SQLite、SQL server、PostgreSQL和MySQL数据库。

安装

  1. 克隆存储库:
git clone https://github.com/executeautomation/mcp-database-server.git
cd mcp-database-server
  1. 安装依赖项:
npm install
  1. 构建项目:
npm run build

使用选项

有两种方法可以将此MCP服务器与Claude一起使用:

  1. 直接使用:全局安装包并直接使用
  2. 地方发展:从本地开发环境运行

直接使用NPM包

使用此MCP服务器的最简单方法是全局安装:

npm install -g @executeautomation/database-server

这允许您直接使用服务器,而无需在本地构建。

地方发展设置

如果要修改代码或从本地环境运行:

  1. 克隆并构建存储库,如安装部分所示
  2. 使用下面“用法”部分中的命令运行服务器

用法

SQLite数据库

要与SQLite数据库一起使用:

node dist/src/index.js /path/to/your/database.db

SQL Server数据库

要与SQL Server数据库一起使用,请执行以下操作:

node dist/src/index.js --sqlserver --server <server-name> --database <database-name> [--user <username> --password <password>]

所需参数:

  • --server:SQL Server主机名或IP地址
  • --database:数据库的名称

可选参数:

  • --user:SQL Server身份验证的用户名(如果未提供,将使用Windows身份验证)
  • --password:SQL Server身份验证密码
  • --port:端口号(默认值:1433)

PostgreSQL数据库

要与PostgreSQL数据库一起使用:

node dist/src/index.js --postgresql --host <host-name> --database <database-name> [--user <username> --password <password>]

所需参数:

  • --host:PostgreSQL主机名或IP地址
  • --database:数据库的名称

可选参数:

  • --user:PostgreSQL身份验证的用户名
  • --password:PostgreSQL身份验证密码
  • --port:端口号(默认值:5432)
  • --ssl:启用SSL连接(true/false)
  • --connection-timeout:连接超时(毫秒)(默认值:30000)

mysql数据库

标准认证

要与MySQL数据库一起使用:

node dist/src/index.js --mysql --host <host-name> --database <database-name> --port <port> [--user <username> --password <password>]

所需参数:

  • --host:MySQL主机名或IP地址
  • --database:数据库的名称
  • --port:端口号(默认值:3306)

可选参数:

  • --user:MySQL身份验证的用户名
  • --password:MySQL身份验证密码
  • --ssl:启用SSL连接(true/false或object)
  • --connection-timeout:连接超时(毫秒)(默认值:30000)

AWS IAM身份验证

对于具有IAM数据库身份验证的Amazon RDS MySQL实例:

先决条件:

  • 必须配置AWS凭据(RDS签名者使用默认凭据提供程序链)
  • 使用以下方法之一进行配置:
    • aws configure (使用默认配置文件)
    • AWS_PROFILE=myprofile 环境变量
    • AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY 环境变量
    • IAM角色(如果在EC2上运行)
node dist/src/index.js --mysql --aws-iam-auth --host <rds-endpoint> --database <database-name> --user <aws-username> --aws-region <region>

所需参数:

  • --host:RDS端点主机名
  • --database:数据库的名称
  • --aws-iam-auth:启用AWS IAM身份验证
  • --user:AWS IAM用户名(也是数据库用户)
  • --aws-region:RDS实例所在的AWS区域

注意:AWS IAM身份验证会自动启用SSL

配置Claude桌面

直接使用配置

如果您全局安装了该软件包,请使用以下配置Claude Desktop:

{
  "mcpServers": {
    "sqlite": {
      "command": "npx",
      "args": [
        "-y",
        "@executeautomation/database-server",
        "/path/to/your/database.db"
      ]
    },
    "sqlserver": {
      "command": "npx",
      "args": [
        "-y",
        "@executeautomation/database-server",
        "--sqlserver",
        "--server", "your-server-name",
        "--database", "your-database-name",
        "--user", "your-username",
        "--password", "your-password"
      ]
    },
    "postgresql": {
      "command": "npx",
      "args": [
        "-y",
        "@executeautomation/database-server",
        "--postgresql",
        "--host", "your-host-name",
        "--database", "your-database-name",
        "--user", "your-username",
        "--password", "your-password"
      ]
    },
    "mysql": {
      "command": "npx",
      "args": [
        "-y",
        "@executeautomation/database-server",
        "--mysql",
        "--host", "your-host-name",
        "--database", "your-database-name",
        "--port", "3306",
        "--user", "your-username",
        "--password", "your-password"
      ]
    },
    "mysql-aws": {
      "command": "npx",
      "args": [
        "-y",
        "@executeautomation/database-server",
        "--mysql",
        "--aws-iam-auth",
        "--host", "your-rds-endpoint.region.rds.amazonaws.com",
        "--database", "your-database-name",
        "--user", "your-aws-username",
        "--aws-region", "us-east-1"
      ]
    }
  }
}

本地开发配置

对于本地开发,请配置Claude Desktop以使用本地构建的版本:

{
  "mcpServers": {
    "sqlite": {
      "command": "node",
      "args": [
        "/absolute/path/to/mcp-database-server/dist/src/index.js", 
        "/path/to/your/database.db"
      ]
    },
    "sqlserver": {
      "command": "node",
      "args": [
        "/absolute/path/to/mcp-database-server/dist/src/index.js",
        "--sqlserver",
        "--server", "your-server-name",
        "--database", "your-database-name",
        "--user", "your-username",
        "--password", "your-password"
      ]
    },
    "postgresql": {
      "command": "node",
      "args": [
        "/absolute/path/to/mcp-database-server/dist/src/index.js",
        "--postgresql",
        "--host", "your-host-name",
        "--database", "your-database-name",
        "--user", "your-username",
        "--password", "your-password"
      ]
    },
    "mysql": {
      "command": "node",
      "args": [
        "/absolute/path/to/mcp-database-server/dist/src/index.js",
        "--mysql",
        "--host", "your-host-name",
        "--database", "your-database-name",
        "--port", "3306",
        "--user", "your-username",
        "--password", "your-password"
      ]
    },
    "mysql-aws": {
      "command": "node",
      "args": [
        "/absolute/path/to/mcp-database-server/dist/src/index.js",
        "--mysql",
        "--aws-iam-auth",
        "--host", "your-rds-endpoint.region.rds.amazonaws.com",
        "--database", "your-database-name",
        "--user", "your-aws-username",
        "--aws-region", "us-east-1"
      ]
    }
  }
}

Claude Desktop配置文件通常位于:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • 窗户: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

可用的数据库工具

MCP数据库服务器提供了Claude可以使用的以下工具:

工具 说明 必需参数
read_query 执行SELECT查询以读取数据 query:SQL SELECT语句
write_query 执行INSERT、UPDATE或DELETE查询 query:SQL修改语句
create_table 在数据库中创建新表 query:CREATE TABLE语句
alter_table 修改现有表架构 query:ALTER TABLE语句
drop_table 从数据库中删除表 table_name:表格名称
confirm:安全标志(必须为真)
list_tables 获取所有表的列表 无
describe_table 查看表的架构信息 table_name:表格名称
export_query 将查询结果导出为CSV/JSON query:SQL SELECT语句
format:“csv”或“json”
append_insight 在备忘录中添加业务见解 insight:见解文本
list_insights 列出所有商业见解 无

有关如何在Claude中使用这些工具的实际示例,请参阅 使用示例.

其他文件

发展

要在开发模式下运行服务器,请执行以下操作:

npm run dev

要观察开发过程中的变化:

npm run watch

需求

  • Node.js 18+
  • 对于SQL Server连接:SQL Server 2012或更高版本
  • PostgreSQL连接:PostgreSQL 9.5或更高版本

许可证

麻省理工学院