clickhouse-mcp
by JakePeralta7
README.md
# clickhouse-mcp
An MCP server (stdio) for [ClickHouse](https://clickhouse.com). The database is
a **required argument on every database-touching tool call**, not server state:
one server can answer questions about many databases without reconnecting.
Built with the official [`mcp`](https://pypi.org/project/mcp/) SDK v2 and
[`clickhouse-connect`](https://pypi.org/project/clickhouse-connect/). Connection
details come from environment variables; the per-call `database` is validated
as a simple identifier and routed through the driver's per-query database
setting. It is never interpolated into SQL.
> **Safety model:** `execute_sql` is read-only by default. Statements other than
> `SELECT`, `SHOW`, `DESCRIBE`, `DESC`, `EXPLAIN`, `WITH` and `EXISTS` (and
> `SELECT ... INTO OUTFILE`) are rejected locally, and every query also carries
> ClickHouse's `readonly=1` and `allow_ddl=0` settings. Set
> `CLICKHOUSE_ALLOW_WRITE_ACCESS=true` to allow full SQL, including DDL and DML;
> the server then sends neither setting. These are defense-in-depth measures,
> not a substitute for access control in ClickHouse: deploy with a dedicated,
> least-privilege user, never `default` or an admin account. `readonly` does not
> block backups, and `allow_ddl` does not restrict grants. Queries are capped by
> `CLICKHOUSE_TIMEOUT` and `CLICKHOUSE_MAX_ROWS`, and the row cap uses a matching
> `max_block_size`; row-returning `SELECT`/`WITH`/`EXPLAIN` statements are also
> wrapped in an outer `LIMIT` so ClickHouse stops at the cap.
> Note that `readonly=1` rejects query-level `SETTINGS` clauses, so remove them
> or use a restricted ClickHouse profile. It can also reject reads needing table
> functions. In write-enabled mode the caps are
> best-effort because a query can override them with its own `SETTINGS`; enable
> writes only for trusted callers.
## Configuration (environment variables)
| Variable | Required | Default | Description |
| --- | --- | --- | --- |
| `CLICKHOUSE_HOST` | yes | — | ClickHouse hostname or IP |
| `CLICKHOUSE_USER` | yes | — | ClickHouse user |
| `CLICKHOUSE_PASSWORD` | no | empty | Password for the user |
| `CLICKHOUSE_PORT` | no | driver default | HTTP/HTTPS port (`8123` / `8443`) |
| `CLICKHOUSE_SECURE` | no | `false` | Use HTTPS/TLS |
| `CLICKHOUSE_VERIFY` | no | `true` | Verify TLS certificate |
| `CLICKHOUSE_TIMEOUT` | no | `30` | Connect/read timeout and max execution time, seconds |
| `CLICKHOUSE_MAX_ROWS` | no | `1000` | Hard cap on rows returned per call |
| `CLICKHOUSE_ALLOW_WRITE_ACCESS` | no | `false` | Allow DDL/DML through `execute_sql` |
There is intentionally **no** `CLICKHOUSE_DATABASE` variable. Booleans accept
`true/false/1/0/yes/no/on/off` (case-insensitive); invalid values abort startup.
`.env.example` is the canonical list of variables.
## Concurrency
One ClickHouse client serves the whole process, and the MCP SDK runs each
synchronous tool on a worker thread, so calls can overlap. The client is created
with `autogenerate_session_id=False`, the driver's supported mode for sharing a
client across threads; nothing here relies on session state, because every call
is a single statement and `database`, `readonly` and the row/time caps travel as
per-request settings.
## Tools
| Tool | Description | Read-only |
| --- | --- | --- |
| `list_databases(limit, offset)` | List databases visible to the configured user | yes |
| `list_tables(database, limit, offset)` | List tables in `database` | yes |
| `describe_table(database, table)` | Describe one table in `database` | yes |
| `execute_sql(database, sql, max_rows)` | Run one SQL statement in `database` | yes by default; writes opt-in |
`database` and `table` must be simple identifiers: letters, digits and
underscores, starting with a letter or underscore, at most 128 characters.
Quoted, dotted or Unicode names are rejected. This is a deliberate ceiling: it
keeps caller-controlled names safe without a parser. `sql` must contain exactly
one statement; trailing semicolons are ignored, semicolons inside string
literals are fine, and a second statement is rejected.
List tools return `{"items": [...], "returned_count": 50, "has_more": true,
"next_offset": 50, "query_id": "abc123def456"}`. `returned_count` is the number
of items in this page; when `has_more` is true, pass `next_offset` as the next
call's `offset`. `limit` defaults to 50 and values above `CLICKHOUSE_MAX_ROWS`
are clamped to that cap. `describe_table` returns the same item shape with
`returned_count`, `truncated` and `query_id`.
`execute_sql` returns:
```json
{
"database": "analytics",
"query_id": "abc123def456",
"columns": ["event", "count"],
"data": [["click", 42]],
"row_count": 1,
"truncated": false,
"summary": "OK",
"statistics": {"read_rows": 100, "elapsed_ns": 1234567}
}
```
Non-finite Float values (`nan`, `inf`, `-inf`) are returned as `null` so the
payload remains valid JSON.
For DDL/DML, `columns` and `data` are empty, `row_count` is 0, and `summary`
carries the driver's result. Each call gets a correlation ID that is also sent
to ClickHouse as `query_id`, so stderr logs and `system.query_log` line up.
Logs go to stderr only; stdout is reserved for MCP stdio transport. SQL arguments
are never logged, and credentials/long literals in driver messages are
redacted while short table, column and setting names are kept for diagnosis.
Failures are surfaced as MCP tool errors with a code prefix:
`INVALID_INPUT` (including invalid database/table names), `QUERY_FAILED` or
`CONFIG_ERROR`. Driver messages are redacted before they reach the model.
## Required ClickHouse privileges
- `list_databases` and `list_tables` read `system.databases` and `system.tables`;
the configured user needs access to those system tables.
- `describe_table` runs `DESCRIBE TABLE`, which requires the corresponding
`SHOW`/metadata privilege on the table.
- `execute_sql` needs `SELECT` for read queries, plus the relevant DDL/DML
grants when `CLICKHOUSE_ALLOW_WRITE_ACCESS=true`.
- Use a dedicated user/profile. System tables may expose names beyond the
user's query grants, and ClickHouse remains the authoritative access control.
## Example calls
```json
{"tool": "list_tables", "arguments": {"database": "analytics", "limit": 50, "offset": 0}}
```
```json
{"tool": "execute_sql", "arguments": {"database": "analytics", "sql": "SELECT event, count FROM events LIMIT 10", "max_rows": 10}}
```
```json
{"tool": "execute_sql", "arguments": {"database": "analytics", "sql": "SELECT * FROM events SETTINGS max_threads=4"}}
```
The last example is rejected in read-only mode because query-level `SETTINGS`
are disabled; remove the clause or use a restricted profile.
## Example client configuration
```json
{
"mcpServers": {
"clickhouse": {
"command": "uvx",
"args": [
"--from",
"git+https://github.com/JakePeralta7/clickhouse-mcp.git",
"clickhouse-mcp"
],
"env": {
"CLICKHOUSE_HOST": "clickhouse.internal",
"CLICKHOUSE_USER": "mcp_reader",
"CLICKHOUSE_PASSWORD": "from-your-secret-store"
}
}
}
}
```
Docker:
```json
{
"mcpServers": {
"clickhouse": {
"command": "docker",
"args": ["run", "--rm", "-i",
"-e", "CLICKHOUSE_HOST", "-e", "CLICKHOUSE_USER",
"-e", "CLICKHOUSE_PASSWORD", "ghcr.io/jakeperalta7/clickhouse-mcp:latest"]
}
}
}
```
## Development
- `uv sync --group dev`
- `uv run pytest -q` (tests, including the tool-contract checks)
- `uv run pyright` (type check)
- `uv run python -m clickhouse_mcp` (fails fast without host/user)
- `docker compose up -d` then `$env:CLICKHOUSE_INTEGRATION="1"; uv run pytest -q -m integration`
for live checks against the seeded ClickHouse
- `evals/README.md` for the ten-question evaluation set
See `AGENTS.md` for the module layout, verification tiers and the add-a-tool
recipe.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues