使用说明
mcp-db通用
数据库无关 MCP(模型上下文协议)服务器 它允许您通过Claude或GitHub Copilot与数据库聊天。用简单的英语提问,AI将为您编写和执行SQL。
支持的数据库
| 数据库 | 驱动程序 | 安装 |
|---|---|---|
| PostgreSQL | pg |
npm install pg |
| MySQL | mysql2 |
npm install mysql2 |
| SQL Server | mssql |
npm install mssql |
| SQLite | better-sqlite3 |
npm install better-sqlite3 |
| Oracle | oracledb |
npm install oracledb |
快速开始
0.先决条件——安装Node.js
此工具运行于 Node.js。如果您没有安装它,请先安装:
- 首选 nodejs.org 并下载 长期支持 版本(推荐给大多数用户)
- 运行安装程序并按照提示操作
- 安装后,打开终端(Windows上的命令提示符/PowerShell,macOS/Linux上的终端)并验证其是否正常工作:
node --version # should print something like v20.x.x
npm --version # should print something like 10.x.x
如果两个命令都返回版本号,则可以继续
1.全局安装
npm install -g fm-db-mcp-universal
# Then install your DB driver:
npm install -g pg # PostgreSQL
npm install -g mysql2 # MySQL
npm install -g mssql tedious # SQL Server
npm install -g better-sqlite3 # SQLite
npm install -g oracledb # Oracle
2.连接到克劳德桌面
编辑 ~/Library/Application Support/Claude/claude_desktop_config.json (macOS)或 C:\Users\<username>\AppData\Roaming\Claude\claude_desktop_config.json (Windows):
{
"mcpServers": {
"my-db": {
"command": "npx",
"args": ["fm-db-mcp-universal"],
"env": {
"DB_CLIENT": "oracle",
"DB_HOST": "localhost",
"DB_PORT": "1521",
"DB_NAME": "orcl",
"DB_USER": "xxx",
"DB_PASSWORD": "xxx"
}
}
}
}
3.连接VS Code(GitHub复制代理模式)
创建 .vscode/mcp.json 在您的工作空间中:
{
"servers": {
"oracle-db": {
"command": "npx",
"args": [
"fm-db-mcp-universal"
],
"env": {
"DB_CLIENT": "oracle",
"DB_HOST": "localhost",
"DB_PORT": "1521",
"DB_NAME": "orcl",
"DB_USER": "FMCPROD1",
"DB_PASSWORD": "FMCPROD1"
}
},
"postgres-db": {
"command": "npx",
"args": [
"fm-db-mcp-universal"
],
"env": {
"DB_CLIENT": "postgres",
"DB_HOST": "localhost",
"DB_PORT": "5432",
"DB_USER": "postgres",
"DB_PASSWORD": "password",
"DB_NAME": "postgres"
}
},
"mssql-db": {
"command": "npx",
"args": [
"fm-db-mcp-universal"
],
"env": {
"DB_CLIENT": "mssql",
"DB_HOST": "localhost",
"DB_PORT": "1433",
"DB_USER": "sa",
"DB_PASSWORD": "sadmin",
"DB_NAME": "ALLMERGE"
}
},
}
}
配置
所有配置都是通过环境变量进行的:
| 变量 | 描述 | 默认值 |
|---|---|---|
DB_CLIENT |
数据库类型: postgres, mysql, mssql, sqlite, oracle |
postgres |
DB_HOST |
数据库主机 | localhost |
DB_PORT |
数据库端口(如果未设置,则自动检测) | 每个数据库 |
DB_USER |
用户名 | -- |
DB_PASSWORD |
密码 | -- |
DB_NAME |
数据库名称 | -- |
DB_FILENAME |
文件路径(仅限SQLite) | -- |
DB_CONNECTION_STRING |
完整连接字符串(覆盖上述内容) | -- |
DB_READONLY |
设置 true 禁用写入/DDL |
false |
DB_POOL_MIN |
连接池最小值 | 1 |
DB_POOL_MAX |
连接池最大值 | 5 |
使用连接字符串
{
"env": {
"DB_CLIENT": "postgres",
"DB_CONNECTION_STRING": "postgresql://user:pass@localhost:5432/mydb"
}
}
数据库特定示例
PostgreSQL
{
"DB_CLIENT": "postgres",
"DB_HOST": "localhost",
"DB_USER": "postgres",
"DB_PASSWORD": "secret",
"DB_NAME": "myapp"
}
MySQL/MariaDB
{
"DB_CLIENT": "mysql",
"DB_HOST": "localhost",
"DB_USER": "root",
"DB_PASSWORD": "secret",
"DB_NAME": "myapp"
}
SQL Server(MSSQL)
{
"DB_CLIENT": "mssql",
"DB_HOST": "localhost",
"DB_PORT": "1433",
"DB_USER": "sa",
"DB_PASSWORD": "YourPassword123!",
"DB_NAME": "Northwind"
}
SQLite
{
"DB_CLIENT": "sqlite",
"DB_FILENAME": "/path/to/database.db"
}
甲骨文
{
"DB_CLIENT": "oracle",
"DB_HOST": "localhost",
"DB_PORT": "1521",
"DB_USER": "myuser",
"DB_PASSWORD": "mypassword",
"DB_NAME": "ORCL"
}
可用的MCP工具
| 工具 | 说明 |
|---|---|
db_ping |
测试连接并确认其正常工作 |
db_query |
运行SELECT查询(只读,自动限制为100行) |
db_execute |
运行INSERT/UPDATE/DELETE/DDL(在只读模式下禁用) |
db_list_tables |
列出所有表和视图 |
db_describe_table |
获取表的列、类型和可空性 |
db_table_indexes |
获取表的索引信息 |
db_foreign_keys |
获取表的外键关系 |
db_schema_snapshot |
一次完整转储所有表的架构 |
聊天互动示例
一旦连接,就自然地说话:
“我有什么桌子?” → Calls
db_list_tables
“显示过去30天内注册的所有用户” → 写入并执行带有日期筛选器的SELECT
“有多少订单处于‘待处理’状态?” → COUNT查询
“订单表的模式是什么?” → Calls
db_describe_table
“为users.email添加索引” → Calls
db_execute使用CREATE INDEX语句
“给我一份今年按地区划分的销售额摘要” → 带聚合的GROUP BY查询
只读模式
要保护生产数据库,请启用只读模式:
{
"env": {
"DB_READONLY": "true",
...
}
}
在只读模式下, db_execute 已禁用--只允许SELECT查询。
从源头运行
git clone https://github.com/yourname/mcp-db-universal
cd mcp-db-universal
npm install
npm install better-sqlite3 # or your DB driver
npm run build
然后配置Claude/Copilot以使用:
{
"command": "node",
"args": ["/path/to/mcp-db-universal/dist/index.js"],
"env": { ... }
}
或者直接在开发模式下运行:
DB_CLIENT=sqlite DB_FILENAME=./test.db npm run dev
发布到npm
npm run build
npm publish
然后用户可以使用 npx:
{
"command": "npx",
"args": ["mcp-db-universal"],
"env": { ... }
}
许可证
麻省理工学院