Universal DB MCP MCP Server

$npx universal-db-mcp
README.md

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 startnpm 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 模式)

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 mode
API_KEYSSecret keys for API authentication
DB_TYPErequiredType of database (mysql, postgres, redis, etc.)

Configuration

claude_desktop_config.json
{
  "mcpServers": {
    "mysql-db": {
      "command": "npx",
      "args": [
        "universal-db-mcp",
        "--type", "mysql",
        "--host", "localhost",
        "--port", "3306",
        "--user", "root",
        "--password", "your_password",
        "--database", "your_database"
      ]
    }
  }
}

Try it

Show me the schema for the users table and explain the relationships.
Find the top 10 customers by total order value from the last month.
List all keys in the Redis database that match the pattern 'user:session:*'.
Analyze the database structure and suggest an optimized query for finding inactive accounts.
Compare the data in the orders table between the production and staging databases.

Frequently Asked Questions

What are the key features of Universal DB MCP?

Natural language to SQL conversion and execution. Automatic database schema discovery and analysis. Support for 15+ database types including MySQL, PostgreSQL, Redis, and Oracle. Default read-only mode for secure data exploration. Dual-mode support: MCP for Claude Desktop and HTTP API for third-party integrations.

What can I use Universal DB MCP for?

Ad-hoc data analysis without writing manual SQL queries. Troubleshooting production issues by exploring complex table relationships. AI-assisted development for understanding legacy database schemas. Integrating database querying capabilities into LLM workflows like Coze or Dify.

How do I install Universal DB MCP?

Install Universal DB MCP by running: npx universal-db-mcp

What MCP clients work with Universal DB MCP?

Universal DB MCP works with any MCP-compatible client including Claude Desktop, Claude Code, Cursor, and other editors with MCP support.

Use Universal DB MCP with Conare

Manage MCP servers visually, upload persistent context, and never start from zero with Claude Code & Codex.

Try Free