AI agent for ClickHouse database analysis via MCP
Project description
ClickHouse MCP Agent
AI agent for ClickHouse database analysis via MCP (Model Context Protocol).
A single MCP server (mcp-clickhouse) driven by a single agent instance. Access restriction is performed via explicit allow-lists you pass per call (allowed_tables, allowed_databases), rather than managing multiple keys or fan-out across multiple agents.
Features
- Query ClickHouse databases using natural language with AI models
- Structured output:
analysis,confidence,sql_used - Easy connection management (predefined or custom)
- Conversational context with message-history pruning/summarization
- No CLI or external .env required — configure at runtime
- Access restriction via per-call allow-lists (
allowed_tables,allowed_databases) - Streamable results via
run_stream() - Persistent MCP server mode via
async with ClickHouseAgent() - Typed exception hierarchy for reliable error handling
Supported Providers
- OpenAI
- Anthropic
- Google Gemini
- Groq
- Mistral
- Cohere
Local Development (Docker)
The fastest way to get started — no cloud ClickHouse account needed:
# Start ClickHouse with seeded demo data (orders + products)
docker compose up -d
# Install the package with dev dependencies
pip install -e ".[dev]"
# Run the examples (set your API key first)
GOOGLE_API_KEY=... python examples/example_minimal.py
GOOGLE_API_KEY=... python examples/example_stream.py
The docker/init.sql file seeds demo.orders (25 orders, May 2026) and demo.products (10 products across Electronics, Sports, Home, Books) automatically on first start.
Quickstart
import asyncio
from agent.clickhouse_agent import ClickHouseAgent
from agent.config import config
config.set_ai_model("openai:gpt-4o-mini")
config.set_model_api_key("openai", "your_api_key_here")
config.set_clickhouse(host="localhost", port="8123", user="default", password="", secure="false")
async def main():
agent = ClickHouseAgent()
result = await agent.run(
allowed_tables=["orders", "products"],
allowed_databases=["demo"],
query="give me some insights on the recent data",
)
print("Analysis:", result.analysis)
print("Confidence:", result.confidence)
print("SQL used:", result.sql_used)
asyncio.run(main())
Persistent server (multiple queries)
Use the context manager to keep the MCP subprocess alive across calls — avoids subprocess startup overhead on every query:
async def main():
async with ClickHouseAgent() as agent:
r1 = await agent.run(query="how many orders were placed last week?")
r2 = await agent.run(query="which products are selling fastest?", message_history=r1.messages)
Switching providers
All providers use the same interface — just swap the model string and key:
# Anthropic
config.set_ai_model("anthropic:claude-haiku-4-5-20251001")
config.set_model_api_key("anthropic", "your_key")
# Google
config.set_ai_model("google-gla:gemini-2.0-flash")
config.set_model_api_key("google", "your_key")
# Groq
config.set_ai_model("groq:llama-3.3-70b-versatile")
config.set_model_api_key("groq", "your_key")
Message History & Summarization
Pass message_history between calls for multi-turn conversations. When token usage exceeds summarize_config.token_limit, older messages are automatically summarized into a compact form by a separate summarizer agent.
summarize_config.set_token_limit(10000)
summarize_config.set_ai_model("gemini-2.5-flash")
Output
Each call to ClickHouseAgent.run() returns a RunResult:
| Field | Description |
|---|---|
analysis |
Natural-language result text from the model |
confidence |
Confidence level (1–10) |
sql_used |
List of SQL strings executed during the run |
messages |
Full (possibly pruned/summarized) message history |
new_messages |
Only messages created in the latest turn |
last_message |
The last message in the conversation |
usage |
Token/usage statistics for the run |
Error Handling
All errors raise from a typed hierarchy so you can catch at the right level:
from agent.exceptions import ClickHouseMCPError, MCPConnectionError, AgentExecutionError
try:
result = await agent.run(query="...")
except MCPConnectionError:
# MCP subprocess failed to start or connection dropped
...
except AgentExecutionError:
# Agent logic failed during the run
...
except ClickHouseMCPError:
# Any library error
...
Requirements
- Python 3.10+
- AI API key for your provider (OpenAI, Anthropic, Google/Gemini, Groq, Mistral, Cohere)
All dependencies are managed via pyproject.toml.
Roadmap
✅ Done
- MCP integration via
pydantic_ai.mcp.MCPServerStdio - SQL generation/execution via MCP tools
- Schema inspection (databases/tables/columns)
- Config-driven connections (playground/local/custom)
- Access restriction via per-call allow-lists (
allowed_tables,allowed_databases) - Runtime provider/model selection and API key management
- Structured outputs (
ClickHouseOutput) andRunResultwithsql_used - Message history pruning/summarization
- Streaming results via
run_stream() - Persistent MCP server via
async with ClickHouseAgent() - Typed exception hierarchy
- Local development via Docker (
docker compose up -d) rufflinting, Python 3.13 support, CI hardened
⚙️ 0.11 — Agent API expansion
- Async batch queries (parallel queries in one call)
ClickHouseAgent.reset()for lifecycle control without re-instantiationstructlogoptional dep for structured observabilitypydantic-aiAPI audit and model ref updates (Claude 4 / GPT family)
🔒 0.12 — Stable
- API locked — no breaking changes without a major version
- All known bugs resolved
py.typedcheck added to CI
🔭 Post-1.0 — Future
- Database-agnostic abstraction (Elasticsearch, MongoDB, Postgres)
- FastAPI standalone deployment option
Contributing
Open an issue or pull request for features or fixes.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file clickhouse_mcp_agent-0.10.0.tar.gz.
File metadata
- Download URL: clickhouse_mcp_agent-0.10.0.tar.gz
- Upload date:
- Size: 17.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
73bc05fa9f5db16fda6f1dbc22695914978d2d7d5e3f445ebe512cb730cc5f96
|
|
| MD5 |
b7c7b7d044b76ed814bba71bc26ba85d
|
|
| BLAKE2b-256 |
0751983b47b7b279bc8466ee930d7f3a2215bc815c73518524b338b0cd3eeb7d
|
Provenance
The following attestation bundles were made for clickhouse_mcp_agent-0.10.0.tar.gz:
Publisher:
publish-pypi.yml on AranNomante/clickhousemcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
clickhouse_mcp_agent-0.10.0.tar.gz -
Subject digest:
73bc05fa9f5db16fda6f1dbc22695914978d2d7d5e3f445ebe512cb730cc5f96 - Sigstore transparency entry: 1630197358
- Sigstore integration time:
-
Permalink:
AranNomante/clickhousemcp@4a56f5d86d5d88eed0cca51317454b543d36167b -
Branch / Tag:
refs/tags/v0.10.0 - Owner: https://github.com/AranNomante
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@4a56f5d86d5d88eed0cca51317454b543d36167b -
Trigger Event:
release
-
Statement type:
File details
Details for the file clickhouse_mcp_agent-0.10.0-py3-none-any.whl.
File metadata
- Download URL: clickhouse_mcp_agent-0.10.0-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
633c9c150e29538a15e4115c2a5e42606f74d482e5254cf268448ddaa39ef52c
|
|
| MD5 |
11f19f41bd33327dadddea663dd77af5
|
|
| BLAKE2b-256 |
6ec25292fef458e9f338905d12ba861c0789673cd63be2d4fbfe105f5bf1787b
|
Provenance
The following attestation bundles were made for clickhouse_mcp_agent-0.10.0-py3-none-any.whl:
Publisher:
publish-pypi.yml on AranNomante/clickhousemcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
clickhouse_mcp_agent-0.10.0-py3-none-any.whl -
Subject digest:
633c9c150e29538a15e4115c2a5e42606f74d482e5254cf268448ddaa39ef52c - Sigstore transparency entry: 1630197363
- Sigstore integration time:
-
Permalink:
AranNomante/clickhousemcp@4a56f5d86d5d88eed0cca51317454b543d36167b -
Branch / Tag:
refs/tags/v0.10.0 - Owner: https://github.com/AranNomante
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@4a56f5d86d5d88eed0cca51317454b543d36167b -
Trigger Event:
release
-
Statement type: