数据库远程MCP服务器

通过Model Context Protocol (MCP)工具提供远程数据库访问功能,支持HTTP和stdio两种传输方式。

  • by agts86
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 agts86/database-remote-mcp

使用说明

数据库远程MCP服务器

将远程数据库访问功能作为Model Context Protocol(MCP)工具公开的服务器。超文本传输协议 和 标准 对较大场景进行渲染期间已观察到该故障。

概要

  • MCP_TRANSPORT 使用环境变量切换传输
    • 未设定/ http →HTTP服务器模式(/mcp, /mcp/stream)
    • stdio →stdin/stdout MCP协议模式
  • DB_TYPE 根据需要自动切换运行时
    • sqlserver / postgres / mysql ->RDBMS运行时
    • mongodb ->NoSQL运行时
  • 使用环境变量控制工具的启用
    • 关系数据库管理系统: ENABLED_TOOLS
    • NoSQL: ENABLED_NOSQL_TOOLS

当前功能

RDBMS工具

  • read_query
  • write_query
  • export_query
  • list_tables
  • describe_table
  • get_schema
  • create_table
  • alter_table
  • drop_table
  • append_insight
  • list_insights

NoSQL工具

  • list_collections(MongoDB的收藏一览取得)

NoSQL当前 list_collections 中所述修改相应参数的值。

支持数据库

  • SQL Server(mssql)
  • PostgreSQL(pg)
  • MySQL(mysql2)
  • MongoDB(mongodb)

系统要求

  • Node.js>=24
  • pnpm 推奨

起动

pnpm install
pnpm build
pnpm start          # HTTPモード(デフォルト)

stdio模式:

MCP_TRANSPORT=stdio pnpm start

开发模式(HTTP):

pnpm start:dev

MCP客户端设置示例

标准

克劳德代码/克劳德桌面

克劳德代码 —全局注册(可用于所有项目):

claude mcp add database-remote-mcp --scope user -e MCP_TRANSPORT=stdio -- \
  node --env-file=/path/to/database-remote-mcp/.env /path/to/database-remote-mcp/dist/main.js

克劳德代码 —项目本地(.mcp.json):

{
  "mcpServers": {
    "database-remote-mcp": {
      "command": "node",
      "args": [
        "--env-file=/path/to/database-remote-mcp/.env",
        "/path/to/database-remote-mcp/dist/main.js"
      ],
      "env": {
        "MCP_TRANSPORT": "stdio"
      }
    }
  }
}

克劳德桌面 — claude_desktop_config.json:

{
  "mcpServers": {
    "database-remote-mcp": {
      "command": "node",
      "args": [
        "--env-file=/path/to/database-remote-mcp/.env",
        "/path/to/database-remote-mcp/dist/main.js"
      ],
      "env": {
        "MCP_TRANSPORT": "stdio"
      }
    }
  }
}

法典

~/.codex/config.toml:

[mcp_servers.database-remote-mcp]
command = "node"
args = [
  "--env-file=/path/to/database-remote-mcp/.env",
  "/path/to/database-remote-mcp/dist/main.js"
]

[mcp_servers.database-remote-mcp.env]
MCP_TRANSPORT = "stdio"

GitHub副本(VS代码)

.vscode/mcp.json:

{
  "servers": {
    "database-remote-mcp": {
      "type": "stdio",
      "command": "node",
      "args": [
        "--env-file=/path/to/database-remote-mcp/.env",
        "/path/to/database-remote-mcp/dist/main.js"
      ],
      "env": {
        "MCP_TRANSPORT": "stdio"
      }
    }
  }
}

超文本传输协议

克劳德代码/克劳德桌面

克劳德代码 —全局注册:

claude mcp add database-remote-mcp --scope user --transport http \
  http://localhost:3000/mcp/stream

克劳德桌面 — claude_desktop_config.json:

{
  "mcpServers": {
    "database-remote-mcp": {
      "type": "http",
      "url": "http://localhost:3000/mcp/stream"
    }
  }
}

法典

~/.codex/config.toml:

[mcp_servers.database-remote-mcp]
url = "http://localhost:3000/mcp/stream"
enabled = true

GitHub副本(VS代码)

.vscode/mcp.json:

{
  "servers": {
    "database-remote-mcp": {
      "type": "http",
      "url": "http://localhost:3000/mcp/stream"
    }
  }
}

环境变数

.env.sample 的 .env 中所述修改相应参数的值。

共通

APP_PORT=3000          # HTTPモードのみ使用
ALLOWED_ORIGINS=
DB_TYPE=postgres
MCP_TRANSPORT=         # 未設定またはhttp=HTTPモード / stdio=stdioモード

启用工具

ENABLED_TOOLS=
ENABLED_NOSQL_TOOLS=

RDBMS 接続

SERVER=localhost
PORT=5432
DATABASE=postgres
USER=postgres
PASSWORD=
DB_SSL=

DB_TYPE 的 sqlserver / postgres / mysql 时褪色为此颜色。

MongoDB 接続

MONGO_URI=mongodb://localhost:27017
MONGO_DATABASE=admin

MONGO_URI 未设定时 mongodb://SERVER:PORT 来修改标记元素的显示属性。

体系结构

依存方向: Presentation -> Application -> Domain <- Infrastructure

src/
├── app.module.ts          # HTTPモード用ルートモジュール
├── app-stdio.module.ts    # stdioモード用ルートモジュール(Presentation層なし)
├── presentation/          # HTTPモードのみ使用
│   └── mcp/
│       ├── mcp.controller.ts
│       ├── mcp.controller.module.ts
│       └── sse.interceptor.ts
├── application/
│   └── mcp/
│       ├── mcp.service.ts
│       ├── runtime-resolver.service.ts
│       ├── shared/           # connectStdio() を含む共通実装
│       ├── rdbms/
│       └── nosql/
├── domain/
│   ├── mcp/
│   │   ├── shared/
│   │   ├── rdbms/
│   │   └── nosql/
│   ├── rdbms/
│   └── nosql/
└── infrastructure/
    ├── config/
    ├── rdbms/
    └── nosql/

测试和质量检查

pnpm lint
pnpm build
pnpm test