A multi-database MCP server supporting multiple remote databases
Project description
MultiDB MCP Server
支持多个远程数据库的 MCP (Model Context Protocol) 服务。采用无状态设计,每次调用时指定要操作的数据库。
功能特性
- 🔄 同时连接多个数据库(MySQL/PostgreSQL)
- 🎯 无状态设计 - 无需维护连接状态
- 🔍 查询、表结构查看、数据库管理
- 🛡️ 配置文件管理连接信息
安装
推荐方式:使用 uvx
uvx --from . multidb-mcp
其他方式
使用 uv:
uv venv && source .venv/bin/activate
uv pip install -e .
multidb-mcp
使用 pip:
pip install -e .
multidb-mcp
配置
创建 config.json 文件,示例如下:
{
"databases": {
"dev1": {
"type": "mysql",
"host": "localhost",
"port": 3306,
"user": "root",
"password": "password",
"database": "dev_db"
},
"test": {
"type": "postgresql",
"host": "localhost",
"port": 5432,
"user": "postgres",
"password": "password",
"database": "test_db"
}
}
}
可复制 config.example.json 作为起点:
cp config.example.json config.json # 编辑后填入实际连接信息
配置文件路径优先级
- 命令行参数:
--config /path/to/config.json - 环境变量:
DATABASE_CONFIG_PATH=/path/to/config.json - 默认路径:
./config.json
使用
启动服务
# 使用默认配置文件
multidb-mcp
# 使用自定义配置文件
multidb-mcp --config /path/to/config.json
# 使用环境变量
export DATABASE_CONFIG_PATH=/path/to/config.json && multidb-mcp
# 开发模式
fastmcp dev multidb_mcp/server.py
运行演示
python demo.py
MCP 工具
1. list_databases
列出所有已配置的数据库。
2. execute_query
在指定数据库上执行 SQL 查询。
| 参数 | 类型 | 说明 |
|---|---|---|
connection_name |
string | 配置文件中的数据库连接名称 |
query |
string | SQL 查询语句 |
3. list_tables
列出指定数据库中的所有表。
| 参数 | 类型 | 说明 |
|---|---|---|
connection_name |
string | 配置文件中的数据库连接名称 |
4. describe_table
查看表结构详情(字段、类型、约束等)。
| 参数 | 类型 | 说明 |
|---|---|---|
connection_name |
string | 配置文件中的数据库连接名称 |
table_name |
string | 表名 |
使用场景示例
场景 1: 对比不同环境的数据(无状态)
1. list_databases() - 查看所有可用的数据库
2. execute_query("dev1", "SELECT * FROM users WHERE id = 123") - 查询开发环境数据
3. execute_query("production", "SELECT * FROM users WHERE id = 123") - 查询生产环境数据
4. 对比两次查询结果
场景 2: 数据同步(无状态)
1. execute_query("production", "SELECT * FROM products WHERE category = 'new'") - 获取生产数据
2. execute_query("test", "INSERT INTO products ...") - 插入数据到测试库
场景 3: 数据库结构对比(无状态)
1. describe_table("dev1", "users") - 查看开发环境的表结构
2. describe_table("production", "users") - 查看生产环境的表结构
3. 对比两个环境的表结构差异
开发
运行测试
# 安装开发依赖
uv pip install pytest pytest-asyncio
# 运行测试
pytest tests/ -v
安全注意事项
- 不要提交配置文件:
config.json包含敏感的数据库凭证,确保已经添加到.gitignore - 使用只读账户: 对于生产数据库,建议使用只读权限的账户
- 网络安全: 确保数据库服务器有适当的防火墙规则
- 密码安全: 使用强密码,考虑使用环境变量或密钥管理服务
- SQL 注入风险:
execute_query工具直接执行 SQL 语句,存在 SQL 注入风险。仅在可信环境下使用,不要暴露给不可信的用户输入
许可证
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
multidb_mcp-0.2.0.tar.gz
(129.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file multidb_mcp-0.2.0.tar.gz.
File metadata
- Download URL: multidb_mcp-0.2.0.tar.gz
- Upload date:
- Size: 129.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9650da940cf2f36473675bad2e6c6ccb4c7f90d514a03512da9efcb920a5cabf
|
|
| MD5 |
03bdbfcdb2009f6617a4018d42107be4
|
|
| BLAKE2b-256 |
7f782e018b86ca65352a2b1f8270874595bed7abe53edfd87575582fbb485cc5
|
File details
Details for the file multidb_mcp-0.2.0-py3-none-any.whl.
File metadata
- Download URL: multidb_mcp-0.2.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80466c6cd557c4d16c049300f21e15b088c6fb53fd44125c832629102a00f888
|
|
| MD5 |
131c3bea47a24d792c37433d9f6cc244
|
|
| BLAKE2b-256 |
21ccc807a0c83c3351cb7a93982208585757757ad58d1420828982a9a57ee644
|