Enables Claude Desktop to interact with MySQL, PostgreSQL, and Redis databases
🔌 MCP 数据库万能连接器
让 Claude Desktop 直接连接你的数据库,用自然语言查询和分析数据
NEW! 现在支持 HTTP API 模式,可在 Coze、n8n、Dify 等平台中使用
🎯 为什么使用本项目
作为开发者,你是否遇到过这些场景:
- 📊 临时数据分析:想快速查看生产数据库的某些指标,但不想写 SQL?
- 🔍 问题排查:需要跨多个表关联查询,但记不清表结构?
- 🤖 AI 辅助开发:希望 Claude 能直接理解你的数据库结构,生成准确的查询?
MCP 数据库万能连接器 通过 Model Context Protocol (MCP) 协议,让 Claude Desktop 成为你的数据库助手:
✅ 自然语言查询 - 用中文描述需求,Claude 自动生成并执行 SQL
✅ 智能表结构理解 - 自动获取数据库 Schema,提供精准建议
✅ 多数据库支持 - MySQL、PostgreSQL、Redis、Oracle、达梦、SQL Server、MongoDB、SQLite、KingbaseES、GaussDB/OpenGauss、OceanBase、TiDB、ClickHouse、PolarDB、Vastbase、HighGo、GoldenDB 一键切换
✅ 安全第一 - 默认只读模式,防止误操作删库
✅ 开箱即用 - 无需复杂配置,一行命令启动
✅ 智能缓存 - Schema 信息自动缓存,大幅提升大型数据库的响应速度
🌐 双模式支持
本项目支持两种运行模式:
1. MCP 模式(默认)
- 通过 stdio 协议与 Claude Desktop 通信
- 适用于本地开发和 Claude Desktop 集成
- 启动命令:
npm start或npm run start:mcp
2. HTTP API 模式(NEW!)
- 提供 REST API 接口
- 适用于 Coze、n8n、Dify 等第三方平台集成
- 支持 Docker、Serverless、PaaS 等多种部署方式
- 启动命令:
npm run start:http
快速切换模式:
# MCP 模式(Claude Desktop)
npm run start:mcp -- --type mysql --host localhost --port 3306 --user root --password xxx --database mydb
# HTTP API 模式(REST API)
MODE=http npm start
📖 详细文档:
- HTTP API 参考文档(中文) | English
- 部署指南(中文) | English
- Coze 集成指南(中文) | English
- n8n 集成指南(中文) | English
- Dify 集成指南(中文) | English
- 中文文档索引 - 所有中文文档导航
🚀 快速开始(HTTP API 模式)
1. 安装依赖
npm install -g universal-db-mcp
2. 配置环境变量
创建 .env 文件:
MODE=http
HTTP_PORT=3000
API_KEYS=your-secret-key
DB_TYPE=mysql
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=your_password
DB_DATABASE=your_database
3. 启动服务
npm run start:http
4. 测试 API
# 健康检查
curl http://localhost:3000/api/health
# 连接数据库
curl -X POST http://localhost:3000/api/connect \
-H "X-API-Key: your-secret-key" \
-H "Content-Type: application/json" \
-d '{"type":"mysql","host":"localhost","port":3306,"user":"root","password":"xxx","database":"test"}'
# 执行查询
curl -X POST http://localhost:3000/api/query \
-H "X-API-Key: your-secret-key" \
-H "Content-Type: application/json" \
-d '{"sessionId":"xxx","query":"SELECT * FROM users LIMIT 10"}'
5. Docker 部署
# 使用 Docker Compose
cd docker
docker-compose up -d
# 或使用 Docker 直接运行
docker build -t universal-db-mcp -f docker/Dockerfile .
docker run -p 3000:3000 \
-e MODE=http \
-e API_KEYS=your-key \
universal-db-mcp
🚀 快速开始(MCP 模式)
前置要求
- Node.js >= 20
- Claude Desktop 应用
- 至少一个数据库实例(MySQL/PostgreSQL/Redis/Oracle/达梦/SQL Server/MongoDB/SQLite/KingbaseES/GaussDB/OceanBase/TiDB/ClickHouse/PolarDB/Vastbase/HighGo/GoldenDB)
安装
npm install -g universal-db-mcp
或使用 npx 直接运行(无需安装):
npx universal-db-mcp
配置 Claude Desktop
编辑 Claude Desktop 配置文件:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
添加以下配置:
1. MySQL 示例
{
"mcpServers": {
"mysql-db": {
"command": "npx",
"args": [
"universal-db-mcp",
"--type", "mysql",
"--host", "localhost",
"--port", "3306",
"--user", "root",
"--password", "your_password",
"--database", "your_database"
]
}
}
}
2. PostgreSQL 示例
{
"mcpServers": {
"postgres-db": {
"command": "npx",
"args": [
"universal-db-mcp",
"--type", "postgres",
"--host", "localhost",
"--port", "5432",
"--user", "postgres",
"--password", "your_password",
"--database", "your_database"
]
}
}
}
3. Redis 示例
{
"mcpServers": {
"redis-db": {
"command": "npx",
"args": [
"universal-db-mcp",
"--type", "redis",
"--host", "localhost",
"--port", "6379",
"--password", "your_password"
]
}
}
}
注意:Redis 不需要 --database 参数,可以通过 --database 指定数据库编号(0-15)。
4. Oracle 示例
{
"mcpServers": {
"oracle-db": {
"command": "npx",
"args": [
"universal-db-mcp",
"--type", "oracle",
"--host", "localhost",
"--port", "1521",
"--user", "system",
"--password", "your_password",
"--database", "ORCL"
]
}
}
}
说明:
- Oracle 12c 及以上版本
- 默认端口为 1521
--database参数为服务名(Service Name)
Tools (2)
queryExecute SQL queries against the connected database to retrieve or analyze data.connectEstablish a connection to a specific database instance using provided credentials.Environment Variables
MODERunning mode (mcp or http)HTTP_PORTPort for HTTP API modeAPI_KEYSSecret keys for API authenticationDB_TYPErequiredType of database (mysql, postgres, redis, etc.)Configuration
{
"mcpServers": {
"mysql-db": {
"command": "npx",
"args": [
"universal-db-mcp",
"--type", "mysql",
"--host", "localhost",
"--port", "3306",
"--user", "root",
"--password", "your_password",
"--database", "your_database"
]
}
}
}