Skip to content
Back to search

legion-mcp

A server that helps people access and query data in databases using the Legion Query Runner with Model Context Protocol (MCP) in Python.

Safety scan

A static text scan, not a guarantee. Select the badge to see what was scanned and every finding.

Checked today
Badge for your README
findsafeskills safety rating

Markdown

[![findsafeskills safety rating](/api/listings/theralabs--legion-mcp/badge.svg)](/listings/theralabs--legion-mcp)

HTML

<a href="/listings/theralabs--legion-mcp"><img src="/api/listings/theralabs--legion-mcp/badge.svg" alt="findsafeskills safety rating" /></a>

Install

i

Install instructions from the README

As written by the repo's author. findsafeskills has not verified these steps.

Required Parameters

For single database configuration: - **DB_TYPE**: The database type code (see table above) - **DB_CONFIG**: A JSON configuration string for database connection

For multi-database configuration: - **DB_CONFIGS**: A JSON array of database configurations, each containing: - **db_type**: The database type code - **configuration**: Database connection configuration - **description**: A human-readable description of the database

The configuration format varies by database type. See the [API documentation](https://theralabs.github.io/legion-database/docs/category/query-runners) for database-specific configuration details.

Installation Methods

Option 1: Using UV (Recommended)

When using [`uv`](https://docs.astral.sh/uv/), no specific installation is needed. We will use [`uvx`](https://docs.astral.sh/uv/guides/tools/) to directly run *database-mcp*.

**UV Configuration Example (Single Database):**

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 Configuration Example (Multiple Databases):**

{
    "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": []
      }
    }
}

Option 2: Using PIP

Install via pip:

pip install database-mcp

**PIP Configuration Example (Single Database):**

{
  "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\"}"
      }
    }
  }
}

What was scanned

The files the safety scan read, as written by the repository's author and shown as plain text. Lines with a finding are marked.

README.md · README · 309 lines

View this file on GitHub (read at the repository's current version: the scan did not record which commit it read)

  1. # Multi-Database MCP Server (by Legion AI)
  2. A server that helps people access and query data in databases using the Legion Query Runner with integration of the Model Context Protocol (MCP) Python SDK.
  3. # Start Generation Here
  4. This tool is provided by [Legion AI](https://thelegionai.com/). To use the full-fledged and fully powered AI data analytics tool, please visit the site. Email us if there is one database you want us to support.
  5. # End Generation Here
  6. ## Why Choose Database MCP
  7. Database MCP stands out from other database access solutions for several compelling reasons:
  8. - **Unified Multi-Database Interface**: Connect to PostgreSQL, MySQL, SQL Server, and other databases through a single consistent API - no need to learn different client libraries for each database type.
  9. - **AI-Ready Integration**: Built specifically for AI assistant interactions through the Model Context Protocol (MCP), enabling natural language database operations.
  10. - **Zero-Configuration Schema Discovery**: Automatically discovers and exposes database schemas without manual configuration or mapping.
  11. - **Database-Agnostic Tools**: Find tables, explore schemas, and execute queries with the same set of tools regardless of the underlying database technology.
  12. - **Secure Credential Management**: Handles database authentication details securely, separating credentials from application code.
  13. - **Simple Deployment**: Works with modern AI development environments like LangChain, FastAPI, and others with minimal setup.
  14. - **Extensible Design**: Easily add custom tools and prompts to enhance functionality for specific use cases.
  15. Whether you're building AI agents that need database access or simply want a unified interface to multiple databases, Database MCP provides a streamlined solution that dramatically reduces development time and complexity.
  16. ## Features
  17. - Multi-database support - connect to multiple databases simultaneously
  18. - Database access via Legion Query Runner
  19. - Model Context Protocol (MCP) support for AI assistants
  20. - Expose database operations as MCP resources, tools, and prompts
  21. - Multiple deployment options (standalone MCP server, FastAPI integration)
  22. - Query execution and result handling
  23. - Flexible configuration via environment variables, command-line arguments, or MCP settings JSON
  24. - User-driven database selection for multi-database setups
  25. ## Supported Databases
  26. | Database | DB_TYPE code |
  27. |----------|--------------|
  28. | PostgreSQL | pg |
  29. | Redshift | redshift |
  30. | CockroachDB | cockroach |
  31. | MySQL | mysql |
  32. | RDS MySQL | rds_mysql |
  33. | Microsoft SQL Server | mssql |
  34. | Big Query | bigquery |
  35. | Oracle DB | oracle |
  36. | SQLite | sqlite |
  37. We use Legion Query Runner library as connectors. You can find more info on their [api doc](https://theralabs.github.io/legion-database/docs/category/query-runners).
  38. ## What is MCP?
  39. The Model Context Protocol (MCP) is a specification for maintaining context in AI applications. This server uses the [MCP Python SDK](https://github.com/modelcontextprotocol/python-sdk) to:
  40. - Expose database operations as tools for AI assistants
  41. - Provide database schemas and metadata as resources
  42. - Generate useful prompts for database operations
  43. - Enable stateful interactions with databases
  44. ## Installation & Configuration
  45. ### Required Parameters
  46. For single database configuration:
  47. - **DB_TYPE**: The database type code (see table above)
  48. - **DB_CONFIG**: A JSON configuration string for database connection
  49. For multi-database configuration:
  50. - **DB_CONFIGS**: A JSON array of database configurations, each containing:
  51. - **db_type**: The database type code
  52. - **configuration**: Database connection configuration
  53. - **description**: A human-readable description of the database
  54. The configuration format varies by database type. See the [API documentation](https://theralabs.github.io/legion-database/docs/category/query-runners) for database-specific configuration details.
  55. ### Installation Methods
  56. #### Option 1: Using UV (Recommended)
  57. When using [`uv`](https://docs.astral.sh/uv/), no specific installation is needed. We will use [`uvx`](https://docs.astral.sh/uv/guides/tools/) to directly run *database-mcp*.
  58. **UV Configuration Example (Single Database):**
  59. ```json
  60. REPLACE DB_TYPE and DB_CONFIG with your connection info.
  61. {
  62. "mcpServers": {
  63. "database-mcp": {
  64. "command": "uvx",
  65. "args": [
  66. "database-mcp"
  67. ],
  68. "env": {
  69. "DB_TYPE": "pg",
  70. "DB_CONFIG": "{\"host\":\"localhost\",\"port\":5432,\"user\":\"user\",\"password\":\"pw\",\"dbname\":\"dbname\"}"
  71. },
  72. "disabled": true,
  73. "autoApprove": []
  74. }
  75. }
  76. }
  77. ```
  78. **UV Configuration Example (Multiple Databases):**
  79. ```json
  80. {
  81. "mcpServers": {
  82. "database-mcp": {
  83. "command": "uvx",
  84. "args": [
  85. "database-mcp"
  86. ],
  87. "env": {
  88. "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\"}]"
  89. },
  90. "disabled": true,
  91. "autoApprove": []
  92. }
  93. }
  94. }
  95. ```
  96. #### Option 2: Using PIP
  97. Install via pip:
  98. ```bash
  99. pip install database-mcp
  100. ```
  101. **PIP Configuration Example (Single Database):**
  102. ```json
  103. {
  104. "mcpServers": {
  105. "database": {
  106. "command": "python",
  107. "args": [
  108. "-m", "database_mcp",
  109. "--repository", "path/to/git/repo"
  110. ],
  111. "env": {
  112. "DB_TYPE": "pg",
  113. "DB_CONFIG": "{\"host\":\"localhost\",\"port\":5432,\"user\":\"user\",\"password\":\"pw\",\"dbname\":\"dbname\"}"
  114. }
  115. }
  116. }
  117. }
  118. ```
  119. ## Running the Server
  120. ### Production Mode
  121. ```bash
  122. python mcp_server.py
  123. ```
  124. ### Configuration Methods
  125. #### Environment Variables (Single Database)
  126. ```bash
  127. export DB_TYPE="pg" # or mysql, postgresql, etc.
  128. export DB_CONFIG='{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"}'
  129. uv run src/database_mcp/mcp_server.py
  130. ```
  131. #### Environment Variables (Multiple Databases)
  132. ```bash
  133. 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"}]'
  134. uv run src/database_mcp/mcp_server.py
  135. ```
  136. If you don't specify an ID, the system will generate one automatically based on the database type and description:
  137. ```bash
  138. 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"}]'
  139. # IDs will be generated as something like "pg_postgres_0" and "my_mysqldb_1"
  140. uv run src/database_mcp/mcp_server.py
  141. ```
  142. #### Command Line Arguments (Single Database)
  143. ```bash
  144. python mcp_server.py --db-type pg --db-config '{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"}'
  145. ```
  146. #### Command Line Arguments (Multiple Databases)
  147. ```bash
  148. 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"}]'
  149. ```
  150. Note that you can specify custom IDs for each database using the `id` field, or let the system generate them based on database type and description.
  151. ## Multi-Database Support
  152. When connecting to multiple databases, you need to specify which database to use for each query:
  153. 1. Use the `list_databases` tool to see available databases with their IDs
  154. 2. Use `get_database_info` to view schema details of databases
  155. 3. Use `find_table` to locate a table across all databases
  156. 4. Provide the `db_id` parameter to tools like `execute_query`, `get_table_columns`, etc.
  157. Database connections are managed internally as a dictionary of `DbConfig` objects, with each database having a unique ID. Schema information is represented as a list of table objects, where each table contains its name and column information.
  158. The `select_database` prompt guides users through the database selection process.
  159. ## Schema Representation
  160. Database schemas are represented as a list of table objects, with each table containing information about its columns:
  161. ```json
  162. [
  163. {
  164. "name": "users",
  165. "columns": [
  166. {"name": "id", "type": "integer"},
  167. {"name": "username", "type": "varchar"},
  168. {"name": "email", "type": "varchar"}
  169. ]
  170. },
  171. {
  172. "name": "orders",
  173. "columns": [
  174. {"name": "id", "type": "integer"},
  175. {"name": "user_id", "type": "integer"},
  176. {"name": "product_id", "type": "integer"},
  177. {"name": "quantity", "type": "integer"}
  178. ]
  179. }
  180. ]
  181. ```
  182. This representation makes it easy to programmatically access table and column information while keeping a clean hierarchical structure.
  183. ## Exposed MCP Capabilities
  184. ### Resources
  185. | Resource | Description |
  186. |----------|-------------|
  187. | `resource://schema/{database_id}` | Get the schemas for one or all configured databases |
  188. ### Tools
  189. | Tool | Description |
  190. |------|-------------|
  191. | `execute_query` | Execute a SQL query and return results as a markdown table |
  192. | `execute_query_json` | Execute a SQL query and return results as JSON |
  193. | `get_table_columns` | Get column names for a specific table |
  194. | `get_table_types` | Get column types for a specific table |
  195. | `get_query_history` | Get the recent query history |
  196. | `list_databases` | List all available database connections |
  197. | `get_database_info` | Get detailed information about a database including schema |
  198. | `find_table` | Find which database contains a specific table |
  199. | `describe_table` | Get detailed description of a table including column names and types |
  200. | `get_table_sample` | Get a sample of data from a table |
  201. All database-specific tools (like `execute_query`, `get_table_columns`, etc.) require a `db_id` parameter to specify which database to use.
  202. ### Prompts
  203. | Prompt | Description |
  204. |--------|-------------|
  205. | `sql_query` | Create an SQL query against the database |
  206. | `explain_query` | Explain what a SQL query does |
  207. | `optimize_query` | Optimize a SQL query for better performance |
  208. | `select_database` | Help user select which database to use |
  209. ## Development
  210. ### Using MCP Inspector
  211. run this to start the inspector
  212. ```bash
  213. npx @modelcontextprotocol/inspector uv run src/database_mcp/mcp_server.py
  214. ```
  215. then in the command input field, set something like
  216. ```
  217. run src/database_mcp/mcp_server.py --db-type pg --db-config '{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"}'
  218. ```
  219. ### Testing
  220. ```bash
  221. uv pip install -e ".[dev]"
  222. pytest
  223. ```
  224. ### Publishing
  225. ```bash
  226. # Clean up build artifacts
  227. rm -rf dist/ build/
  228. # Remove any .egg-info directories if they exist
  229. find . -name "*.egg-info" -type d -exec rm -rf {} + 2>/dev/null || true
  230. # Build the package
  231. uv run python -m build
  232. # Upload to PyPI
  233. uv run python -m twine upload dist/*
  234. ```
  235. ## License
  236. This repository is licensed under GPL

Ratings and comments

Sign in to rate this skill.

Sign in

Free to use. If it’s helped, pass it on.