QueryCraftMCP
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.
README
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
sqlite3library. - Easily configurable active backend using the
ACTIVE_DB_BACKENDenvironment 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_datafor PostgreSQL). - Raw SQL query execution (e.g.,
execute_raw_sqlfor PostgreSQL,execute_queryfor 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
.envfile 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/:
QueryCraftMCP/
├── src/
│ ├── init.py
│ ├── main.py # Main application entry point
│ │
│ └── db_backends/
│ ├── init.py
│ ├── postgres/ # PostgreSQL specific modules
│ │ ├── init.py
│ │ ├── lifespan.py
│ │ ├── schema_tools.py
│ │ └── query_tools.py
│ └── sqlite/ # SQLite specific modules
│ ├── init.py
│ ├── lifespan.py
│ ├── schema_tools.py
│ └── query_tools.py
│
├── .env # For local environment variables
├── requirements.txt
├── Dockerfile # For building Docker images (example provided in discussion)
└── .dockerignore # Specifies files to ignore in Docker build (example provided in discussion)
└── README.md
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:
git clone <your-repository-url> cd QueryCraftMCP -
Create a Virtual Environment (recommended):
python -m venv venv source venv/bin/activate # On Linux/macOS # venv\Scripts\activate # On Windows -
Install Dependencies:
pip install -r requirements.txtThe dependencies include
mcp[cli],asyncpg, andpython-dotenv. -
Configure Environment Variables: Create a
.envfile in the project root. Populate it with the necessary configurations:# .env # --- General Configuration --- # Choose the active database backend: "postgres" or "sqlite" ACTIVE_DB_BACKEND="postgres" # MCP Server Host and Port (used by main.py) MCP_HOST="0.0.0.0" MCP_PORT="8888" # The port your MCP server will listen on with SSE/HTTP # --- PostgreSQL Backend Configuration --- # Required if ACTIVE_DB_BACKEND is "postgres" POSTGRES_DATABASE_URL="postgresql://your_user:your_password@your_pg_host:5432/your_database" # --- SQLite Backend Configuration --- # Required if ACTIVE_DB_BACKEND is "sqlite" # This path is relative to where the application runs. # If running in Docker, this path will be inside the container. SQLITE_DATABASE_PATH="querycraft_data.db"- Replace placeholder values (like
your_user,your_password, etc.) with your actual credentials and paths. - The
MCP_HOSTandMCP_PORTare used byFastMCPwhen it's instantiated inmain.py.
- Replace placeholder values (like
Running the Application
1. Locally (Directly with Python)
Ensure your .env file is configured correctly.
python -m src.main
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):
docker build -t querycraftmcp .
Then, run the Docker container. You'll need to pass the environment variables.
Example for PostgreSQL Backend:
docker run -it --rm \
-p 8888:8888 \
-e ACTIVE_DB_BACKEND="postgres" \
-e POSTGRES_DATABASE_URL="postgresql://docker_user:docker_pass@host.docker.internal:5432/docker_db" \
-e MCP_HOST="0.0.0.0" \
-e MCP_PORT="8888" \
querycraftmcp
- 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:8888maps the host's port to the container's port.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。