A comprehensive Model Context Protocol (MCP) server for interacting with ClickHouse databases. This server provides agent-friendly tools for database exploration, query execution, schema management, and performance monitoring.
- List Databases: Explore all available databases with metadata
- Database Statistics: Get comprehensive stats about database usage
- System Metrics: Monitor ClickHouse performance and activity
- List Tables: Browse tables with row counts and size information
- Search Tables: Find tables using pattern matching across databases
- Get Schema: Retrieve detailed table structure with columns, types, and indexes
- Execute Query: Run SELECT, INSERT, UPDATE, DELETE, and DDL queries
- Parameterized Queries: Secure query execution with parameter binding
- Response Formats: Get results in JSON or Markdown format
- Automatic Pagination: Handle large result sets efficiently
- Character Limit Management: Automatic truncation of large responses
- Error Handling: Helpful error messages for common issues
- Connection Pooling: Efficient resource management
- Multiple Response Formats: JSON for machines, Markdown for humans
- Python 3.10 or higher
- ClickHouse server (local or cloud, version 20.0+, tested with 25.7+)
- MCP-compatible client (Claude Desktop, Cursor, etc.)
# Clone the repository
git clone https://github.com/akworob/clickhouse-mcp-server.git
cd clickhouse-mcp-server
# Create and activate virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtpip install -r requirements.txtOr install packages individually:
pip install mcp clickhouse-connect httpx pydanticConfigure your ClickHouse connection using environment variables:
# Required
export CLICKHOUSE_HOST="localhost" # ClickHouse server hostname
export CLICKHOUSE_USER="default" # Username
export CLICKHOUSE_PASSWORD="your-password" # Password
# Optional
export CLICKHOUSE_PORT="8443" # Port (8443 for HTTPS, 9000 for native)
export CLICKHOUSE_DATABASE="default" # Default database
export CLICKHOUSE_SECURE="true" # Use secure connection (HTTPS/TLS)Create a .env file in the same directory:
CLICKHOUSE_HOST=my-clickhouse.cloud
CLICKHOUSE_PORT=8443
CLICKHOUSE_USER=default
CLICKHOUSE_PASSWORD=SecurePassword123
CLICKHOUSE_DATABASE=analytics
CLICKHOUSE_SECURE=trueStart the MCP server:
python clickhouse_mcp.pyAdd to your Claude Desktop configuration (claude_desktop_config.json):
Using Virtual Environment (Recommended):
{
"mcpServers": {
"clickhouse": {
"command": "/absolute/path/to/mcp_clickhouse/venv/bin/python",
"args": ["/absolute/path/to/mcp_clickhouse/clickhouse_mcp.py"],
"env": {
"CLICKHOUSE_HOST": "localhost",
"CLICKHOUSE_PORT": "8123",
"CLICKHOUSE_USER": "default",
"CLICKHOUSE_PASSWORD": "your-password",
"CLICKHOUSE_SECURE": "false"
}
}
}
}Using System Python:
{
"mcpServers": {
"clickhouse": {
"command": "python",
"args": ["/absolute/path/to/clickhouse_mcp.py"],
"env": {
"CLICKHOUSE_HOST": "localhost",
"CLICKHOUSE_PORT": "8123",
"CLICKHOUSE_USER": "default",
"CLICKHOUSE_PASSWORD": "your-password",
"CLICKHOUSE_SECURE": "false"
}
}
}
}Note: Replace /absolute/path/to/ with the actual path to your installation. Use absolute paths for reliability.
Add to your Cursor MCP settings:
{
"mcp": {
"servers": {
"clickhouse": {
"command": "python",
"args": ["clickhouse_mcp.py"],
"cwd": "/path/to/server",
"env": {
"CLICKHOUSE_HOST": "localhost",
"CLICKHOUSE_USER": "default",
"CLICKHOUSE_PASSWORD": "your-password"
}
}
}
}
}List all databases with metadata.
Example Request:
List all ClickHouse databases
List tables in a specific database.
Example Request:
Show all tables in the analytics database
Get detailed schema information for a table.
Example Request:
Get the schema for the events table in analytics database
Execute SQL queries with optional parameters.
Example Requests:
Run query: SELECT count(*) FROM analytics.events WHERE date >= '2024-01-01'
Execute parameterized query:
SELECT * FROM users WHERE age > {min_age:Int32}
with parameters: {"min_age": 18}
Get comprehensive statistics about a database.
Example Request:
Get statistics for the analytics database
Search for tables matching a pattern.
Example Request:
Find all tables with "user" in the name
Get current system metrics and performance indicators.
Example Request:
Show ClickHouse system metrics
Create a local ClickHouse instance for testing:
version: '3.8'
services:
clickhouse:
image: clickhouse/clickhouse-server:latest
container_name: clickhouse-mcp
ports:
- "8123:8123" # HTTP interface
- "9000:9000" # Native protocol
- "8443:8443" # HTTPS interface
environment:
CLICKHOUSE_DB: default
CLICKHOUSE_USER: default
CLICKHOUSE_PASSWORD: changeme
CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT: 1
volumes:
- clickhouse-data:/var/lib/clickhouse
- clickhouse-logs:/var/log/clickhouse-server
ulimits:
nofile:
soft: 262144
hard: 262144
volumes:
clickhouse-data:
clickhouse-logs:Start the container:
docker-compose up -dTest the connection:
curl 'http://localhost:8123/' --data 'SELECT version()'-
List all databases:
Agent: "Show me all databases" -
Explore a specific database:
Agent: "List tables in the analytics database" -
Examine table structure:
Agent: "Get schema for the events table in analytics" -
Query data:
Agent: "Select top 10 rows from analytics.events ordered by timestamp"
-
Check database statistics:
Agent: "Get statistics for the production database" -
Find related tables:
Agent: "Search for tables containing 'user' in any database" -
Run analytical query:
Agent: "Calculate daily active users for the last 30 days" -
Monitor performance:
Agent: "Show current system metrics"
-
Authentication: Always use strong passwords and consider using certificate-based authentication for production.
-
Network Security: Use HTTPS/TLS connections (port 8443) for encrypted communication.
-
Query Parameters: Always use parameterized queries to prevent SQL injection:
# Good - parameterized query = "SELECT * FROM users WHERE id = {user_id:Int32}" params = {"user_id": 123} # Bad - string concatenation query = f"SELECT * FROM users WHERE id = {user_id}"
-
Access Control: Configure ClickHouse user permissions appropriately:
- Use read-only users for analytics
- Restrict access to system tables
- Implement row-level security where needed
- Use LIMIT: Always include LIMIT clauses for exploratory queries
- Add Indexes: Create appropriate indexes for frequently queried columns
- Partition Tables: Use partitioning for time-series data
- Materialized Views: Pre-compute expensive aggregations
- Connection Pooling: The MCP server maintains a persistent connection
Error: "Failed to initialize ClickHouse connection"
- Check hostname and port are correct
- Verify ClickHouse server is running
- Test with
clickhouse-clientorcurl
Error: "Access denied"
- Verify username and password
- Check user permissions in ClickHouse
- Ensure user has access to specified database
Error: "SQL syntax error"
- Check query syntax matches ClickHouse SQL dialect
- Verify table and column names exist
- Use backticks for reserved words
Error: "Query timeout"
- Add appropriate WHERE clauses to limit data
- Use sampling for large tables
- Consider creating materialized views
Server not appearing in client:
- Check MCP configuration file syntax
- Verify Python path is correct
- Check server starts without errors:
python clickhouse_mcp.py --help
Contributions are welcome! Areas for improvement:
-
Additional Tools:
- Backup and restore operations
- User management tools
- Cluster management
- Query optimization suggestions
-
Enhanced Features:
- Query result caching
- Query history tracking
- Cost estimation for queries
- Visual query builder
-
Integrations:
- Support for ClickHouse Cloud API
- Integration with monitoring tools
- Export to various formats (CSV, Parquet)
MIT License - See LICENSE file for details.
For issues, questions, or suggestions:
- Check the troubleshooting section
- Review ClickHouse documentation: https://clickhouse.com/docs
- Check MCP documentation: https://modelcontextprotocol.io
- Open an issue on GitHub
- Initial release
- Core database management tools
- Query execution with parameterization
- Multiple response formats
- System metrics monitoring
- Comprehensive error handling