Multi-Database MCP Server (by Legion AI)
Unified multi-database access and AI interaction server with MCP integration.
Key Features
Use Cases
README
Multi-Database MCP Server (by Legion AI)
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.
Start Generation Here
This tool is provided by Legion AI. 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.
End Generation Here
Why Choose Database MCP
Database MCP stands out from other database access solutions for several compelling reasons:
- 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.
- AI-Ready Integration: Built specifically for AI assistant interactions through the Model Context Protocol (MCP), enabling natural language database operations.
- Zero-Configuration Schema Discovery: Automatically discovers and exposes database schemas without manual configuration or mapping.
- Database-Agnostic Tools: Find tables, explore schemas, and execute queries with the same set of tools regardless of the underlying database technology.
- Secure Credential Management: Handles database authentication details securely, separating credentials from application code.
- Simple Deployment: Works with modern AI development environments like LangChain, FastAPI, and others with minimal setup.
- Extensible Design: Easily add custom tools and prompts to enhance functionality for specific use cases.
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.
Features
- Multi-database support - connect to multiple databases simultaneously
- Database access via Legion Query Runner
- Model Context Protocol (MCP) support for AI assistants
- Expose database operations as MCP resources, tools, and prompts
- Multiple deployment options (standalone MCP server, FastAPI integration)
- Query execution and result handling
- Flexible configuration via environment variables, command-line arguments, or MCP settings JSON
- User-driven database selection for multi-database setups
Supported Databases
| Database | DB_TYPE code |
|---|---|
| PostgreSQL | pg |
| Redshift | redshift |
| CockroachDB | cockroach |
| MySQL | mysql |
| RDS MySQL | rds_mysql |
| Microsoft SQL Server | mssql |
| Big Query | bigquery |
| Oracle DB | oracle |
| SQLite | sqlite |
We use Legion Query Runner library as connectors. You can find more info on their api doc.
What is MCP?
The Model Context Protocol (MCP) is a specification for maintaining context in AI applications. This server uses the MCP Python SDK to:
- Expose database operations as tools for AI assistants
- Provide database schemas and metadata as resources
- Generate useful prompts for database operations
- Enable stateful interactions with databases
Installation & Configuration
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 for database-specific configuration details.
Installation Methods
Option 1: Using UV (Recommended)
When using uv, no specific installation is needed. We will use uvx 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\"}"
}
}
}
}
Running the Server
Production Mode
python mcp_server.py
Configuration Methods
Environment Variables (Single Database)
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
Environment Variables (Multiple Databases)
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
If you don't specify an ID, the system will generate one automatically based on the database type and description:
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
Command Line Arguments (Single Database)
python mcp_server.py --db-type pg --db-config '{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"}'
Command Line Arguments (Multiple Databases)
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"}]'
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.
Multi-Database Support
When connecting to multiple databases, you need to specify which database to use for each query:
- Use the
list_databasestool to see available databases with their IDs - Use
get_database_infoto view schema details of databases - Use
find_tableto locate a table across all databases - Provide the
db_idparameter to tools likeexecute_query,get_table_columns, etc.
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.
The select_database prompt guides users through the database selection process.
Schema Representation
Database schemas are represented as a list of table objects, with each table containing information about its columns:
[
{
"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"}
]
}
]
This representation makes it easy to programmatically access table and column information while keeping a clean hierarchical structure.
Exposed MCP Capabilities
Resources
| Resource | Description |
|---|---|
resource://schema/{database_id} |
Get the schemas for one or all configured databases |
Tools
| Tool | Description |
|---|---|
execute_query |
Execute a SQL query and return results as a markdown table |
execute_query_json |
Execute a SQL query and return results as JSON |
get_table_columns |
Get column names for a specific table |
get_table_types |
Get column types for a specific table |
get_query_history |
Get the recent query history |
list_databases |
List all available database connections |
get_database_info |
Get detailed information about a database including schema |
find_table |
Find which database contains a specific table |
describe_table |
Get detailed description of a table including column names and types |
get_table_sample |
Get a sample of data from a table |
All database-specific tools (like execute_query, get_table_columns, etc.) require a db_id parameter to specify which database to use.
Prompts
| Prompt | Description |
|---|---|
sql_query |
Create an SQL query against the database |
explain_query |
Explain what a SQL query does |
optimize_query |
Optimize a SQL query for better performance |
select_database |
Help user select which database to use |
Development
Using MCP Inspector
run this to start the inspector
npx @modelcontextprotocol/inspector uv run src/database_mcp/mcp_server.py
then in the command input field, set something like
run src/database_mcp/mcp_server.py --db-type pg --db-config '{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"}'
Testing
uv pip install -e ".[dev]"
pytest
Publishing
# 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/*
License
This repository is licensed under GPL
Star History
Repository Owner
User
Repository Details
Programming Languages
Tags
Topics
Join Our Newsletter
Stay updated with the latest AI tools, news, and offers by subscribing to our weekly newsletter.
Related MCPs
Discover similar Model Context Protocol servers
MCP 数据库工具 (MCP Database Utilities)
A secure bridge enabling AI systems safe, read-only access to multiple databases via unified configuration.
MCP Database Utilities provides a secure, standardized service for AI systems to access and analyze databases like SQLite, MySQL, and PostgreSQL using a unified YAML-based configuration. It enforces strict read-only operations, local processing, and credential protection to ensure data privacy and integrity. The tool is suitable for entities focused on data privacy and minimizes risks by isolating database connections and masking sensitive data. Designed for easy integration, it supports multiple installation options and advanced capabilities such as schema analysis and table browsing.
- ⭐ 85
- MCP
- donghao1393/mcp-dbutils
XiYan MCP Server
A server enabling natural language queries to SQL databases via the Model Context Protocol.
XiYan MCP Server is a Model Context Protocol (MCP) compliant server that allows users to query SQL databases such as MySQL and PostgreSQL using natural language. It leverages the XiYanSQL model, providing state-of-the-art text-to-SQL translation and supports both general LLMs and local deployment for enhanced security. The server lists available database tables as resources and can read table contents, making it simple to integrate with different applications.
- ⭐ 218
- MCP
- XGenerationLab/xiyan_mcp_server
YDB MCP
MCP server for AI-powered natural language database operations on YDB.
YDB MCP acts as a Model Context Protocol server enabling YDB databases to be accessed via any LLM supporting MCP. It allows AI-driven and natural language interaction with YDB instances by bridging database operations with language model interfaces. Flexible deployment through uvx, pipx, or pip is supported, along with multiple authentication methods. The integration empowers users to manage YDB databases conversationally through standardized protocols.
- ⭐ 24
- MCP
- ydb-platform/ydb-mcp
dbt MCP Server
Bridge dbt projects and AI agents with rich project context.
dbt MCP Server provides an implementation of the Model Context Protocol for dbt projects, enabling seamless integration between dbt and AI agents. It allows agents to access and understand the context of dbt Core, dbt Fusion, and dbt Platform projects. The tool supports connection to external AI products and offers resources for building custom agents. Documentation and examples are provided to facilitate adoption and integration.
- ⭐ 420
- MCP
- dbt-labs/dbt-mcp
Pica MCP Server
A Model Context Protocol (MCP) server for seamless integration with 100+ platforms via Pica.
Pica MCP Server provides a standardized Model Context Protocol (MCP) interface for interaction with a wide range of third-party services through Pica. It enables direct platform integrations, action execution, and intelligent intent detection while prioritizing secure environment variable management. The server also offers features such as code generation, form and data handling, and robust documentation for platform actions. It supports multiple deployment methods, including standalone, Docker, Vercel, and integration with tools like Claude Desktop and Cursor.
- ⭐ 8
- MCP
- picahq/mcp
TeslaMate MCP Server
Query your TeslaMate data using the Model Context Protocol
TeslaMate MCP Server implements the Model Context Protocol to enable AI assistants and clients to securely access and query Tesla vehicle data, statistics, and analytics from a TeslaMate PostgreSQL database. The server exposes a suite of tools for retrieving vehicle status, driving history, charging sessions, battery health, and more using standardized MCP endpoints. It supports local and Docker deployments, includes bearer token authentication, and is intended for integration with MCP-compatible AI systems like Claude Desktop.
- ⭐ 106
- MCP
- cobanov/teslamate-mcp
Didn't find tool you were looking for?