Legion Database MCP server

Enables natural language querying and management of multiple database types (PostgreSQL, MySQL, SQL Server, BigQuery) for data analysis, business intelligence, and database exploration.
Back to servers
Setup instructions
Provider
Legion AI
Release date
Mar 24, 2025
Language
Python
Package
Stats
4.0K downloads
40 stars

The Multi-Database MCP Server by Legion AI provides a powerful way to access and query multiple databases using natural language through the Model Context Protocol (MCP) Python SDK. It creates a unified interface that connects AI assistants to various database systems through a consistent API.

Installation

Prerequisites

Before installation, you'll need:

  • Python installed on your system
  • Database connection information for each database you want to connect to
  • Basic understanding of JSON for configuration

Install with PIP

pip install database-mcp

Configuration Options

The server requires configuration for database connections, which can be provided in several ways.

For Single Database Connection

You need to specify:

  • DB_TYPE: Database type (pg, mysql, mssql, etc.)
  • DB_CONFIG: JSON string with connection details

For Multiple Database Connections

You need to specify:

  • DB_CONFIGS: JSON array containing multiple database configurations

Usage

Running the Server

Start the server using one of these methods:

# Basic startup
python mcp_server.py

Single Database Configuration Examples

Using Environment Variables:

export DB_TYPE="pg"
export DB_CONFIG='{"host":"localhost","port":5432,"user":"username","password":"password","dbname":"database_name"}'
uv run src/database_mcp/mcp_server.py

Using Command Line Arguments:

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

Multiple Database Configuration Examples

Using Environment Variables:

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

Using Command Line Arguments:

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

Using UV (Recommended Method)

When using uv, you can configure the server in your MCP settings:

Single Database Configuration:

{
    "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": []
      }
    }
}

Multiple Databases Configuration:

{
    "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": []
      }
    }
}

Working with Multiple Databases

When connecting to multiple databases, use these steps:

  1. Use the list_databases tool to see available databases with their IDs
  2. Get database schema details with get_database_info
  3. Find specific tables with find_table
  4. Specify the database ID when executing queries using db_id parameter

Available MCP Tools

The server exposes these database operations as tools:

  • execute_query: Run SQL queries with results as markdown tables
  • execute_query_json: Run SQL queries with results as JSON
  • get_table_columns: View column names for a specific table
  • get_table_types: View column data types for a specific table
  • get_query_history: See recent query history
  • list_databases: Display all available database connections
  • get_database_info: View detailed database schema information
  • find_table: Locate which database contains a specific table
  • describe_table: Get detailed table information including columns and types
  • get_table_sample: Retrieve sample data from a table

Supported Database Types

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

Connection configuration details for each database type can be found in the Legion Query Runner API documentation.

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "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":[]}'

See the official Claude Code MCP documentation for more details.

For Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "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": []
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "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": []
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later