A read-only Model Context Protocol (MCP) server for ClickHouse: metadata discovery, parameterized queries, and execution plan analysis.
Project description
MCP ClickHouse Tool
A read-only Model Context Protocol (MCP) server for ClickHouse that supports metadata discovery, parameterized queries, and query analysis, with profile-based configuration and strict no-DML/DDL enforcement.
Requirements: Python 3.13+, a running ClickHouse instance, and connection details via environment variables.
Quick start
Set a DSN and run the server with MCP Inspector:
# Option 1: Run directly with uvx (no clone needed)
export MCP_CLICKHOUSE_DSN="http://default:@localhost:8123/default"
npx -y @modelcontextprotocol/inspector uvx mcp-clickhousex
# Option 2: Run from source (clone repo, then)
export MCP_CLICKHOUSE_DSN="http://default:@localhost:8123/default"
npx -y @modelcontextprotocol/inspector uv run main.py
Configuration
Connection and behavior are configured via environment variables. The server supports multiple named profiles and a backward-compatible flat layer for single-connection setups.
Single connection (flat env vars)
Flat vars create or override the default profile. This is all you need for a single ClickHouse instance:
export MCP_CLICKHOUSE_DSN="http://user:password@host:8123/database"
export MCP_CLICKHOUSE_DESCRIPTION="Primary cluster" # optional
export MCP_CLICKHOUSE_QUERY_MAX_ROWS="5000" # default: 5000 (capped at 50000)
export MCP_CLICKHOUSE_QUERY_COMMAND_TIMEOUT_SECONDS="30" # default: 30 (capped at 300)
Multiple profiles (structured env vars)
To connect to more than one ClickHouse instance, use the MCP_CLICKHOUSE_PROFILES_<NAME>_ prefix. Profile names must be alphanumeric (no underscores) and are case-insensitive.
# Default profile
export MCP_CLICKHOUSE_PROFILES_DEFAULT_DSN="http://user:pass@primary:8123/mydb"
export MCP_CLICKHOUSE_PROFILES_DEFAULT_DESCRIPTION="Primary cluster"
export MCP_CLICKHOUSE_PROFILES_DEFAULT_QUERY_MAX_ROWS="5000"
export MCP_CLICKHOUSE_PROFILES_DEFAULT_QUERY_COMMAND_TIMEOUT_SECONDS="60"
# Named profile
export MCP_CLICKHOUSE_PROFILES_WAREHOUSE_DSN="http://user:pass@warehouse:8123/analytics"
export MCP_CLICKHOUSE_PROFILES_WAREHOUSE_DESCRIPTION="Analytics warehouse"
export MCP_CLICKHOUSE_PROFILES_WAREHOUSE_QUERY_MAX_ROWS="10000"
export MCP_CLICKHOUSE_PROFILES_WAREHOUSE_QUERY_COMMAND_TIMEOUT_SECONDS="120"
Per-profile fields:
| Suffix | Description | Default |
|---|---|---|
DSN |
Connection DSN (required) | http://default:@localhost:8123/default |
DESCRIPTION |
Human-readable label | — |
QUERY_MAX_ROWS |
Row cap per query | 5000 (max 50000) |
QUERY_COMMAND_TIMEOUT_SECONDS |
Query timeout | 30 (max 300) |
Merge rule: Flat vars always feed into the default profile. If both MCP_CLICKHOUSE_PROFILES_DEFAULT_* and flat vars are set, flat vars win on conflict.
Max rows is applied to every query (server-side via max_result_rows); results may be truncated with a truncated and row_limit field in the response.
Tools
| Tool | Description | Key params |
|---|---|---|
list_profiles |
List configured profiles (name and optional description). | — |
get_cluster_properties |
Get cluster (node) version and execution limits for a profile. | profile (optional) |
run_query |
Execute a read-only SELECT and return tabular results. Database/table must be specified in the SQL (e.g. db.table). Applies the profile's max_rows limit. |
sql, parameters, profile (optional) |
analyze_query |
Analyze a read-only SELECT via EXPLAIN (plan with index usage, pipeline, syntax). Does not execute the query. | sql, parameters, types, database, profile (all optional except sql) |
list_databases |
List all databases. | profile (optional) |
list_tables |
List tables and views in a database. Returns name, engine, primary_key, sorting_key, partition_key, total_rows, total_bytes for query analysis. | database, profile (optional) |
list_columns |
List columns for a table or view. Table may be qualified as database.table. |
table, database, profile (optional) |
Response shapes:
run_query,list_databases,list_tables,list_columns—{ columns, rows }.run_querymay includetruncatedandrow_limitwhen capped.analyze_query—{ plan?, pipeline?, syntax? }keyed by requested EXPLAIN type; each value is text.list_profiles— list of{ name, description }.get_cluster_properties—{ version, limits }wherelimits.queryincludesmax_rows,hard_row_limit,command_timeout_seconds.
run_query validates that the SQL is a single, read-only SELECT (or WITH … SELECT). INSERT, UPDATE, DELETE, DDL, and multi-statement batches are rejected.
Security
Read-only (SELECT only); parameterized queries supported (%(name)s or {name:Type} syntax). Use environment variables for connection credentials — never commit secrets.
MCP host examples
Snippets for common MCP clients using uvx mcp-clickhousex (no clone required; ensure uv is on your PATH). Replace connection details as needed.
Cursor
{
"mcpServers": {
"clickhouse": {
"command": "uvx",
"args": ["mcp-clickhousex"],
"env": {
"MCP_CLICKHOUSE_DSN": "http://default:@localhost:8123/default"
}
}
}
}
Codex
[mcp_servers.clickhouse]
command = "uvx"
args = ["mcp-clickhousex"]
[mcp_servers.clickhouse.env]
MCP_CLICKHOUSE_DSN = "http://default:@localhost:8123/default"
OpenCode
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"clickhouse": {
"type": "local",
"enabled": true,
"command": ["uvx", "mcp-clickhousex"],
"environment": {
"MCP_CLICKHOUSE_DSN": "http://default:@localhost:8123/default"
}
}
}
}
GitHub Copilot (agent)
{
"inputs": [],
"servers": {
"clickhouse": {
"type": "stdio",
"command": "uvx",
"args": ["mcp-clickhousex"],
"env": {
"MCP_CLICKHOUSE_DSN": "http://default:@localhost:8123/default"
}
}
}
}
Config file locations: Cursor .cursor/mcp.json, Codex/Copilot/OpenCode vary by client; see your client's MCP docs.
Tests
Tests require a running ClickHouse instance. The test suite creates a sample table in the default database, seeds it, and drops it after.
# Run all tests (unit + functional + e2e)
uv run pytest tests/ -v
The test harness uses MCP_TEST_CLICKHOUSE_DSN to locate the ClickHouse instance. If unset, it falls back to http://admin:password123@localhost:8123/default. Set the variable to point tests at a different server without affecting your production MCP_CLICKHOUSE_DSN:
export MCP_TEST_CLICKHOUSE_DSN="http://user:pass@testhost:8123/default"
uv run pytest tests/ -v
Roadmap
No planned features at this time. Open an issue to suggest improvements.
Contributing
Open issues or PRs; follow existing style and add tests where appropriate.
License
MIT. See LICENSE.
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 mcp_clickhousex-0.2.0a1.tar.gz.
File metadata
- Download URL: mcp_clickhousex-0.2.0a1.tar.gz
- Upload date:
- Size: 68.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
562497d51227abd22a86b50fffe8e80f1b212d7d3334723e0bdfdd724ca95914
|
|
| MD5 |
86d6c089fe9be2f9d9b031ea7151db31
|
|
| BLAKE2b-256 |
3988881f1f7fb1b3c4ca53ae4d147e7c6c9e40003b10803aa351fa625943aa52
|
Provenance
The following attestation bundles were made for mcp_clickhousex-0.2.0a1.tar.gz:
Publisher:
ci.yml on alyiox/mcp-clickhousex
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcp_clickhousex-0.2.0a1.tar.gz -
Subject digest:
562497d51227abd22a86b50fffe8e80f1b212d7d3334723e0bdfdd724ca95914 - Sigstore transparency entry: 1109394474
- Sigstore integration time:
-
Permalink:
alyiox/mcp-clickhousex@0b06db93d601649d779287881929485417c890c7 -
Branch / Tag:
refs/tags/0.2.0a1 - Owner: https://github.com/alyiox
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@0b06db93d601649d779287881929485417c890c7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file mcp_clickhousex-0.2.0a1-py3-none-any.whl.
File metadata
- Download URL: mcp_clickhousex-0.2.0a1-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b6514d0a8771344e2b793df5fd4fc31892a4a78aec29e008726ebabf83c23f4
|
|
| MD5 |
557ebb3e8c8ce56630900be38142297d
|
|
| BLAKE2b-256 |
e4a6983ebb92aeea679293501c5f926811b0b6358c1a258b098e23a5787cb7f7
|
Provenance
The following attestation bundles were made for mcp_clickhousex-0.2.0a1-py3-none-any.whl:
Publisher:
ci.yml on alyiox/mcp-clickhousex
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcp_clickhousex-0.2.0a1-py3-none-any.whl -
Subject digest:
7b6514d0a8771344e2b793df5fd4fc31892a4a78aec29e008726ebabf83c23f4 - Sigstore transparency entry: 1109394478
- Sigstore integration time:
-
Permalink:
alyiox/mcp-clickhousex@0b06db93d601649d779287881929485417c890c7 -
Branch / Tag:
refs/tags/0.2.0a1 - Owner: https://github.com/alyiox
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@0b06db93d601649d779287881929485417c890c7 -
Trigger Event:
push
-
Statement type: