用于查询数据库表结构的 Model Context Protocol 服务器
Project description
db-schema-mcp
一个用于查询数据库表结构的 Model Context Protocol (MCP) 服务器。该服务器允许 AI 助手(如 Claude Code、Cursor 等)通过标准化协议访问多种数据库类型的表结构信息。
功能特性
- 多数据库支持: SQLite、PostgreSQL、MySQL、Oracle
- 本地执行: 所有数据库操作在本地运行,数据绝不上传
- MCP 协议兼容: 可无缝集成到任何支持 MCP 的 AI 客户端
- 灵活配置: 通过 YAML 配置文件管理多个数据库连接
安装
使用 uv(推荐)
uvx db-schema-mcp --config /path/to/your/db_config.yaml
使用 pip
pip install db-schema-mcp
db-schema-mcp --config /path/to/your/db_config.yaml
配置
创建 YAML 配置文件(例如 db_config.yaml):
databases:
# MySQL 配置
mysql_production:
type: mysql
host: localhost
port: 3306
user: your_username
password: your_password
database: production_db
# 可选: SSL 配置
ssl: true
ssl_ca: /path/to/ca.pem
ssl_cert: /path/to/cert.pem
ssl_key: /path/to/key.pem
# PostgreSQL 配置
postgres_analytics:
type: postgresql
host: localhost
port: 5432
user: postgres
password: your_password
database: analytics_db
# 可选: SSL 配置
sslmode: require
# SQLite 配置
sqlite_local:
type: sqlite
path: /path/to/your/database.db
MCP 客户端配置
将以下配置添加到您的 MCP 客户端配置(例如 Claude Desktop 的 claude_desktop_config.json):
{
"mcpServers": {
"db-schema": {
"command": "uvx",
"args": [
"db-schema-mcp",
"--config", "/path/to/your/db_config.yaml"
]
}
}
}
可用工具
list-connections
列出所有已配置的数据库连接。
参数:
check_status(可选): 验证连接可用性
示例:
您: 我有哪些可用的数据库连接?
AI: 以下是您配置的连接:
1. mysql_production - MySQL @ localhost:3306
2. postgres_analytics - PostgreSQL @ localhost:5432
3. sqlite_local - SQLite @ /path/to/database.db
list-tables
列出指定数据库中的所有表。
参数:
connection_name(必需): 数据库连接名称
示例:
您: 列出 analytics 数据库中的所有表
AI: postgres_analytics 中的表如下:
- customers(客户)
- products(产品)
- orders(订单)
- inventory(库存)
- order_items(订单明细)
describe-table
获取指定表的详细结构信息。
参数:
connection_name(必需): 数据库连接名称table_name(必需): 表名
示例:
您: customers 表的结构是什么样的?
AI: customers 表有以下结构:
| 字段名 | 类型 | 是否可为空 | 默认值 | 说明 |
|--------|------|------------|--------|------|
| id | INTEGER | NO | | 主键 |
| name | VARCHAR(255) | NO | | 客户姓名 |
| email | VARCHAR(255) | YES | NULL | 电子邮箱 |
| registration_date | DATE | NO | | 注册日期 |
| status | VARCHAR(50) | YES | 'active' | 客户状态 |
安全性
- 所有数据库操作在本地执行
- 没有数据传输到外部服务
- 数据库凭证存储在本地配置文件中
- 仅访问元数据(表结构),不访问实际数据
开发
搭建开发环境
# 克隆仓库
git clone https://github.com/soberw/db-schema-mcp.git
cd db-schema-mcp
# 以可编辑模式安装
uv pip install -e ".[dev]"
运行测试
1. 单元测试
使用 pytest 运行单元测试:
# 运行所有测试
pytest
# 运行测试并显示覆盖率
pytest --cov=db_schema_mcp --cov-report=html
# 运行特定测试文件
pytest tests/test_config.py
# 运行测试并显示详细输出
pytest -v
2. 集成测试
使用 run_tests.py 进行集成测试:
# 1. 首先创建测试配置文件
cp config.example.yaml tests/test_config.yaml
# 2. 编辑 tests/test_config.yaml,配置数据库连接
# 3. 创建测试数据库和表
python run_tests.py setup
# 4. 列出所有数据库连接
python run_tests.py list
# 5. 列出指定数据库的表
python run_tests.py tables <连接名>
# 6. 查看表结构
python run_tests.py desc <连接名> <表名>
# 7. 进入交互模式
python run_tests.py interactive
交互模式可用命令:
list- 列出所有数据库连接check- 检查所有连接状态tables <连接名>- 列出表desc <连接名> <表名>- 显示表结构quit或exit- 退出
3. 代码质量检查
# 运行类型检查
mypy src/
# 格式化代码
ruff check src/ --fix
# 仅检查不修改
ruff check src/
数据库支持
| 数据库 | 驱动 | 状态 |
|---|---|---|
| SQLite | sqlite3 (内置) | ✅ 完全支持 |
| PostgreSQL | psycopg3 | ✅ 完全支持 |
| MySQL | aiomysql | ✅ 完全支持 |
| Oracle | oracledb | ✅ 支持 |
许可证
MIT 许可证 - 详见 LICENSE 文件。
贡献
欢迎贡献!请随时提交 Pull Request。
故障排查
连接错误
如果遇到连接错误:
- 验证配置文件路径是否正确
- 检查数据库凭证和主机/端口设置
- 确保数据库可从您的机器访问
- 查看错误消息了解具体详情
SSL/TLS 问题
对于需要 SSL 的数据库:
- 在 YAML 配置中包含 SSL 配置
- 确保证书文件可访问
- 验证 SSL 模式设置与数据库要求匹配
缺少依赖
如果遇到导入错误:
pip install --upgrade db-schema-mcp
或用于开发:
uv pip install -e ".[dev]"
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 db_schema_mcp-1.0.0.tar.gz.
File metadata
- Download URL: db_schema_mcp-1.0.0.tar.gz
- Upload date:
- Size: 28.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
398459c8362a1e4032582326b6a1a6e0b46c76b00bfde7dfdd93ea9f6cfb072d
|
|
| MD5 |
b4443b6d62270e62a0a6c0cb0fb18782
|
|
| BLAKE2b-256 |
7d37f6b0234beb436cc78f8728a26f89db9903794fc5d05d5b8da26dbd27bd8c
|
Provenance
The following attestation bundles were made for db_schema_mcp-1.0.0.tar.gz:
Publisher:
publish.yml on soberw/db-schema-mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
db_schema_mcp-1.0.0.tar.gz -
Subject digest:
398459c8362a1e4032582326b6a1a6e0b46c76b00bfde7dfdd93ea9f6cfb072d - Sigstore transparency entry: 844606028
- Sigstore integration time:
-
Permalink:
soberw/db-schema-mcp@45282c5982f53557943c67e4a36b7f8a6662a32f -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/soberw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@45282c5982f53557943c67e4a36b7f8a6662a32f -
Trigger Event:
push
-
Statement type:
File details
Details for the file db_schema_mcp-1.0.0-py3-none-any.whl.
File metadata
- Download URL: db_schema_mcp-1.0.0-py3-none-any.whl
- Upload date:
- Size: 27.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 |
a0c94dbb05c47c634658da54109dfa8ae15ff06555549312ad9527d44a58938f
|
|
| MD5 |
ec86b18ad0f2c3798be4891dba0ac106
|
|
| BLAKE2b-256 |
bccd7590a5b534448861c11e389541c78fa6311a29d3b6d755daebb30ea3874c
|
Provenance
The following attestation bundles were made for db_schema_mcp-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on soberw/db-schema-mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
db_schema_mcp-1.0.0-py3-none-any.whl -
Subject digest:
a0c94dbb05c47c634658da54109dfa8ae15ff06555549312ad9527d44a58938f - Sigstore transparency entry: 844606031
- Sigstore integration time:
-
Permalink:
soberw/db-schema-mcp@45282c5982f53557943c67e4a36b7f8a6662a32f -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/soberw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@45282c5982f53557943c67e4a36b7f8a6662a32f -
Trigger Event:
push
-
Statement type: