Universal database MCP server supporting multiple database types including PostgreSQL, Redshift, CockroachDB, MySQL, RDS MySQL, Microsoft SQL Server, BigQuery, Oracle DB, and SQLite.
Seamlessly connect your Large Language Models (LLMs) to your databases with the Database MCP Server, powered by Legion AI's cutting-edge query runner and the Model Context Protocol (MCP).
This server bridges the gap between the world of AI and the wealth of information stored in your databases. By leveraging the Model Context Protocol (MCP), it empowers AI assistants to interact with your data in a structured and intelligent manner. Whether you're building an AI-powered data analysis tool, enhancing a chatbot with real-time data access, or creating custom AI workflows, the Database MCP Server provides the foundation for seamless integration.
The Database MCP Server, powered by the Legion Query Runner, supports a wide range of popular database systems:
Database | DB_TYPE Code |
---|---|
PostgreSQL | pg |
Redshift | redshift |
CockroachDB | cockroach |
MySQL | mysql |
RDS MySQL | rds_mysql |
Microsoft SQL Server | mssql |
BigQuery | bigquery |
Oracle DB | oracle |
SQLite | sqlite |
For detailed information on database-specific configurations, refer to the Legion Query Runner API documentation.
The Model Context Protocol (MCP) is a crucial element of this server, enabling context-aware interactions between AI applications and your databases. The MCP Python SDK is used to:
Before installing, ensure you have Python 3.7+ installed.
Regardless of the installation method, you'll need to provide the following parameters:
DB_TYPE
: The database type code (refer to the table above).DB_CONFIG
: A JSON string containing the database connection configuration.The format of DB_CONFIG
varies depending on the database type. Consult the Legion Query Runner API documentation for specific details.
Choose the installation method that best suits your environment:
uv
is the recommended approach for managing dependencies and running the server. No specific installation is required; we'll use uvx
to execute the database-mcp
directly.
UV Configuration Example (uv.json
):
{ "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": [] } } }
Important: Replace DB_TYPE
and DB_CONFIG
with your actual database connection information.
Install the package using pip:
pip install database-mcp
PIP Configuration Example (mcp.json
):
{ "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\"}" } } } }
Use the mcp dev
command for development:
mcp dev mcp_server.py
Run the server directly using Python:
python mcp_server.py
You can configure the server using environment variables or command-line arguments.
export DB_TYPE="pg" # or mysql, postgresql, etc. export DB_CONFIG='{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"}' mcp dev mcp_server.py
python mcp_server.py --db-type pg --db-config '{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"}'
Or with UV:
uv mcp_server.py --db-type pg --db-config '{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"}'
The Database MCP Server exposes the following capabilities to AI agents:
Resource | Description |
---|---|
schema://all | Retrieves the complete database schema. |
Tool | Description |
---|---|
execute_query | Executes a SQL query and returns the results as a Markdown table. |
execute_query_json | Executes a SQL query and returns the results as JSON. |
get_table_columns | Retrieves the column names for a specified table. |
get_table_types | Retrieves the column types for a specified table. |
get_query_history | Retrieves the recent query history. |
Prompt | Description |
---|---|
sql_query | Generates a SQL query against the database. |
explain_query | Explains the functionality of a given SQL query. |
optimize_query | Optimizes a SQL query for improved performance. |
Install development dependencies and run tests:
uv pip install -e ".[dev]" pytest
rm -rf dist/ build/ *.egg-info/ && python -m build python -m build python -m twine upload dist/*
This powerful tool is brought to you by Legion AI. For a fully-featured AI data analytics solution, visit our website.
๐ โ๏ธ Biomedical research server providing access to PubMed, ClinicalTrials.gov, and MyVariant.info.
๐ MCP server that provides SQL analysis, linting, and dialect conversion using [SQLGlot](https://github.com/tobymao/sqlglot)
๐ ๐ All-in-one MCP server for Postgres development and operations, with tools for performance analysis, tuning, and health checks
Supabase MCP Server with support for SQL query execution and database exploration tools