Supports containerized deployment via Docker, with configuration through environment variables to connect to database backends when running in containers.
Uses .env files for configuration management, allowing easy setup of database connections and server parameters through environment variables.
Allows asynchronous interaction with PostgreSQL databases, including schema discovery, listing databases, tables and views, exploring column details, and executing both structured searches and raw SQL queries.
Provides tools for interacting with SQLite databases, including retrieving full table DDL schemas and executing SQL queries against SQLite database files.
QueryCraftMCP
QueryCraftMCP is a flexible Model Context Protocol (MCP) server designed to bridge the gap between Large Language Models (LLMs) and various database systems. It allows LLMs or other MCP clients to dynamically discover database schemas, execute queries, and retrieve data through a standardized protocol. The server supports multiple database backends (currently PostgreSQL and SQLite), selectable via configuration.
Features
- Multi-Database Backend Support:
- PostgreSQL: Asynchronous interaction using
asyncpg
, including connection pooling via lifespan management. - SQLite: Synchronous interaction using the built-in
sqlite3
library. - Easily configurable active backend using the
ACTIVE_DB_BACKEND
environment variable.
- PostgreSQL: Asynchronous interaction using
- Dynamic Tool Loading: Tools and database connection lifespans are dynamically loaded based on the configured backend.
- Comprehensive Database Interaction Tools:
- Schema Discovery: Tools to list available databases (PostgreSQL), database objects (tables/views) (PostgreSQL), and object columns (PostgreSQL). For SQLite, a tool to retrieve the full table DDL schema is provided.
- Data Querying:
- Structured search capabilities (e.g.,
search_data
for PostgreSQL). - Raw SQL query execution (e.g.,
execute_raw_sql
for PostgreSQL,execute_query
for SQLite) with security considerations.
- Structured search capabilities (e.g.,
- Lifespan Management: Robust management of database connections throughout the application lifecycle.
- Transport Protocol: Utilizes Server-Sent Events (SSE) for MCP communication.
- Configuration: Primarily through
.env
file and environment variables. - Docker Support: Includes a
Dockerfile
(suggested in discussion, not uploaded) for easy containerization and deployment.
Project Structure
The project follows a src/
layout, with database-specific implementations organized under src/db_backends/
:
Prerequisites
- Python 3.9+
- Docker (if running via Docker)
- Access to a PostgreSQL server (if using the PostgreSQL backend)
- A writable directory for the SQLite database file (if using the SQLite backend)
Setup
- Clone the Repository:
- Create a Virtual Environment (recommended):
- Install Dependencies:The dependencies include
mcp[cli]
,asyncpg
, andpython-dotenv
. - Configure Environment Variables:
Create a
.env
file in the project root. Populate it with the necessary configurations:- Replace placeholder values (like
your_user
,your_password
, etc.) with your actual credentials and paths. - The
MCP_HOST
andMCP_PORT
are used byFastMCP
when it's instantiated inmain.py
.
- Replace placeholder values (like
Running the Application
1. Locally (Directly with Python)
Ensure your .env
file is configured correctly.
The server will start using the configured ACTIVE_DB_BACKEND and will listen on the host and port specified by MCP_HOST and MCP_PORT (defaulting to 0.0.0.0:8888) using SSE transport.
- Using Docker First, build the Docker image (assuming you have a Dockerfile like the one suggested in prior discussions):
Then, run the Docker container. You'll need to pass the environment variables.
Example for PostgreSQL Backend:
- Replace docker_user, docker_pass, docker_db with your actual PostgreSQL credentials.
- host.docker.internal can be used to connect to a PostgreSQL server running on your host machine from within the Docker container (on Docker Desktop for Mac/Windows).
-p 8888:8888
maps the host's port to the container's port.
This server cannot be installed
A flexible Model Context Protocol server that enables LLMs to interact with database systems, supporting dynamic schema discovery and query execution across PostgreSQL and SQLite backends.
Related MCP Servers
- -securityAlicense-qualityA Model Context Protocol server providing LLMs read-only access to PostgreSQL databases for inspecting schemas and executing queries.Last updated -16,94817JavaScriptMIT License
- -securityAlicense-qualityA Model Context Protocol server that provides read-only access to PostgreSQL databases with enhanced multi-schema support, allowing LLMs to inspect database schemas across multiple namespaces and execute read-only queries while maintaining schema isolation.Last updated -132JavaScriptMIT License
- -securityFlicense-qualityA Model Context Protocol server providing both read and write access to PostgreSQL databases, enabling LLMs to query data, modify records, and manage database schemas.Last updated -4JavaScript
- -securityFlicense-qualityA Model Context Protocol server that provides LLMs with full read-write access to PostgreSQL databases, allowing both querying and modifying database content with transaction management and safety controls.Last updated -847TypeScript