Legion_mcp

Legion_mcp

By TheRaLabs GitHub

-

legion_mcp database-access
Overview

What is Legion MCP?

Legion MCP (Model Context Protocol) Server is a server designed to facilitate access and querying of data in databases using the Legion Query Runner, integrated with the Model Context Protocol (MCP) Python SDK.

How to use Legion MCP?

To use Legion MCP, set up the server by installing dependencies, configuring your database connection, and running the server in either development or production mode. You can execute SQL queries and manage database operations through the MCP interface.

Key features of Legion MCP?

  • Database access via Legion Query Runner
  • Support for Model Context Protocol (MCP) for AI assistants
  • Exposes database operations as MCP resources, tools, and prompts
  • Multiple deployment options including standalone and FastAPI integration
  • Flexible configuration through environment variables and command-line arguments

Use cases of Legion MCP?

  1. Enabling AI assistants to perform database operations seamlessly.
  2. Providing a structured way to manage and query database schemas and metadata.
  3. Facilitating stateful interactions with databases for applications.

FAQ from Legion MCP?

  • What is the Model Context Protocol (MCP)?

MCP is a specification for maintaining context in AI applications, allowing for better interaction with databases.

  • How do I install Legion MCP?

You can install it using the uv dependency manager and follow the setup instructions provided in the documentation.

  • Can I run Legion MCP in production?

Yes, Legion MCP can be run in production mode with appropriate configurations.

Content

Legion MCP (Model Context Protocol) Server

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.

Features

  • 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

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

Setup with uv

This project uses uv for dependency management.

Installation

  1. Install uv:
pip install uv
  1. Create and activate a virtual environment:
uv venv
source .venv/bin/activate  # On Unix/macOS
# OR
.venv\Scripts\activate  # On Windows
  1. Install dependencies:
uv pip install -e .

Development

To run the server in development mode:

mcp dev mcp_server.py

For production mode:

python mcp_server.py

Testing

Run tests with:

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

Standalone FastMCP Server

The simplest approach using the high-level FastMCP API:

python mcp_server.py

This runs a standalone MCP server using the FastMCP class, which provides:

  • Easy setup with decorators for resources, tools, and prompts
  • Automatic context management
  • Lifecycle management via lifespan

MCP Development

For development with the MCP Inspector tool:

mcp dev mcp_server.py

MCP Configuration

Environment Variables

When running with the MCP CLI, you can configure the database connection using 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

For direct execution, use command line arguments:

python mcp_server.py --db-type pg --db-config '{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"}'

MCP Settings JSON

To integrate with AI assistants like Claude, you can configure the MCP server in the settings JSON:

{
  "legion_mcp": {
    "command": "uv",
    "args": [
      "--directory",
      "/path/to/legion-mcp",
      "run",
      "mcp_server.py"
    ],
    "env": {
      "DB_TYPE": "pg",
      "DB_CONFIG": "{\"host\":\"localhost\",\"port\":5432,\"user\":\"username\",\"password\":\"password\",\"dbname\":\"database_name\"}"
    },
    "disabled": false,
    "autoApprove": []
  }
}

This configuration:

  • Specifies the command to run the server (uv run mcp_server.py)
  • Sets the working directory (/path/to/legion-mcp)
  • Provides database connection details via environment variables
  • Controls whether the MCP is enabled or disabled
  • Can specify actions to auto-approve without user confirmation

Place this configuration in your editor's MCP settings file to enable database access for your AI assistant.

Exposed MCP Capabilities

Resources

  • schema://all - Get the complete database schema

Tools

  • 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

Prompts

  • 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

Development

Run tests:

pytest

License

This repository is licensed under CC BY-NC-SA 4.0

No tools information available.
PostgreSQL
PostgreSQL by modelcontextprotocol

Read-only database access with schema inspection

postgresql database-access
View Details