Get the FREE Ultimate OpenClaw Setup Guide →

legion

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

Installation
Run this command in your terminal to add the MCP server to Claude Code.
Run in terminal:
Command
claude mcp add --transport stdio theralabs-legion-mcp uvx legion \
  --env DB_TYPE="pg" \
  --env DB_CONFIG="{"host":"localhost","port":5432,"user":"user","password":"pw","dbname":"dbname"}"

How to use

This MCP server provides a unified, multi-database access layer built around the Legion Query Runner and the Model Context Protocol (MCP) Python SDK. It exposes database operations, schemas, and metadata as MCP resources, tools, and prompts so AI agents can query multiple databases through natural language or tool invocations. You can configure single-database or multi-database setups via environment variables or MCP settings, enabling AI-assisted data exploration, prompt generation, and stateful interactions across PostgreSQL, MySQL, SQL Server, and more through a single interface.

To use the server, connect an MCP-enabled client (e.g., an agent using the MCP Python SDK or a framework like LangChain) and load the legion MCP server as a resource. Tools exposed by the server include database discovery, table and schema exploration, simple query execution, and metadata retrieval. For multi-database deployments, specify multiple DB_CONFIGS entries so the server can route queries to the correct database instance. The Legion Query Runner integration ensures consistent query execution across supported database types while preserving the MCP workflow for prompts and state management.

How to install

Prerequisites:

  • Python 3.8+ (recommended)
  • Access to uv (for UV-based execution) or pip for Python installs
  • Optional Docker if you prefer containerized deployment

Option A: Using UV (Recommended)

  1. Install UV if you don’t have it
# Follow UV installation instructions from https://docs.astral.sh/uv/
  1. Install and run the MCP server via UV configuration
# Example UV configuration (single database)
{
  "mcpServers": {
    "legion": {
      "command": "uvx",
      "args": [
        "legion"
      ],
      "env": {
        "DB_TYPE": "pg",
        "DB_CONFIG": "{\"host\":\"localhost\",\"port\":5432,\"user\":\"user\",\"password\":\"pw\",\"dbname\":\"dbname\"}"
      },
      "disabled": true,
      "autoApprove": []
    }
  }
}
  1. Start the UV-based MCP server using your preferred command runner for the configuration file above.

Option B: Using PIP (Python)

  1. Install the package (if provided as a Python package)
pip install database-mcp
  1. Create an MCP config JSON for the legions setup (single or multi DB) using the examples in the repository, e.g.:
{
  "mcpServers": {
    "legion": {
      "command": "python",
      "args": [
        "-m", "legion_mcp_server",
        "--repository", "/path/to/repo"
      ],
      "env": {
        "DB_TYPE": "pg",
        "DB_CONFIG": "{\"host\":\"localhost\",\"port\":5432,\"user\":\"user\",\"password\":\"pw\",\"dbname\":\"dbname\"}"
      }
    }
  }
}
  1. Run the MCP server with the configuration provided above.

Note: The repository may offer additional command-line flags or environment variables for multi-database configurations (DB_CONFIGS) and for enabling/disabling specific databases. Adapt the configuration accordingly to your environment.

Additional notes

Tips and common issues:

  • Use DB_CONFIG for a single database or DB_CONFIGS for multi-database deployments. Each entry can include db_type, configuration, and a human-friendly description.
  • Ensure DB_TYPE matches the table of supported databases (pg, mysql, redshift, mssql, etc.).
  • When using UV, the server name in the args should match the MCP server key (e.g., legion).
  • If credentials or hostnames change, update the corresponding DB_CONFIG/DB_CONFIGS values and reload the MCP server.
  • For production, consider using a proper secret management approach for DB_PASSWORD instead of embedding in environment variables.
  • The MCP integration exposes resources, tools, and prompts—explore discovery endpoints to list available resources and test tool invocations before embedding in agents.

Related MCP Servers

Sponsor this space

Reach thousands of developers