DBHub 是一个实现了 Model Context Protocol (MCP) 服务器接口的通用数据库网关。该网关允许兼容 MCP 的客户端连接并探索不同的数据库。
演示 SSE 端点
https://raw.githubusercontent.com/bytebase/dbhub/main/resources/images/mcp-inspector.webp
支持矩阵
数据库资源
资源名称 | URI 格式 | PostgreSQL | MySQL | MariaDB | SQL Server | SQLite | Oracle |
schemas | db://schemas | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
tables_in_schema | db://schemas/{schemaName}/tables | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
table_structure_in_schema | db://schemas/{schemaName}/tables/{tableName} | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
indexes_in_table | db://schemas/{schemaName}/tables/{tableName}/indexes | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
procedures_in_schema | db://schemas/{schemaName}/procedures | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ |
procedure_details_in_schema | db://schemas/{schemaName}/procedures/{procedureName} | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ |
数据库工具
工具 | 命令名称 | PostgreSQL | MySQL | MariaDB | SQL Server | SQLite | Oracle |
执行 SQL | execute_sql | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
列出连接器 | list_connectors | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
提示功能
提示 | 命令名称 | PostgreSQL | MySQL | MariaDB | SQL Server | SQLite | Oracle |
生成 SQL | generate_sql | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
解释数据库元素 | explain_db | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
安装
Docker
NPM
注意:演示模式包含一个捆绑的 SQLite 示例“员工”数据库,其中包含员工、部门、薪资等表格。
Claude Desktop
https://raw.githubusercontent.com/bytebase/dbhub/main/resources/images/claude-desktop.webp
- Claude Desktop 仅支持
stdio
传输模式 https://github.com/orgs/modelcontextprotocol/discussions/16
Cursor
https://raw.githubusercontent.com/bytebase/dbhub/main/resources/images/cursor.webp
- Cursor 支持
stdio
和sse
两种传输模式。
- 请按照 Cursor MCP 指南 操作,并确保使用 Agent 模式。
使用
只读模式
您可以在只读模式下运行 DBHub,该模式限制 SQL 查询执行为只读操作:
这为连接生产数据库提供了额外的安全层。
配置数据库连接
您可以使用 DBHub 的演示模式进行测试,该模式包含一个示例员工数据库:
对于真实数据库,需要提供数据库源名称 (DSN)。您可以通过以下几种方式提供:
- 命令行参数(最高优先级):
- 环境变量(第二优先级):
- 环境文件(第三优先级):
- 开发环境:创建
.env.local
并填写您的 DSN - 生产环境:创建
.env
并填写您的 DSN
[!WARNING] 在 Docker 中运行时,使用host.docker.internal
代替localhost
以连接到主机上的数据库。例如:mysql://user:password@host.docker.internal:3306/dbname
DBHub 支持以下数据库连接字符串格式:
数据库 | DSN 格式 | 示例 |
MySQL | mysql://[user]:[password]@[host]:[port]/[database] | mysql://user:password@localhost:3306/dbname |
MariaDB | mariadb://[user]:[password]@[host]:[port]/[database] | mariadb://user:password@localhost:3306/dbname |
PostgreSQL | postgres://[user]:[password]@[host]:[port]/[database] | postgres://user:password@localhost:5432/dbname?sslmode=disable |
SQL Server | sqlserver://[user]:[password]@[host]:[port]/[database] | sqlserver://user:password@localhost:1433/dbname |
SQLite | sqlite:///[path/to/file] 或 sqlite::memory: | sqlite:///path/to/database.db , sqlite:C:/Users/YourName/data/database.db (windows) 或 sqlite::memory: |
Oracle | oracle://[user]:[password]@[host]:[port]/[service_name] | oracle://username:password@localhost:1521/service_name |
Oracle
如果看到错误 "NJS-138: connections to this database server version are not supported by node-oracledb in Thin mode",您需要使用 Thick 模式,如下所述。
Docker
使用
bytebase/dbhub-oracle-thick
代替 bytebase/dbhub
的 Docker 镜像。npx
- 下载并安装适用于您平台的 Oracle Instant Client
- 将
ORACLE_LIB_DIR
环境变量设置为您的 Oracle Instant Client 路径:
SQL Server
额外的查询参数:
authentication
authentication=azure-active-directory-access-token
. 仅在从 Azure 运行时适用。请参阅 DefaultAzureCredential。
传输模式
- stdio(默认) - 用于与 Claude Desktop 等工具的直接集成:
- sse - 用于浏览器和网络客户端:
命令行选项
选项 | 描述 | 默认值 |
demo | 使用示例员工数据库运行演示模式 | false |
dsn | 数据库连接字符串 | 如果不使用演示模式则为必需 |
transport | 传输模式: stdio 或 sse | stdio |
port | HTTP 服务器端口(仅在 --transport=sse 时适用) | 8080 |
readonly | 限制 SQL 执行为只读操作 | false |
开发
- 安装依赖:
- 以开发模式运行:
- 构建生产版本:
使用 MCP Inspector 调试
stdio
SSE
连接到 DBHub 服务器的
/sse
端点贡献者
Star 历史
- Author:waytomcp
- URL:https://www.waytomcp.com/article/bytebase/dbhub
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!