Skip to main content
Glama
cgrdavies

mcp-clickhouse-long-running

by cgrdavies
mcp_env.py4.88 kB
"""Environment configuration for the MCP ClickHouse server. This module handles all environment variable configuration with sensible defaults and type conversion. """ from dataclasses import dataclass import os from typing import Optional @dataclass class ClickHouseConfig: """Configuration for ClickHouse connection settings. This class handles all environment variable configuration with sensible defaults and type conversion. It provides typed methods for accessing each configuration value. Required environment variables: CLICKHOUSE_HOST: The hostname of the ClickHouse server CLICKHOUSE_USER: The username for authentication CLICKHOUSE_PASSWORD: The password for authentication Optional environment variables (with defaults): CLICKHOUSE_PORT: The port number (default: 8443 if secure=True, 8123 if secure=False) CLICKHOUSE_SECURE: Enable HTTPS (default: true) CLICKHOUSE_VERIFY: Verify SSL certificates (default: true) CLICKHOUSE_CONNECT_TIMEOUT: Connection timeout in seconds (default: 1800) CLICKHOUSE_SEND_RECEIVE_TIMEOUT: Send/receive timeout in seconds (default: 1800) CLICKHOUSE_DATABASE: Default database to use (default: None) """ def __init__(self): """Initialize the configuration from environment variables.""" self._validate_required_vars() @property def host(self) -> str: """Get the ClickHouse host.""" return os.environ["CLICKHOUSE_HOST"] @property def port(self) -> int: """Get the ClickHouse port. Defaults to 8443 if secure=True, 8123 if secure=False. Can be overridden by CLICKHOUSE_PORT environment variable. """ if "CLICKHOUSE_PORT" in os.environ: return int(os.environ["CLICKHOUSE_PORT"]) return 8443 if self.secure else 8123 @property def username(self) -> str: """Get the ClickHouse username.""" return os.environ["CLICKHOUSE_USER"] @property def password(self) -> str: """Get the ClickHouse password.""" return os.environ["CLICKHOUSE_PASSWORD"] @property def database(self) -> Optional[str]: """Get the default database name if set.""" return os.getenv("CLICKHOUSE_DATABASE") @property def secure(self) -> bool: """Get whether HTTPS is enabled. Default: True """ return os.getenv("CLICKHOUSE_SECURE", "true").lower() == "true" @property def verify(self) -> bool: """Get whether SSL certificate verification is enabled. Default: True """ return os.getenv("CLICKHOUSE_VERIFY", "true").lower() == "true" @property def connect_timeout(self) -> int: """Get the connection timeout in seconds. Default: 1800 """ return int(os.getenv("CLICKHOUSE_CONNECT_TIMEOUT", "1800")) @property def send_receive_timeout(self) -> int: """Get the send/receive timeout in seconds. Default: 1800 (30 minutes) """ return int(os.getenv("CLICKHOUSE_SEND_RECEIVE_TIMEOUT", "1800")) def get_client_config(self) -> dict: """Get the configuration dictionary for clickhouse_connect client. Returns: dict: Configuration ready to be passed to clickhouse_connect.get_client() """ config = { "host": self.host, "port": self.port, "username": self.username, "password": self.password, "secure": self.secure, "verify": self.verify, "connect_timeout": self.connect_timeout, "send_receive_timeout": self.send_receive_timeout, "client_name": "mcp_clickhouse", } # Add optional database if set if self.database: config["database"] = self.database return config def _validate_required_vars(self) -> None: """Validate that all required environment variables are set. Raises: ValueError: If any required environment variable is missing. """ missing_vars = [] for var in ["CLICKHOUSE_HOST", "CLICKHOUSE_USER", "CLICKHOUSE_PASSWORD"]: if var not in os.environ: missing_vars.append(var) if missing_vars: raise ValueError(f"Missing required environment variables: {', '.join(missing_vars)}") # Global instance placeholder for the singleton pattern _CONFIG_INSTANCE = None def get_config(): """ Gets the singleton instance of ClickHouseConfig. Instantiates it on the first call. """ global _CONFIG_INSTANCE if _CONFIG_INSTANCE is None: # Instantiate the config object here, ensuring load_dotenv() has likely run _CONFIG_INSTANCE = ClickHouseConfig() return _CONFIG_INSTANCE

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/cgrdavies/mcp-clickhouse'

If you have feedback or need assistance with the MCP directory API, please join our Discord server