Labsco
oualib logo

ClickHouse Cloud & On-Prem

β˜… 7

from oualib

A server for managing ClickHouse databases and ClickHouse Cloud services.

πŸ”₯πŸ”₯πŸ”₯πŸ”₯βœ“ VerifiedAccount requiredAdvanced setup

MCP ClickHouse: Database Operations + Cloud Management

PyPI - Version Python 3.12+ License Code style: black Ruff

A comprehensive Model Context Protocol (MCP) server that provides two distinct capabilities:

  1. Database Operations - Connect to and query any ClickHouse database (local, cloud, or self-hosted)
  2. Cloud Management - Complete ClickHouse Cloud infrastructure management via API

πŸ“š Table of Contents

🎯 Choose Your Use Case

This MCP server supports two independent use cases. You can use one or both:

πŸ“Š Database Operations Only

For: Data analysis, querying, and exploration of ClickHouse databases

  • Connect to any ClickHouse instance (local, self-hosted, or ClickHouse Cloud)
  • Execute read-only queries safely
  • Explore database schemas and metadata
  • Setup: Database connection credentials only

☁️ Cloud Management Only

For: Managing ClickHouse Cloud infrastructure programmatically

  • Create, configure, and manage cloud services
  • Handle API keys, members, and organizations
  • Monitor usage, costs, and performance
  • Setup: ClickHouse Cloud API keys only

πŸ”„ Both Combined

For: Complete ClickHouse workflow from infrastructure to data

  • Manage cloud services AND query the databases within them
  • End-to-end data pipeline management
  • Setup: Both database credentials and cloud API keys

🌟 Why This Server?

This repository significantly improves over the original ClickHouse MCP server:

FeatureOriginal Server (v0.1.10)This Server
Database Operations3 basic tools3 enhanced tools with safety features
Query Security❌ run_select_query allows ANY SQL operationβœ… Proper query filtering and readonly mode
Cloud Management❌ Noneβœ… 50+ comprehensive tools (100% API coverage)
Safety Controls❌ No protection against destructive operationsβœ… Advanced readonly modes for both database and cloud operations
Code QualityBasicProduction-ready with proper structure
ConfigurationLimited optionsFlexible setup for any use case
Error HandlingBasicRobust with detailed error messages
SSL SupportLimitedFull SSL configuration options

[!WARNING] Security Notice: The original ClickHouse MCP server (v0.1.10) has a critical security flaw where run_select_query can execute ANY SQL operation including DROP, DELETE, INSERT, etc., despite its name suggesting it only runs SELECT queries. This server implements proper query filtering and safety controls.

✨ Capabilities Overview

πŸ“Š Database Operations (3 Tools)

Connect to and query any ClickHouse database:

  • List databases and tables with detailed metadata
  • Execute SELECT queries with safety guarantees (read-only mode)
  • Explore schemas including column types, row counts, and table structures
  • Works with: Local ClickHouse, self-hosted instances, ClickHouse Cloud databases, and the free SQL Playground

☁️ Cloud Management (50+ Tools)

Complete ClickHouse Cloud API integration:

  • Organizations (5 tools): Manage settings, metrics, private endpoints
  • Services (12 tools): Create, scale, start/stop, configure, delete cloud services
  • API Keys (5 tools): Full CRUD operations for programmatic access
  • Members & Invitations (8 tools): User management and access control
  • Backups (4 tools): Configure and manage automated backups
  • ClickPipes (7 tools): Data ingestion pipeline management
  • Monitoring (3 tools): Usage analytics, costs, and audit logs
  • Network (6 tools): Private endpoints and security configuration

πŸ”’ Safety Features

This MCP server includes comprehensive safety controls to prevent accidental data modification or infrastructure changes:

πŸ“Š Database Safety

  • Automatic Read-Only Mode: All database queries run with readonly = 1 by default
  • Query Filtering: Only SELECT, SHOW, DESCRIBE, and EXPLAIN queries are allowed
  • Manual Override: Set CLICKHOUSE_READONLY=false to enable write operations when needed

☁️ Cloud Management Safety

  • Protected Operations: Destructive cloud operations (delete, stop) can be enabled
  • Safe Mode: Set CLICKHOUSE_CLOUD_READONLY=false to allow infrastructure changes
  • Audit Trail: All operations are logged for accountability

πŸ›‘οΈ Security Best Practices

  • Minimal Privileges: Create dedicated users with limited permissions
  • SSL by Default: Secure connections enabled automatically
  • Environment Variables: Sensitive credentials never hardcoded
  • Timeout Controls: Prevent runaway queries and operations

πŸ› οΈ Available Tools

πŸ“Š Database Tools (3 tools)

These tools work with any ClickHouse database when database configuration is provided:

  • list_databases() - List all available databases
  • list_tables(database, like?, not_like?) - List tables with detailed metadata including schema, row counts, and column information
  • run_query(query) - Execute queries with safety controls:
    • Read-only mode (CLICKHOUSE_READONLY=true): Only SELECT, SHOW, DESCRIBE, EXPLAIN queries
    • Full access mode (CLICKHOUSE_READONLY=false): All SQL operations including INSERT, UPDATE, DELETE, CREATE, DROP

[!NOTE] Query Safety: When CLICKHOUSE_READONLY=true, all queries automatically run with readonly = 1 setting and are filtered to prevent data modification operations.

☁️ Cloud Management Tools (50+ tools)

These tools work with ClickHouse Cloud when API credentials are provided. Tool availability depends on the CLICKHOUSE_CLOUD_READONLY setting:

πŸ” Read-Only Operations (Available when CLICKHOUSE_CLOUD_READONLY=true)

Organization Monitoring (3 tools)

  • cloud_list_organizations() - List available organizations
  • cloud_get_organization(organization_id) - Get organization details
  • cloud_get_organization_metrics(organization_id, filtered_metrics?) - Get Prometheus metrics

Service Monitoring (3 tools)

  • cloud_list_services(organization_id) - List all services in organization
  • cloud_get_service(organization_id, service_id) - Get detailed service information
  • cloud_get_service_metrics(organization_id, service_id, filtered_metrics?) - Get service performance metrics

Resource Inspection (8 tools)

  • cloud_list_api_keys(organization_id) - List all API keys (metadata only)
  • cloud_get_api_key(organization_id, key_id) - Get API key details
  • cloud_list_members(organization_id) - List organization members
  • cloud_get_member(organization_id, user_id) - Get member details
  • cloud_list_invitations(organization_id) - List pending invitations
  • cloud_get_invitation(organization_id, invitation_id) - Get invitation details
  • cloud_list_backups(organization_id, service_id) - List service backups
  • cloud_get_backup(organization_id, service_id, backup_id) - Get backup details

Configuration Inspection (5 tools)

  • cloud_get_backup_configuration(organization_id, service_id) - Get backup configuration
  • cloud_get_private_endpoint_config(organization_id, service_id) - Get private endpoint configuration
  • cloud_list_clickpipes(organization_id, service_id) - List ClickPipes
  • cloud_get_clickpipe(organization_id, service_id, clickpipe_id) - Get ClickPipe details
  • cloud_get_available_regions() - Get supported regions

Analytics & Monitoring (3 tools)

  • cloud_list_activities(organization_id, from_date?, to_date?) - Get audit logs
  • cloud_get_activity(organization_id, activity_id) - Get activity details
  • cloud_get_usage_cost(organization_id, from_date, to_date) - Get usage analytics

⚠️ Write Operations (Available only when CLICKHOUSE_CLOUD_READONLY=false)

Organization Management (2 tools)

  • cloud_update_organization(organization_id, name?, private_endpoints?) - Update organization settings
  • cloud_get_organization_private_endpoint_info(organization_id, cloud_provider, region) - Get private endpoint info

Service Management (9 tools)

  • cloud_create_service(organization_id, name, provider, region, ...) - Create new service
  • cloud_update_service(organization_id, service_id, ...) - Update service settings
  • cloud_update_service_state(organization_id, service_id, command) - Start/stop services
  • cloud_update_service_scaling(organization_id, service_id, ...) - Configure scaling (legacy)
  • cloud_update_service_replica_scaling(organization_id, service_id, ...) - Configure replica scaling
  • cloud_update_service_password(organization_id, service_id, ...) - Update service password
  • cloud_create_service_private_endpoint(organization_id, service_id, id, description) - Create private endpoint
  • cloud_delete_service(organization_id, service_id) - Delete service

API Key Management (3 tools)

  • cloud_create_api_key(organization_id, name, roles, ...) - Create new API key
  • cloud_update_api_key(organization_id, key_id, ...) - Update API key properties
  • cloud_delete_api_key(organization_id, key_id) - Delete API key

User Management (3 tools)

  • cloud_update_member_role(organization_id, user_id, role) - Update member role
  • cloud_remove_member(organization_id, user_id) - Remove member
  • cloud_create_invitation(organization_id, email, role) - Send invitation
  • cloud_delete_invitation(organization_id, invitation_id) - Cancel invitation

Infrastructure Management (12 tools)

  • cloud_update_backup_configuration(organization_id, service_id, ...) - Update backup settings
  • cloud_create_clickpipe(organization_id, service_id, name, description, source, destination, field_mappings?) - Create ClickPipe
  • cloud_update_clickpipe(organization_id, service_id, clickpipe_id, ...) - Update ClickPipe
  • cloud_update_clickpipe_scaling(organization_id, service_id, clickpipe_id, replicas?) - Scale ClickPipe
  • cloud_update_clickpipe_state(organization_id, service_id, clickpipe_id, command) - Control ClickPipe state
  • cloud_delete_clickpipe(organization_id, service_id, clickpipe_id) - Delete ClickPipe
  • cloud_list_reverse_private_endpoints(organization_id, service_id) - List reverse private endpoints
  • cloud_create_reverse_private_endpoint(organization_id, service_id, ...) - Create reverse private endpoint
  • cloud_get_reverse_private_endpoint(organization_id, service_id, reverse_private_endpoint_id) - Get details
  • cloud_delete_reverse_private_endpoint(organization_id, service_id, reverse_private_endpoint_id) - Delete endpoint
  • cloud_create_query_endpoint_config(organization_id, service_id, roles, open_api_keys, allowed_origins) - Create query config
  • cloud_delete_query_endpoint_config(organization_id, service_id) - Delete query config

[!CAUTION] Production Warning: Write operations can create billable resources, modify running services, or delete infrastructure. Always use CLICKHOUSE_CLOUD_READONLY=true in production unless infrastructure changes are specifically required.

πŸ”§ Development

Local Development Setup

  1. Start ClickHouse for testing:

    cd test-services
    docker compose up -d
  2. Create environment file:

    cat > .env << EOF
    # Database configuration (development mode)
    CLICKHOUSE_HOST=localhost
    CLICKHOUSE_PORT=8123
    CLICKHOUSE_USER=default
    CLICKHOUSE_PASSWORD=clickhouse
    CLICKHOUSE_SECURE=false
    CLICKHOUSE_READONLY=false
    
    # Cloud configuration (optional, safe mode)
    CLICKHOUSE_CLOUD_KEY_ID=your-key-id
    CLICKHOUSE_CLOUD_KEY_SECRET=your-key-secret
    CLICKHOUSE_CLOUD_READONLY=true
    EOF
  3. Install and run:

    uv sync                               # Install dependencies
    source .venv/bin/activate            # Activate virtual environment
    mcp dev chmcp/mcp_server.py          # Start for testing
    # OR
    python -m chmcp.main                 # Start normally

Testing Safety Features

# Test read-only database mode
CLICKHOUSE_READONLY=true python -m chmcp.main

# Test cloud monitoring mode  
CLICKHOUSE_CLOUD_READONLY=true python -m chmcp.main

# Test full access mode (development only)
CLICKHOUSE_READONLY=false CLICKHOUSE_CLOUD_READONLY=false python -m chmcp.main

Project Structure

chmcp/
β”œβ”€β”€ __init__.py                 # Package initialization
β”œβ”€β”€ main.py                     # Entry point
β”œβ”€β”€ mcp_env.py                  # Database environment configuration
β”œβ”€β”€ mcp_server.py              # Main server + database tools (3 tools)
β”œβ”€β”€ cloud_config.py            # Cloud API configuration
β”œβ”€β”€ cloud_client.py            # HTTP client for Cloud API
└── cloud_tools.py             # Cloud MCP tools (50+ tools)

Running Tests

uv sync --all-extras --dev              # Install dev dependencies
uv run ruff check .                     # Run linting
docker compose up -d                    # Start test ClickHouse
uv run pytest tests                     # Run tests

πŸ“„ License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

Developed by Badr Ouali