Sponsored by Deepsite.site

Legion_mcp

Created By
TheRaLabs10 months ago
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

Server Config

{
  "mcpServers": {
    "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": []
    }
  }
}
Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
Tavily Mcp
DeepChatYour AI Partner on Desktop
ChatWiseThe second fastest AI chatbot™
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Y GuiA web-based graphical interface for AI chat interactions with support for multiple AI models and MCP (Model Context Protocol) servers.
WindsurfThe new purpose-built IDE to harness magic
Amap Maps高德地图官方 MCP Server
CursorThe AI Code Editor
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Playwright McpPlaywright MCP server
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Serper MCP ServerA Serper MCP Server
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code