Skip to main content
Glama

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 都能提供简化的解决方案,大大减少开发时间和复杂性。

特征

  • 多数据库支持——同时连接多个数据库
  • 通过 Legion Query Runner 访问数据库
  • 人工智能助手的模型上下文协议 (MCP) 支持
  • 将数据库操作公开为 MCP 资源、工具和提示
  • 多种部署选项(独立 MCP 服务器、FastAPI 集成)
  • 查询执行和结果处理
  • 通过环境变量、命令行参数或 MCP 设置 JSON 进行灵活配置
  • 用户驱动的多数据库设置数据库选择

支持的数据库

数据库DB_TYPE代码
PostgreSQL前列腺素
红移红移
蟑螂数据库蟑螂
MySQLmysql
RDS MySQLrds_mysql
微软 SQL 服务器mssql
大查询大查询
Oracle 数据库神谕
SQLiteSQLite

我们使用 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-mcp

PIP配置示例(单个数据库):

{ "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,或者让系统根据数据库类型和描述生成它们。

多数据库支持

当连接到多个数据库时,需要指定每个查询使用哪个数据库:

  1. 使用list_databases工具查看可用的数据库及其 ID
  2. 使用get_database_info查看数据库的架构详细信息
  3. 使用find_table在所有数据库中定位表
  4. 为诸如execute_queryget_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 功能

资源

资源描述
resource://schema/{database_id}获取一个或所有已配置数据库的架构

工具

工具描述
execute_query执行 SQL 查询并以 markdown 表形式返回结果
execute_query_json执行 SQL 查询并以 JSON 格式返回结果
get_table_columns获取特定表的列名
get_table_types获取特定表的列类型
get_query_history获取最近的查询历史记录
list_databases列出所有可用的数据库连接
get_database_info获取有关数据库的详细信息(包括架构)
find_table查找哪个数据库包含特定表
describe_table获取表的详细描述,包括列名和类型
get_table_sample从表中获取数据样本

所有特定于数据库的工具(如execute_queryget_table_columns等)都需要一个db_id参数来指定要使用的数据库。

提示

迅速的描述
sql_query针对数据库创建 SQL 查询
explain_query解释 SQL 查询的作用
optimize_query优化 SQL 查询以获得更好的性能
select_database帮助用户选择要使用的数据库

发展

使用 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 许可

-
security - not tested
A
license - permissive license
-
quality - not tested

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

一个服务器,帮助用户使用集成了模型上下文协议 (MCP) Python SDK 的查询运行器 (Query Runner) 访问和查询数据库中的数据。由 Legion AI 团队 (thelegionai.com) 支持。

支持的数据库包括 PostgreSQL、Redshift、MySQL、Microsoft SQL Server、Google API、Amazon Web Services(通过 boto3)、CockroachDB、SQLite

  1. 从这里开始生成
    1. 终结此世代
      1. 为什么选择数据库 MCP
      2. 特征
      3. 支持的数据库
      4. 什么是 MCP?
      5. 安装和配置
      6. 运行服务器
      7. 多数据库支持
      8. 模式表示
      9. 公开的 MCP 功能
      10. 发展
      11. 执照

    Related MCP Servers

    • -
      security
      F
      license
      -
      quality
      A server that enables interaction with PostgreSQL, MySQL, MariaDB, or SQLite databases through Claude Desktop using natural language queries.
      Last updated -
      Python
    • -
      security
      A
      license
      -
      quality
      This 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 format
      Last updated -
      9
      Python
      MIT License
      • Linux
      • Apple
    • -
      security
      A
      license
      -
      quality
      A Model Context Protocol server that provides Claude access to Turso-hosted LibSQL databases, enabling database table listing, schema retrieval, and SELECT query execution.
      Last updated -
      68
      5
      TypeScript
      MIT License
      • Apple
    • -
      security
      A
      license
      -
      quality
      An MCP server that enables MySQL database integration with Claude. You can execute SQL queries and manage database connections.
      Last updated -
      1
      Python
      MIT License
      • Apple

    View all related MCP servers

    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