mcp服务器数据库
MCP(模型上下文协议)服务器,提供与关系数据库交互的工具。支持PostgreSQL、MySQL和SQLite。
建筑
graph TB
subgraph Client
style Client fill:#4A90D9,stroke:#2E6BA6,color:#FFFFFF
MCP_Client["MCP Client"]
end
subgraph Server["mcp-server-database"]
style Server fill:#2ECC71,stroke:#1A9B52,color:#FFFFFF
Index["index.ts
MCP Server Entry"]
subgraph Tools
style Tools fill:#F39C12,stroke:#C67D0A,color:#FFFFFF
T1["query"]
T2["list_tables"]
T3["describe_table"]
T4["list_databases"]
T5["execute_migration"]
T6["get_schema"]
end
end
subgraph Databases["Database Engines"]
style Databases fill:#9B59B6,stroke:#7D3C98,color:#FFFFFF
PG["PostgreSQL"]
MY["MySQL"]
SQ["SQLite"]
end
MCP_Client -- "stdio transport" --> Index
Index --> T1 & T2 & T3 & T4 & T5 & T6
T1 --> PG & MY & SQ
T2 --> PG & MY & SQ
T3 --> PG & MY & SQ
T4 --> PG & MY
T5 --> PG & MY & SQ
T6 --> PG & MY & SQ安装
npm install
npm run build配置
| 变量 | 描述 | 必填 |
|---|---|---|
DB_DRIVER | 数据库驱动程序: postgres, mysql,或 sqlite | 是的 |
DB_HOST | 数据库主机 | 是(postgres/mysql) |
DB_PORT | 数据库端口 | 否 |
DB_USER | 数据库用户名 | 是(postgres/mysql) |
DB_PASSWORD | 数据库密码 | 是(postgres/mysql) |
DB_NAME | 数据库名称 | 是 |
DB_PATH | SQLite文件的路径 | 是(SQLite) |
用法
独立
DB_DRIVER=postgres DB_HOST=localhost DB_USER=admin DB_PASSWORD=secret DB_NAME=mydb npm start发展
npm run dev码头工人
docker build -t mcp-server-database .
docker run -e DB_DRIVER=postgres -e DB_HOST=host.docker.internal -e DB_USER=admin -e DB_PASSWORD=secret -e DB_NAME=mydb mcp-server-databaseMCP客户端配置
{
"mcpServers": {
"database": {
"command": "node",
"args": ["dist/index.js"],
"env": {
"DB_DRIVER": "postgres",
"DB_HOST": "localhost",
"DB_PORT": "5432",
"DB_USER": "admin",
"DB_PASSWORD": "secret",
"DB_NAME": "mydb"
}
}
}
}工具参考
| 工具 | 说明 | 参数 |
|---|---|---|
query | 执行SQL查询 | sql, params? |
list_tables | 列出所有表 | 无 |
describe_table | 描述表模式 | table_name |
list_databases | 列出服务器上的数据库 | 无 |
execute_migration | 执行迁移SQL脚本 | sql |
get_schema | 获取完整的数据库架构DDL | 无 |
许可证
麻省理工学院
