le

legion-mcp

Universal database MCP server supporting multiple database types including PostgreSQL, Redshift, CockroachDB, MySQL, RDS MySQL, Microsoft SQL Server, BigQuery, Oracle DB, and SQLite.

Publisherlegion-mcp
Submitted date4/13/2025

๐Ÿš€ Database MCP Server: Unleash the Power of AI-Driven Data Access ๐Ÿš€

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).

Introduction

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.

Key Features

  • Database Access via Legion Query Runner: Harness the power of Legion AI's robust query runner for efficient and reliable database connectivity.
  • Model Context Protocol (MCP) Support: Seamlessly integrate with AI assistants using the MCP standard for context-aware interactions.
  • MCP Resources, Tools, and Prompts: Expose database operations as readily available resources, tools, and prompts for AI agents.
  • Flexible Deployment Options: Deploy as a standalone MCP server or integrate directly into your FastAPI applications.
  • Query Execution and Result Handling: Execute SQL queries and receive results in various formats, including Markdown tables and JSON.
  • Configurability: Tailor the server to your specific needs with environment variables, command-line arguments, or MCP settings JSON.

Supported Databases

The Database MCP Server, powered by the Legion Query Runner, supports a wide range of popular database systems:

DatabaseDB_TYPE Code
PostgreSQLpg
Redshiftredshift
CockroachDBcockroach
MySQLmysql
RDS MySQLrds_mysql
Microsoft SQL Servermssql
BigQuerybigquery
Oracle DBoracle
SQLitesqlite

For detailed information on database-specific configurations, refer to the Legion Query Runner API documentation.

Understanding the Model Context Protocol (MCP)

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:

  • Expose Database Operations as Tools: Provide AI assistants with pre-defined tools for executing queries and retrieving data.
  • Provide Database Schemas and Metadata as Resources: Equip AI agents with the necessary information about your database structure.
  • Generate Useful Prompts: Offer pre-built prompts to guide AI agents in formulating effective database queries.
  • Enable Stateful Interactions: Facilitate multi-turn conversations with databases, maintaining context across interactions.

Installation and Configuration

Prerequisites

Before installing, ensure you have Python 3.7+ installed.

Required Parameters

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.

Installation Methods

Choose the installation method that best suits your environment:

Option 1: Using UV (Recommended)

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.

Option 2: Using PIP

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

Running the Server

Development Mode

Use the mcp dev command for development:

mcp dev mcp_server.py

Production Mode

Run the server directly using Python:

python mcp_server.py

Configuration Methods

You can configure the server using environment variables or command-line arguments.

Environment Variables

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

Command Line Arguments

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"}'

Exposed MCP Capabilities

The Database MCP Server exposes the following capabilities to AI agents:

Resources

ResourceDescription
schema://allRetrieves the complete database schema.

Tools

ToolDescription
execute_queryExecutes a SQL query and returns the results as a Markdown table.
execute_query_jsonExecutes a SQL query and returns the results as JSON.
get_table_columnsRetrieves the column names for a specified table.
get_table_typesRetrieves the column types for a specified table.
get_query_historyRetrieves the recent query history.

Prompts

PromptDescription
sql_queryGenerates a SQL query against the database.
explain_queryExplains the functionality of a given SQL query.
optimize_queryOptimizes a SQL query for improved performance.

Development

Testing

Install development dependencies and run tests:

uv pip install -e ".[dev]" pytest

Publishing

rm -rf dist/ build/ *.egg-info/ && python -m build python -m build python -m twine upload dist/*

Brought to you by Legion AI

This powerful tool is brought to you by Legion AI. For a fully-featured AI data analytics solution, visit our website.

Visit More

View All