SQLAlchemy MCP Server 🧙
A Model Context Protocol (MCP) server that enables AI assistants to interact with your databases safely and efficiently. Connect to SQLite, PostgreSQL, or MySQL databases and let AI help you explore schemas, query data, and analyze your database structure.
What This Does
This MCP server allows AI assistants to:
- 🔍 Explore your database structure - List schemas, tables, columns, and relationships
- 📊 Query your data safely - Execute read-only queries with built-in safety controls
- 🛡️ Protect your data - Read-only mode prevents accidental data modification
- 📈 Analyze relationships - Understand how your tables connect through foreign keys
- 🔧 Support multiple databases - Works with SQLite, PostgreSQL, and MySQL
Available Tools
The server provides 8 powerful tools for database interaction:
🔍 Schema Discovery Tools
| Tool | Parameters | Description | Safety |
|---|---|---|---|
list_schemas |
none | Lists all schemas in the database | ✅ Safe |
list_tables |
schema_name (optional) |
Lists all tables, optionally filtered by schema | ✅ Safe |
describe_table |
table_name, schema_name (optional) |
Shows table structure: columns, types, constraints, foreign keys | ✅ Safe |
get_table_relationships |
none | Maps all foreign key relationships across the database | ✅ Safe |
📊 Data Exploration Tools
| Tool | Parameters | Description | Safety |
|---|---|---|---|
get_table_data |
table_name, schema_name (optional), limit (default: 10) |
Returns sample data from a table | ✅ Safe |
get_unique_values |
table_name, column_name, schema_name (optional), limit (default: 25) |
Shows unique values in a column with frequency counts | ✅ Safe |
⚡ Query Execution Tools
| Tool | Parameters | Description | Safety |
|---|---|---|---|
execute_read_query |
sql |
Executes read-only SQL (SELECT, SHOW, DESCRIBE, EXPLAIN, WITH) | ✅ Safe |
execute_query |
sql |
Executes any SQL including writes (INSERT, UPDATE, DELETE, DDL) | ⚠️ Destructive |
🛡️ Built-in Safety Features
- Automatic Input Validation: All table/column names are validated against SQL injection
- Result Limits: Default maximum of 25 rows returned (configurable)
- Query Timeout: Automatic timeout after 30 seconds (configurable)
- Read-Only Mode: When enabled, blocks all write operations
- Smart Query Detection: Automatically categorizes queries as safe or destructive
Quick Setup
Quick Reference
| Database | Install Command | Dependencies |
|---|---|---|
| SQLite | uvx mcp-sqlalchemy |
None (works out of the box) |
| PostgreSQL | uvx "mcp-sqlalchemy[postgresql]" |
asyncpg |
| MySQL | uvx "mcp-sqlalchemy[mysql]" |
aiomysql |
| All | uvx "mcp-sqlalchemy[all]" |
asyncpg + aiomysql |
Installation & Configuration
Option 1: Install via uvx (Recommended)
Add to your AI assistant's MCP configuration:
{
"mcpServers": {
"sqlalchemy": {
"command": "uvx",
"args": [
"mcp-sqlalchemy[postgresql]", // Choose: [postgresql], [mysql], [all], or omit for SQLite only
],
"env": {
"DATABASE_URL": "sqlite:////absolute/path/to/database.db",
"READ_ONLY_MODE": "true"
}
}
}
}
Option 2: Local Development Setup
For development or customization:
# Clone the repository
git clone https://github.com/woonstadrotterdam/mcp-sqlalchemy.git
cd mcp-sqlalchemy
uv venv
source .venv/bin/activate # or .venv/Scripts/activate on Windows
# Install dependencies (choose one):
uv sync # SQLite only
uv sync --extra postgresql # + PostgreSQL support
uv sync --extra mysql # + MySQL support
uv sync --extra all # All database support
Test the Connection
# Start the development server to test using the proper entry point
uv run mcp dev src/mcp_sqlalchemy/_dev.py
This will open a web interface where you can test the connection and explore your database.
Installation Method Comparison
| Method | Pros | Cons | Best For |
|---|---|---|---|
| uvx | Simple install, automatic updates, works anywhere | Requires internet, need [extra] syntax for PostgreSQL/MySQL |
Most users |
| Local | Works offline, can modify code, full control, easy dependency management | Manual updates, manage dependencies | Developers |
Updating Your Installation
For uvx installations:
# Updates happen automatically when you restart your AI assistant
# Or force update with:
uvx mcp-sqlalchemy
For local installations:
cd mcp-sqlalchemy
git pull
uv sync
Database Connection Examples
SQLite (File-based)
# Local database file
DATABASE_URL="sqlite:///database/myapp.db"
# Relative path
DATABASE_URL="sqlite:///./data/database.db"
# Absolute path
DATABASE_URL="sqlite:////full/path/to/database.db"
PostgreSQL
# Basic connection
DATABASE_URL="postgresql://user:password@localhost:5432/dbname"
# With specific schema
DATABASE_URL="postgresql://user:password@localhost:5432/dbname"
DB_SCHEMA_NAME="public"
# Remote database
DATABASE_URL="postgresql://user:password@db.example.com:5432/dbname"
MySQL
# Local MySQL
DATABASE_URL="mysql://user:password@localhost:3306/dbname"
# Remote MySQL
DATABASE_URL="mysql://user:password@mysql.example.com:3306/dbname"
Safety Features
Read-Only Mode (Recommended)
Read-only mode is enabled by default to prevent any data modifications:
# The server automatically detects and blocks:
# - INSERT, UPDATE, DELETE statements
# - DROP, CREATE, ALTER statements
# - Any potentially destructive operations
Automatic Protections
- Query Validation: Blocks dangerous SQL patterns
- Result Limits: Prevents overwhelming responses (default: 25 rows)
- Read-Only by Default: Safe mode enabled by default
- Timeout Protection: Queries timeout after 30 seconds
- Input Sanitization: Validates table and column names
What Your AI Can Do
Database Exploration
- "Show me all the tables in my database" → Lists all tables across schemas
- "Describe the users table" → Shows columns, types, and constraints
- "What tables are related to the orders table?" → Shows foreign key relationships
- "List all schemas in the database" → Shows available schemas
Data Analysis
- "Show me a sample of the users table" → Returns first 10 rows
- "What are the unique values in the status column?" → Lists distinct values with counts
- "Query all active users" → Executes:
SELECT * FROM users WHERE status = 'active' - "How many orders were placed last month?" → Custom date-based queries
Schema Understanding
- "Explain the database structure" → Comprehensive schema overview
- "How are customers connected to orders?" → Relationship mapping
- "What indexes exist on the products table?" → Index information
Configuration Options
You can customize the server behavior:
# Set maximum rows returned per query
MAX_RESULT_ROWS=50
# Set query timeout (seconds)
MAX_QUERY_TIMEOUT=60
# Enable read-only mode
READ_ONLY_MODE=true
# Set default schema (PostgreSQL)
DB_SCHEMA_NAME=public
Troubleshooting
Installation Issues
"uvx: command not found"
- Install uvx:
curl -LsSf https://astral.sh/uv/install.sh | sh - Or via pip:
pip install uvthen useuv tool runinstead ofuvx
"Module not found" for PostgreSQL/MySQL
- PostgreSQL: Install with
[postgresql]syntax or ensureasyncpgis available - MySQL: Install with
[mysql]syntax or ensureaiomysqlis available - All databases: Use
[all]syntax for complete support
Connection Issues
"Database URL must be provided"
- Make sure
DATABASE_URLis set in your MCP configuration'senvsection or add--database-urlto the command line - Check the URL format matches your database type
"No module named 'greenlet'"
- For local installs: Run
uv syncto install all dependencies - For uvx installs: This should auto-install; try forcing reinstall with
uvx --force ... - The
greenletpackage is required for async database operations
"Connection refused"
- Verify your database server is running
- Check the hostname, port, username, and password
- For PostgreSQL/MySQL, ensure the database exists
Query Issues
"Query timeout"
- The query is taking too long (>30 seconds)
- Add
LIMITclauses to large queries - Consider adding database indexes for better performance
"Only read-only queries allowed"
- You're in read-only mode (this is good for safety!)
- Use the "Execute Read Query" tool instead
- Or disable read-only mode if you need write access
Metadata
Release files for mcp-sqlalchemy 0.2.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| mcp_sqlalchemy-0.2.4.tar.gz | 19.9 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mcp_sqlalchemy-0.2.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 37.6 kB
Release files / mcp_sqlalchemy-0.2.4.tar.gz
| Download URL | mcp_sqlalchemy-0.2.4.tar.gz |
|---|---|
| Size | 19.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4e928a2177dec98f23ec728524c11f934b0be12bb1800e4a88c679c8cc708cb6
|
|
BLAKE2b-256 checksum How to use checksums |
979d9549197b417d56d55f49d7a7ce79a853d07e2866f81df64c612b6194672a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 17, 2025.
Transparency logRelease files / mcp_sqlalchemy-0.2.4-py3-none-any.whl
| Download URL | mcp_sqlalchemy-0.2.4-py3-none-any.whl |
|---|---|
| Size | 17.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
37c8eefb2613861b2140a77dd2ce09feed302d4b3e4957d7633fd96ed3698b04
|
|
BLAKE2b-256 checksum How to use checksums |
13f0d2ee4c150fabe01d65bba3d2d20a40f24bd512a7f4d99d97ede1559e9191
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 17, 2025.
Transparency logSecurity for mcp-sqlalchemy 0.2.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for mcp_sqlalchemy-0.2.4.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 17, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.