"""Configuration management for the MCP server."""
import os
from pathlib import Path
from typing import Optional
class Config:
"""Configuration loader and manager."""
def __init__(self, config_path: Optional[Path] = None):
"""
Initialize configuration.
Args:
config_path: Optional path to config file. Defaults to ~/.codex_mcp/config.toml
"""
if config_path is None:
self.config_dir = Path.home() / ".codex_mcp"
config_path = self.config_dir / "config.toml"
else:
self.config_dir = config_path.parent
self.config_path = config_path
self._load_config()
def _load_config(self):
"""Load configuration from environment variables or defaults."""
# Telegram Bot Configuration
self.telegram_bot_token = os.getenv("TELEGRAM_BOT_TOKEN", "")
self.telegram_chat_id = os.getenv("TELEGRAM_CHAT_ID", "")
# Security: Allowed user IDs (comma-separated)
allowed_ids = os.getenv("TELEGRAM_ALLOWED_USER_IDS", "")
self.telegram_allowed_user_ids = set()
if allowed_ids:
try:
self.telegram_allowed_user_ids = {
int(uid.strip()) for uid in allowed_ids.split(",") if uid.strip()
}
except ValueError as e:
print(f"Warning: Invalid TELEGRAM_ALLOWED_USER_IDS format: {e}")
# Enable Telegram only when all required values are present
self.telegram_enabled = bool(
self.telegram_bot_token and self.telegram_chat_id and self.telegram_allowed_user_ids
)
def validate(self) -> tuple[bool, Optional[str]]:
"""
Validate configuration.
Returns:
Tuple of (is_valid, error_message)
"""
if self.telegram_bot_token or self.telegram_chat_id or self.telegram_allowed_user_ids:
if not self.telegram_bot_token:
return False, "TELEGRAM_BOT_TOKEN is required when Telegram is enabled"
if not self.telegram_chat_id:
return False, "TELEGRAM_CHAT_ID is required when Telegram is enabled"
if not self.telegram_allowed_user_ids:
return False, "TELEGRAM_ALLOWED_USER_IDS is required when Telegram is enabled"
return True, None