Skip to main content

ClickHouse MCP Agent

PyPI pydantic-ai-slim mcp-clickhouse

AI agent for ClickHouse database analysis via MCP (Model Context Protocol).

A single MCP server (mcp-clickhouse) driven by a single agent instance. You can scope what the agent discovers per call with allow-lists (allowed_tables, allowed_databases). See Security for what they do and do not restrict.

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
  • Per-call discovery scoping via allow-lists (allowed_tables, allowed_databases)
  • Read-only queries by default (enforced by mcp-clickhouse)
  • Streamable results via run_stream()
  • Persistent MCP server mode via async with ClickHouseAgent()
  • Parallel queries via run_batch()
  • Lifecycle reset via reset()
  • Query result cache via enable_cache=True
  • Typed exception hierarchy for reliable error handling
  • Optional structlog integration (pip install ".[logging]")

Supported Providers

Provider Key env var Notes
Google Gemini GOOGLE_API_KEY Default
OpenAI OPENAI_API_KEY
Anthropic ANTHROPIC_API_KEY
Grok (xAI) XAI_API_KEY Model prefix xai: (e.g. xai:grok-4)
Groq GROQ_API_KEY Fast open models (e.g. groq:llama-3.3-70b-versatile), free tier
Mistral MISTRAL_API_KEY
Cohere CO_API_KEY

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 Google API key first)
GOOGLE_API_KEY=... python examples/example_minimal.py
GOOGLE_API_KEY=... python examples/example_stream.py
GOOGLE_API_KEY=... python examples/example_0_11.py
GOOGLE_API_KEY=... python examples/example_integration.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)

Parallel queries

async with ClickHouseAgent() as agent:
    results = await agent.run_batch(
        ["how many orders?", "total revenue?", "top 5 products?"],
        allowed_databases=["demo"],
    )
    for r in results:
        print(r.analysis)

Query result cache

agent = ClickHouseAgent(enable_cache=True)
result = await agent.run(query="how many orders?", allowed_databases=["demo"])
# identical call returns instantly from cache (stateless queries only)

Lifecycle reset

agent = ClickHouseAgent()
await agent.run(query="...")
await agent.reset()          # tear down MCP subprocess
await agent.run(query="...")  # re-initializes on next call

Switching providers

All providers use the same interface — just swap the model string and key:

# Anthropic Claude 4
config.set_ai_model("anthropic:claude-sonnet-4-6")
config.set_model_api_key("anthropic", "your_key")

# Google Gemini
config.set_ai_model("google:gemini-3.1-flash-lite")
config.set_model_api_key("google", "your_key")

# Grok (xAI)
config.set_ai_model("xai:grok-4")
config.set_model_api_key("xai", "your_key")

# Groq (fast open models, free tier — good for testing)
config.set_ai_model("groq:llama-3.3-70b-versatile")
config.set_model_api_key("groq", "your_key")

Security

Queries are read-only. mcp-clickhouse runs every query with ClickHouse's readonly setting unless CLICKHOUSE_ALLOW_WRITE_ACCESS=true is set for the server, so the agent cannot modify data.

Allow-lists scope discovery; they are not an access control. allowed_databases and allowed_tables filter what the list_databases and list_tables tools return, which keeps the model focused on the right tables. They do not inspect the SQL passed to run_query: a model that writes SELECT * FROM other_db.other_table directly can still read it.

To restrict what can be read, use ClickHouse permissions. Connect with a ClickHouse user that only has SELECT grants on the data the agent may see:

CREATE USER agent_reader IDENTIFIED BY '...';
GRANT SELECT ON demo.orders TO agent_reader;
GRANT SELECT ON demo.products TO agent_reader;

ClickHouse then rejects any query outside those grants, whatever SQL the model generates.

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("google:gemini-3.1-flash-lite")

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+
  • An AI provider API key (Google, OpenAI, Anthropic, Grok, Groq, Mistral, or Cohere)

All dependencies are managed via pyproject.toml.

Roadmap

✅ Done (0.11.x)

  • MCP integration via pydantic_ai.mcp.MCPServerStdio
  • SQL generation/execution via MCP tools
  • Schema inspection (databases/tables/columns)
  • Config-driven connections (playground/local/custom)
  • Discovery scoping via per-call allow-lists (allowed_tables, allowed_databases)
  • Runtime provider/model selection and API key management
  • Structured outputs (ClickHouseOutput) and RunResult with sql_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)
  • ruff linting, Python 3.13 support, CI hardened
  • Async batch queries via run_batch()
  • reset() for lifecycle control
  • Query result cache (enable_cache=True)
  • structlog optional dep (pip install ".[logging]")

✅ 0.12 — Stable

  • API locked — no breaking changes without a major version
  • All known bugs resolved
  • py.typed check added to CI
  • mypy agent/ added to CI matrix
  • Updated to mcp-clickhouse 0.4.0; list_databases tool now enforced by allowed_databases allow-list
  • Default model updated to google:gemini-3.1-flash-lite

✅ 0.12.1 — Fixes

  • Allow-list filtering now works against real mcp-clickhouse responses (previously bypassed)
  • Contract tests against the real mcp-clickhouse server, and live tests against ClickHouse in CI
  • Grok (xAI) and Groq provider setup corrected

🚧 Next

  • Migrate to pydantic-ai 2.x (MCPToolset) and mcp-clickhouse 0.7
  • role and query timeout options (ClickHouse-enforced access control)

🔭 Post-1.0 — Future

  • FastAPI standalone deployment option

Contributing

Open an issue or pull request for features or fixes.

Metadata

Release files for clickhouse-mcp-agent 0.12.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for clickhouse-mcp-agent 0.12.1
File Size Uploaded Provenance Actions
clickhouse_mcp_agent-0.12.1.tar.gz 24.8 kB With provenance Details for clickhouse_mcp_agent-0.12.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for clickhouse-mcp-agent 0.12.1
File Interpreter ABI Platform Actions
clickhouse_mcp_agent-0.12.1-py3-none-any.whl Python 3 none any Details for clickhouse_mcp_agent-0.12.1-py3-none-any.whl

Total release size: 41.1 kB

Release files / clickhouse_mcp_agent-0.12.1.tar.gz

Download URL clickhouse_mcp_agent-0.12.1.tar.gz
Size 24.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c3cdaa2c3c0ff37e11ed15ea353619ba2fa1bf9adb6a48b70c9e0ba5bbc17b42
BLAKE2b-256 checksum
How to use checksums
cd1af68020b8080ab57e05124ee1d164075a268958b81599b837a9f2c0cf11b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 28, 2026.

Transparency log

Release files / clickhouse_mcp_agent-0.12.1-py3-none-any.whl

Download URL clickhouse_mcp_agent-0.12.1-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
620bb01a846c1cce28d90493a739bb5bb17065f45b69854e4a282f617a230ea8
BLAKE2b-256 checksum
How to use checksums
2aa83825d15971ac0973688f7dd4f4db92f746d476982cff64abdc2cea8f30ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 28, 2026.

Transparency log

Security for clickhouse-mcp-agent 0.12.1

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

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for clickhouse_mcp_agent-0.12.1.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 28, 2026.

Transparency log

A 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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page