database-mcp
多数据库 MCP 服务器(由 Legion AI 提供)
该服务器使用集成了模型上下文协议 (MCP) Python SDK 的 Legion Query Runner 帮助人们访问和查询数据库中的数据。
从这里开始生成
此工具由Legion AI提供。如需使用功能齐全、功能强大的 AI 数据分析工具,请访问该网站。如果您需要我们支持某个数据库,请发送电子邮件联系我们。
终结此世代
为什么选择数据库 MCP
数据库 MCP 与其他数据库访问解决方案相比脱颖而出,有几个令人信服的原因:
统一的多数据库接口:通过单一一致的 API 连接到 PostgreSQL、MySQL、SQL Server 和其他数据库 - 无需为每种数据库类型学习不同的客户端库。
AI-Ready 集成:通过模型上下文协议 (MCP) 专为 AI 助手交互而构建,支持自然语言数据库操作。
零配置模式发现:无需手动配置或映射即可自动发现和公开数据库模式。
与数据库无关的工具:无论底层数据库技术如何,都可以使用同一组工具查找表、探索模式和执行查询。
安全凭证管理:安全地处理数据库身份验证详细信息,将凭证与应用程序代码分开。
简单部署:只需最少的设置即可与 LangChain、FastAPI 等现代 AI 开发环境配合使用。
可扩展设计:轻松添加自定义工具和提示以增强特定用例的功能。
无论您构建的是需要数据库访问的 AI 代理,还是仅仅想要一个统一的多个数据库接口,数据库 MCP 都能提供简化的解决方案,大大减少开发时间和复杂性。
Related MCP server: MCP Database Server
特征
多数据库支持——同时连接多个数据库
通过 Legion Query Runner 访问数据库
人工智能助手的模型上下文协议 (MCP) 支持
将数据库操作公开为 MCP 资源、工具和提示
多种部署选项(独立 MCP 服务器、FastAPI 集成)
查询执行和结果处理
通过环境变量、命令行参数或 MCP 设置 JSON 进行灵活配置
用户驱动的多数据库设置数据库选择
支持的数据库
数据库 | DB_TYPE代码 |
PostgreSQL | 前列腺素 |
红移 | 红移 |
蟑螂数据库 | 蟑螂 |
MySQL | mysql |
RDS MySQL | rds_mysql |
微软 SQL 服务器 | mssql |
大查询 | 大查询 |
Oracle 数据库 | 神谕 |
SQLite | SQLite |
我们使用 Legion Query Runner 库作为连接器。您可以在其API 文档中找到更多信息。
什么是 MCP?
模型上下文协议 (MCP) 是用于维护 AI 应用程序中上下文的规范。此服务器使用MCP Python SDK来执行以下操作:
将数据库操作公开为人工智能助手的工具
提供数据库模式和元数据作为资源
生成有用的数据库操作提示
启用与数据库的状态交互
安装和配置
必需参数
对于单个数据库配置:
DB_TYPE :数据库类型代码(见上表)
DB_CONFIG :数据库连接的 JSON 配置字符串
对于多数据库配置:
DB_CONFIGS :数据库配置的 JSON 数组,每个配置包含:
db_type :数据库类型代码
配置:数据库连接配置
description :数据库的可读描述
配置格式因数据库类型而异。有关特定数据库的配置详细信息,请参阅API 文档。
安装方法
选项 1:使用 UV(推荐)
使用uv时无需特殊安装。我们将使用uvx直接运行database-mcp 。
UV配置示例(单数据库):
REPLACE DB_TYPE and DB_CONFIG with your connection info.
{
"mcpServers": {
"database-mcp": {
"command": "uvx",
"args": [
"database-mcp"
],
"env": {
"DB_TYPE": "pg",
"DB_CONFIG": "{\"host\":\"localhost\",\"port\":5432,\"user\":\"user\",\"password\":\"pw\",\"dbname\":\"dbname\"}"
},
"disabled": true,
"autoApprove": []
}
}
}UV配置示例(多个数据库):
{
"mcpServers": {
"database-mcp": {
"command": "uvx",
"args": [
"database-mcp"
],
"env": {
"DB_CONFIGS": "[{\"id\":\"pg_main\",\"db_type\":\"pg\",\"configuration\":{\"host\":\"localhost\",\"port\":5432,\"user\":\"user\",\"password\":\"pw\",\"dbname\":\"postgres\"},\"description\":\"PostgreSQL Database\"},{\"id\":\"mysql_data\",\"db_type\":\"mysql\",\"configuration\":{\"host\":\"localhost\",\"port\":3306,\"user\":\"root\",\"password\":\"pass\",\"database\":\"mysql\"},\"description\":\"MySQL Database\"}]"
},
"disabled": true,
"autoApprove": []
}
}
}选项 2:使用 PIP
通过 pip 安装:
pip install database-mcpPIP配置示例(单个数据库):
{
"mcpServers": {
"database": {
"command": "python",
"args": [
"-m", "database_mcp",
"--repository", "path/to/git/repo"
],
"env": {
"DB_TYPE": "pg",
"DB_CONFIG": "{\"host\":\"localhost\",\"port\":5432,\"user\":\"user\",\"password\":\"pw\",\"dbname\":\"dbname\"}"
}
}
}
}运行服务器
生产模式
python mcp_server.py配置方法
环境变量(单个数据库)
export DB_TYPE="pg" # or mysql, postgresql, etc.
export DB_CONFIG='{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"}'
uv run src/database_mcp/mcp_server.py环境变量(多个数据库)
export DB_CONFIGS='[{"id":"pg_main","db_type":"pg","configuration":{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"},"description":"PostgreSQL Database"},{"id":"mysql_users","db_type":"mysql","configuration":{"host":"localhost","port":3306,"user":"root","password":"pass","database":"mysql"},"description":"MySQL Database"}]'
uv run src/database_mcp/mcp_server.py如果您未指定 ID,系统将根据数据库类型和描述自动生成一个:
export DB_CONFIGS='[{"db_type":"pg","configuration":{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"},"description":"PostgreSQL Database"},{"db_type":"mysql","configuration":{"host":"localhost","port":3306,"user":"root","password":"pass","database":"mysql"},"description":"MySQL Database"}]'
# IDs will be generated as something like "pg_postgres_0" and "my_mysqldb_1"
uv run src/database_mcp/mcp_server.py命令行参数(单个数据库)
python mcp_server.py --db-type pg --db-config '{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"}'命令行参数(多个数据库)
python mcp_server.py --db-configs '[{"id":"pg_main","db_type":"pg","configuration":{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"},"description":"PostgreSQL Database"},{"id":"mysql_users","db_type":"mysql","configuration":{"host":"localhost","port":3306,"user":"root","password":"pass","database":"mysql"},"description":"MySQL Database"}]'请注意,您可以使用id字段为每个数据库指定自定义 ID,或者让系统根据数据库类型和描述生成它们。
多数据库支持
当连接到多个数据库时,需要指定每个查询使用哪个数据库:
使用
list_databases工具查看可用的数据库及其 ID使用
get_database_info查看数据库的架构详细信息使用
find_table在所有数据库中定位表为诸如
execute_query、get_table_columns等工具提供db_id参数。
数据库连接在内部以DbConfig对象字典的形式进行管理,每个数据库都有唯一的 ID。架构信息以表对象列表的形式表示,其中每个表包含其名称和列信息。
select_database提示引导用户完成数据库选择过程。
模式表示
数据库模式表示为表对象列表,每个表包含有关其列的信息:
[
{
"name": "users",
"columns": [
{"name": "id", "type": "integer"},
{"name": "username", "type": "varchar"},
{"name": "email", "type": "varchar"}
]
},
{
"name": "orders",
"columns": [
{"name": "id", "type": "integer"},
{"name": "user_id", "type": "integer"},
{"name": "product_id", "type": "integer"},
{"name": "quantity", "type": "integer"}
]
}
]通过这种表示形式,可以轻松地以编程方式访问表和列信息,同时保持清晰的层次结构。
公开的 MCP 功能
资源
资源 | 描述 |
| 获取一个或所有已配置数据库的架构 |
工具
工具 | 描述 |
| 执行 SQL 查询并以 markdown 表形式返回结果 |
| 执行 SQL 查询并以 JSON 格式返回结果 |
| 获取特定表的列名 |
| 获取特定表的列类型 |
| 获取最近的查询历史记录 |
| 列出所有可用的数据库连接 |
| 获取有关数据库的详细信息(包括架构) |
| 查找哪个数据库包含特定表 |
| 获取表的详细描述,包括列名和类型 |
| 从表中获取数据样本 |
所有特定于数据库的工具(如execute_query , get_table_columns等)都需要一个db_id参数来指定要使用的数据库。
提示
迅速的 | 描述 |
| 针对数据库创建 SQL 查询 |
| 解释 SQL 查询的作用 |
| 优化 SQL 查询以获得更好的性能 |
| 帮助用户选择要使用的数据库 |
发展
使用 MCP 检查器
运行此命令来启动检查器
npx @modelcontextprotocol/inspector uv run src/database_mcp/mcp_server.py然后在命令输入字段中设置类似
run src/database_mcp/mcp_server.py --db-type pg --db-config '{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"}'测试
uv pip install -e ".[dev]"
pytest出版
# Clean up build artifacts
rm -rf dist/ build/
# Remove any .egg-info directories if they exist
find . -name "*.egg-info" -type d -exec rm -rf {} + 2>/dev/null || true
# Build the package
uv run python -m build
# Upload to PyPI
uv run python -m twine upload dist/*执照
此存储库采用 GPL 许可
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Servers
- AlicenseNot gradedqualityCmaintenanceThis is a Model Context Protocol (MCP) server for executing SQL queries against Databricks using the Statement Execution API. It enables AI assistants to directly query Databricks data warehouses, analyze database schemas, and retrieve query results in a structured format42MIT
- AlicenseNot gradedqualityDmaintenanceThis server enables Claude to directly interact with SQLite, SQL Server, PostgreSQL, and MySQL databases through the Model Context Protocol, allowing for query execution, table management, and data export capabilities.MIT
- FlicenseNot gradedqualityDmaintenanceA FastAPI-based server that enables executing SQL queries, managing database connections, and retrieving analytics reports through MCP-integrated endpoints. It allows users to interact with database schemas, performance metrics, and access logs using structured queries.
- FlicenseNot gradedqualityBmaintenanceModel Context Protocol (MCP) server that gives AI assistants a safe, correct data-analyst capability over business metrics - without raw SQL improvisation.
Related MCP Connectors
GibsonAI MCP server: manage your databases with natural language
MCP server for interacting with the Supabase platform
MCP server for managing Prisma Postgres.
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/TheRaLabs/legion-mcp'
If you have feedback or need assistance with the MCP directory API, please join our Discord server