SQL Alchemy MCP server

Integrates Claude Desktop with SQL databases using SQLAlchemy to enable direct querying, analysis, and exploration of data across many database engines.
Back to servers
Setup instructions
Provider
Rune Kaagaard
Release date
Dec 26, 2024
Language
Python
Stats
296 stars

MCP Alchemy connects Claude Desktop directly to your databases, allowing it to assist with database exploration, SQL query writing, relationship visualization, and data analysis. It works with PostgreSQL, MySQL, MariaDB, SQLite, Oracle, MS SQL Server, CrateDB, Vertica, and other SQLAlchemy-compatible databases.

Installation

Ensure you have uv installed:

# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh

Configuration with Claude Desktop

Add the appropriate configuration to your claude_desktop_config.json file. You'll need to include the database driver in the --with parameter.

SQLite (built into Python)

{
  "mcpServers": {
    "my_sqlite_db": {
      "command": "uvx",
      "args": ["--from", "mcp-alchemy==2025.8.15.91819",
               "--refresh-package", "mcp-alchemy", "mcp-alchemy"],
      "env": {
        "DB_URL": "sqlite:////absolute/path/to/database.db"
      }
    }
  }
}

PostgreSQL

{
  "mcpServers": {
    "my_postgres_db": {
      "command": "uvx",
      "args": ["--from", "mcp-alchemy==2025.8.15.91819", "--with", "psycopg2-binary",
               "--refresh-package", "mcp-alchemy", "mcp-alchemy"],
      "env": {
        "DB_URL": "postgresql://user:password@localhost/dbname"
      }
    }
  }
}

MySQL/MariaDB

{
  "mcpServers": {
    "my_mysql_db": {
      "command": "uvx",
      "args": ["--from", "mcp-alchemy==2025.8.15.91819", "--with", "pymysql",
               "--refresh-package", "mcp-alchemy", "mcp-alchemy"],
      "env": {
        "DB_URL": "mysql+pymysql://user:password@localhost/dbname"
      }
    }
  }
}

Microsoft SQL Server

{
  "mcpServers": {
    "my_mssql_db": {
      "command": "uvx",
      "args": ["--from", "mcp-alchemy==2025.8.15.91819", "--with", "pymssql",
               "--refresh-package", "mcp-alchemy", "mcp-alchemy"],
      "env": {
        "DB_URL": "mssql+pymssql://user:password@localhost/dbname"
      }
    }
  }
}

Oracle

{
  "mcpServers": {
    "my_oracle_db": {
      "command": "uvx",
      "args": ["--from", "mcp-alchemy==2025.8.15.91819", "--with", "oracledb",
               "--refresh-package", "mcp-alchemy", "mcp-alchemy"],
      "env": {
        "DB_URL": "oracle+oracledb://user:password@localhost/dbname"
      }
    }
  }
}

CrateDB

{
  "mcpServers": {
    "my_cratedb": {
      "command": "uvx",
      "args": ["--from", "mcp-alchemy==2025.8.15.91819", "--with", "sqlalchemy-cratedb>=0.42.0.dev1",
               "--refresh-package", "mcp-alchemy", "mcp-alchemy"],
      "env": {
        "DB_URL": "crate://user:password@localhost:4200/?schema=testdrive"
      }
    }
  }
}

For CrateDB Cloud, use a URL like: crate://user:[email protected]:4200?ssl=true

Vertica

{
  "mcpServers": {
    "my_vertica_db": {
      "command": "uvx",
      "args": ["--from", "mcp-alchemy==2025.8.15.91819", "--with", "vertica-python",
               "--refresh-package", "mcp-alchemy", "mcp-alchemy"],
      "env": {
        "DB_URL": "vertica+vertica_python://user:password@localhost:5433/dbname",
        "DB_ENGINE_OPTIONS": "{\"connect_args\": {\"ssl\": false}}"
      }
    }
  }
}

Environment Variables

Configure MCP Alchemy with these environment variables:

  • DB_URL: SQLAlchemy database URL (required)
  • CLAUDE_LOCAL_FILES_PATH: Directory for full result sets (optional)
  • EXECUTE_QUERY_MAX_CHARS: Maximum output length (optional, default 4000)
  • DB_ENGINE_OPTIONS: JSON string containing additional SQLAlchemy engine options (optional)

Connection Pooling

MCP Alchemy uses optimized connection pooling settings:

  • pool_pre_ping=True: Tests connections before use
  • pool_size=1: Maintains 1 persistent connection
  • max_overflow=2: Allows up to 2 additional connections
  • pool_recycle=3600: Refreshes connections older than 1 hour
  • isolation_level='AUTOCOMMIT': Ensures each query commits automatically

Override defaults with DB_ENGINE_OPTIONS:

{
  "DB_ENGINE_OPTIONS": "{\"pool_size\": 5, \"max_overflow\": 10, \"pool_recycle\": 1800}"
}

API

Tools

all_table_names

  • Returns all table names in the database
  • No input required
  • Returns comma-separated list of tables
users, orders, products, categories

filter_table_names

  • Finds tables matching a substring
  • Input: q (string)
  • Returns matching table names
Input: "user"
Returns: "users, user_roles, user_permissions"

schema_definitions

  • Gets detailed schema for specified tables
  • Input: table_names (string[])
  • Returns table definitions including columns, keys, and relationships
users:
    id: INTEGER, primary key, autoincrement
    email: VARCHAR(255), nullable
    created_at: DATETIME
    
    Relationships:
      id -> orders.user_id

execute_query

  • Executes SQL query with vertical output format
  • Inputs:
    • query (string): SQL query
    • params (object, optional): Query parameters
  • Returns results in clean vertical format
1. row
id: 123
name: John Doe
created_at: 2024-03-15T14:30:00
email: NULL

Result: 1 rows

Claude Local Files Integration

When you set the CLAUDE_LOCAL_FILES_PATH environment variable, MCP Alchemy integrates with claude-local-files, allowing you to:

  • Access complete result sets beyond Claude's context window
  • Generate detailed reports and visualizations
  • Perform deep analysis on large datasets
  • Export results for further processing

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 "mcp-alchemy" '{"command":"uvx","args":["--from","mcp-alchemy==2025.6.19.201831","--refresh-package","mcp-alchemy","mcp-alchemy"],"env":{"DB_URL":"sqlite:////absolute/path/to/database.db"}}'

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": {
        "mcp-alchemy": {
            "command": "uvx",
            "args": [
                "--from",
                "mcp-alchemy==2025.6.19.201831",
                "--refresh-package",
                "mcp-alchemy",
                "mcp-alchemy"
            ],
            "env": {
                "DB_URL": "sqlite:////absolute/path/to/database.db"
            }
        }
    }
}

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": {
        "mcp-alchemy": {
            "command": "uvx",
            "args": [
                "--from",
                "mcp-alchemy==2025.6.19.201831",
                "--refresh-package",
                "mcp-alchemy",
                "mcp-alchemy"
            ],
            "env": {
                "DB_URL": "sqlite:////absolute/path/to/database.db"
            }
        }
    }
}

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