MCP tool for connecting to MySQL, PostgreSQL, Redis and MongoDB databases
Project description
Poly Query MCP
Poly Query MCP 是一个支持多种数据库查询的MCP(Model Context Protocol)工具,允许您通过MCP协议查询MySQL、PostgreSQL、Redis和MongoDB数据库。
功能特性
- 🔌 多数据库支持: 支持MySQL、PostgreSQL、Redis和MongoDB
- ⚙️ 灵活配置: 支持配置文件和环境变量配置
- 🛡️ 错误处理: 完善的错误处理和异常管理
- 📝 日志记录: 结构化的日志记录系统
- 🔧 MCP协议: 完全兼容MCP协议,可与Claude等AI助手集成
快速开始
安装
使用uv安装(推荐)
# 从PyPI安装
uv add poly-query-mcp
# 或者从GitHub安装
uv add git+https://github.com/yourusername/poly-query-mcp.git
# 或者使用uvx直接运行
uvx poly-query-mcp
传统安装方式
- 克隆仓库
git clone <repository-url>
cd poly_query_mcp
- 创建虚拟环境
python -m venv .venv
.venv\Scripts\activate # Windows
source .venv/bin/activate # Linux/Mac
- 安装依赖
pip install -r requirements.txt
配置
- 复制示例配置文件
cp config.json config.json
- 编辑配置文件,添加您的数据库连接信息
使用
启动MCP服务器:
# 使用uvx
uvx poly-query-mcp
# 或者传统方式
python main.py
mcp接入使用:
- 在支持mcp client中(如Claude Desktop)的配置文件中添加此MCP服务器(参考下面完整的mcp的数据库配置):
{
"mcpServers": {
"poly-query-mcp": {
"command": "uvx",
"args": [
"poly-query-mcp",
"--mysql-host", "localhost",
"--mysql-port", "3306",
"--mysql-user", "root",
"--mysql-password", "your_mysql_password",
"--mysql-database", "your_mysql_database"
]
}
}
}
- 重启mcp服务
- 在对话中使用数据库查询功能
更多安装和配置选项请参考 uv安装指南
数据库配置
Poly Query MCP 支持多种配置方式,包括传统配置文件、增强配置文件和环境变量。您可以根据需要选择最适合的配置方式。
1. 直接传入数据库参数
{
"mcpServers": {
"poly-query-mcp": {
"command": "uvx",
"args": [
"poly-query-mcp",
"--mysql-host", "localhost",
"--mysql-port", "3306",
"--mysql-user", "root",
"--mysql-password", "password",
"--mysql-database", "mysql_database",
"--postgresql-host", "localhost",
"--postgresql-port", "5432",
"--postgresql-user", "postgres",
"--postgresql-password", "password",
"--postgresql-database", "postgresql_database",
"--redis-host", "localhost",
"--redis-port", "6379",
"--redis-db", "0",
"--redis-password", "redis_password",
"--mongodb-host", "localhost",
"--mongodb-port", "27017",
"--mongodb-username", "dev_user",
"--mongodb-password", "dev_mongodb_password",
"--mongodb-database", "test"
]
}
}
}
2. 传统配置文件
创建一个 config.json 文件,包含所有数据库的连接信息:
{
"mysql": {
"host": "localhost",
"port": 3306,
"user": "root",
"password": "your_mysql_password",
"database": "your_mysql_database",
"charset": "utf8mb4"
},
"postgresql": {
"host": "localhost",
"port": 5432,
"user": "postgres",
"password": "your_postgresql_password",
"database": "your_postgresql_database",
"pg_schema": "public"
},
"redis": {
"host": "localhost",
"port": 6379,
"db": 0,
"password": "your_redis_password"
},
"mongodb": {
"host": "localhost",
"port": 27017,
"username": "your_mongodb_username",
"password": "your_mongodb_password",
"database": "your_mongodb_database"
}
}
mcp 配置为:
{
"mcpServers": {
"poly-query-mcp": {
"command": "uvx",
"args": [
"poly-query-mcp",
"--config",
"/path/to/config.json",
]
}
}
}
3. 增强配置文件(推荐)
增强配置文件支持多环境配置和配置文件,适合复杂的部署场景。创建 config.enhanced.json 文件:
{
"environments": {
"development": {
"mysql": {
"host": "localhost",
"port": 3306,
"user": "root",
"password": "dev_mysql_password",
"database": "dev_db",
"charset": "utf8mb4"
},
"postgresql": {
"host": "localhost",
"port": 5432,
"user": "postgres",
"password": "dev_postgresql_password",
"database": "dev_db",
"pg_schema": "public"
},
"redis": {
"host": "localhost",
"port": 6379,
"db": 0,
"password": "dev_redis_password"
},
"mongodb": {
"host": "localhost",
"port": 27017,
"username": "dev_user",
"password": "dev_mongodb_password",
"database": "dev_db"
}
},
"production": {
"mysql": {
"host": "prod-mysql.example.com",
"port": 3306,
"user": "app_user",
"password": "${MYSQL_PROD_PASSWORD}",
"database": "production_db",
"charset": "utf8mb4"
},
"postgresql": {
"host": "prod-postgresql.example.com",
"port": 5432,
"user": "app_user",
"password": "${POSTGRESQL_PROD_PASSWORD}",
"database": "production_db",
"pg_schema": "public"
},
"redis": {
"host": "prod-redis.example.com",
"port": 6379,
"db": 0,
"password": "${REDIS_PROD_PASSWORD}"
},
"mongodb": {
"host": "prod-mongodb.example.com",
"port": 27017,
"username": "app_user",
"password": "${MONGODB_PROD_PASSWORD}",
"database": "production_db"
}
}
},
"profiles": {
"local": {
"environment": "development",
"description": "本地开发环境配置"
},
"staging": {
"environment": "production",
"description": "预发布环境配置"
},
"minimal": {
"environment": "development",
"databases": ["mysql"],
"description": "仅MySQL数据库的轻量级配置"
}
},
"defaults": {
"environment": "development",
"log_level": "INFO",
"connection_pool_size": 5,
"connection_timeout": 30
}
}
mcp的配置为:
{
"mcpServers": {
"poly-query-mcp": {
"command": "uvx",
"args": [
"poly-query-mcp",
"--config-enhanced",
"/path/to/config.enhanced.json",
"--environment",
"development"
]
}
}
}
4. 数据库配置详解
MySQL 配置
| 参数 | 类型 | 必需 | 默认值 | 说明 |
|---|---|---|---|---|
| host | string | 是 | - | MySQL服务器地址 |
| port | integer | 否 | 3306 | MySQL服务器端口 |
| user | string | 是 | - | MySQL用户名 |
| password | string | 是 | - | MySQL密码 |
| database | string | 是 | - | MySQL数据库名 |
| charset | string | 否 | utf8mb4 | 字符集 |
使用场景:
- 适用于需要ACID事务支持的关系型数据存储
- 适用于结构化数据,如用户信息、订单数据等
注意事项:
- 确保MySQL用户有足够的权限访问指定的数据库
- 对于生产环境,建议使用SSL连接
- 字符集建议使用utf8mb4以支持完整的Unicode字符集
PostgreSQL 配置
| 参数 | 类型 | 必需 | 默认值 | 说明 |
|---|---|---|---|---|
| host | string | 是 | - | PostgreSQL服务器地址 |
| port | integer | 否 | 5432 | PostgreSQL服务器端口 |
| user | string | 是 | - | PostgreSQL用户名 |
| password | string | 是 | - | PostgreSQL密码 |
| database | string | 是 | - | PostgreSQL数据库名 |
| pg_schema | string | 否 | public | PostgreSQL模式名 |
使用场景:
- 适用于需要复杂查询和数据分析的场景
- 适用于需要高级数据类型和扩展功能的应用
注意事项:
- 确保PostgreSQL用户有足够的权限访问指定的数据库和模式
- 对于生产环境,建议配置连接池以优化性能
- 可以通过pg_schema参数实现多租户隔离
Redis 配置
| 参数 | 类型 | 必需 | 默认值 | 说明 |
|---|---|---|---|---|
| host | string | 是 | - | Redis服务器地址 |
| port | integer | 否 | 6379 | Redis服务器端口 |
| password | string | 否 | - | Redis密码(可选) |
| db | integer | 否 | 0 | Redis数据库索引(0-15) |
使用场景:
- 适用于缓存、会话存储、消息队列等场景
- 适用于需要高性能读写的数据
注意事项:
- Redis数据库索引范围为0-15,确保指定的索引存在
- 对于生产环境,建议配置密码认证
- 考虑使用Redis集群以提高可用性
MongoDB 配置
| 参数 | 类型 | 必需 | 默认值 | 说明 |
|---|---|---|---|---|
| host | string | 是 | - | MongoDB服务器地址 |
| port | integer | 否 | 27017 | MongoDB服务器端口 |
| username | string | 否 | - | MongoDB用户名 |
| password | string | 否 | - | MongoDB密码 |
| database | string | 是 | - | MongoDB数据库名 |
使用场景:
- 适用于文档型数据存储,如日志、配置等
- 适用于需要灵活数据结构的场景
注意事项:
- 确保MongoDB用户有足够的权限访问指定的数据库
- 对于生产环境,建议配置副本集以提高可用性
- 考虑使用索引优化查询性能
5. 安全注意事项
-
密码保护:
- 不要在代码中硬编码密码
- 使用环境变量或配置文件存储敏感信息
- 对于生产环境,考虑使用密钥管理服务
-
网络安全:
- 在生产环境中使用SSL/TLS连接
- 限制数据库访问的网络范围
- 使用防火墙规则限制数据库访问
-
权限控制:
- 为应用程序创建专用的数据库用户
- 仅授予必要的最小权限
- 定期轮换数据库密码
8. 故障排除
常见问题
-
连接超时:
- 检查数据库服务器是否运行
- 验证网络连接和防火墙设置
- 确认主机地址和端口是否正确
-
认证失败:
- 验证用户名和密码是否正确
- 检查用户是否有访问指定数据库的权限
- 确认认证插件是否匹配
-
数据库不存在:
- 确认数据库名称是否正确
- 检查用户是否有创建数据库的权限
- 验证数据库是否已创建
支持的数据库
- MySQL
- PostgreSQL
- Redis
- MongoDB
文档
贡献
欢迎贡献!请查看 贡献指南 了解如何参与项目。
许可证
本项目采用 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
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 poly_query_mcp-0.1.0.5.tar.gz.
File metadata
- Download URL: poly_query_mcp-0.1.0.5.tar.gz
- Upload date:
- Size: 72.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
691ad8dba638525a188fb055b09762ee2ee3fe40ce2d630e4e88ad99a1c9d6e9
|
|
| MD5 |
6825d2e1ac1b4105ab4d4ec344f30b28
|
|
| BLAKE2b-256 |
30235851536e90253357e88c6d96f2f7ca70c301ae3b43c1775d58d6ee72c44f
|
Provenance
The following attestation bundles were made for poly_query_mcp-0.1.0.5.tar.gz:
Publisher:
publish.yml on hqzqaq/poly_query_mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
poly_query_mcp-0.1.0.5.tar.gz -
Subject digest:
691ad8dba638525a188fb055b09762ee2ee3fe40ce2d630e4e88ad99a1c9d6e9 - Sigstore transparency entry: 651324777
- Sigstore integration time:
-
Permalink:
hqzqaq/poly_query_mcp@ae11ce4482ebecaff9231e932c40491b08e6b6b1 -
Branch / Tag:
refs/tags/v1.0.5 - Owner: https://github.com/hqzqaq
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ae11ce4482ebecaff9231e932c40491b08e6b6b1 -
Trigger Event:
release
-
Statement type:
File details
Details for the file poly_query_mcp-0.1.0.5-py3-none-any.whl.
File metadata
- Download URL: poly_query_mcp-0.1.0.5-py3-none-any.whl
- Upload date:
- Size: 33.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2685bddd3c93c87e54ccdb9f9048d4b5a8a354d6b38bac6280c85041371df420
|
|
| MD5 |
37c374b8f25e086b3350d4c03b96c7d6
|
|
| BLAKE2b-256 |
ce29fcca6af8b755eb922c79596657c12ef1d613d0597697e228064648faad39
|
Provenance
The following attestation bundles were made for poly_query_mcp-0.1.0.5-py3-none-any.whl:
Publisher:
publish.yml on hqzqaq/poly_query_mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
poly_query_mcp-0.1.0.5-py3-none-any.whl -
Subject digest:
2685bddd3c93c87e54ccdb9f9048d4b5a8a354d6b38bac6280c85041371df420 - Sigstore transparency entry: 651324781
- Sigstore integration time:
-
Permalink:
hqzqaq/poly_query_mcp@ae11ce4482ebecaff9231e932c40491b08e6b6b1 -
Branch / Tag:
refs/tags/v1.0.5 - Owner: https://github.com/hqzqaq
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ae11ce4482ebecaff9231e932c40491b08e6b6b1 -
Trigger Event:
release
-
Statement type: