From b50550b829c440532c257f45a46835254393ec0f Mon Sep 17 00:00:00 2001 From: latinogino <154024559+latinogino@users.noreply.github.com> Date: Thu, 10 Jul 2025 12:03:40 +0200 Subject: [PATCH] feat: Add configuration management for Dolibarr API --- src/dolibarr_mcp/config.py | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/dolibarr_mcp/config.py diff --git a/src/dolibarr_mcp/config.py b/src/dolibarr_mcp/config.py new file mode 100644 index 0000000..8bbd720 --- /dev/null +++ b/src/dolibarr_mcp/config.py @@ -0,0 +1,47 @@ +"""Configuration management for Dolibarr MCP Server.""" + +import os +from typing import Optional + +from pydantic import BaseModel, Field +from dotenv import load_dotenv + +# Load environment variables from .env file +load_dotenv() + + +class Config(BaseModel): + """Configuration for Dolibarr MCP Server.""" + + dolibarr_url: str = Field( + description="Dolibarr API URL", + default_factory=lambda: os.getenv("DOLIBARR_URL", "") + ) + + api_key: str = Field( + description="Dolibarr API key", + default_factory=lambda: os.getenv("DOLIBARR_API_KEY", "") + ) + + log_level: str = Field( + description="Logging level", + default_factory=lambda: os.getenv("LOG_LEVEL", "INFO") + ) + + def validate_config(self) -> None: + """Validate that required configuration is present.""" + if not self.dolibarr_url: + raise ValueError("DOLIBARR_URL environment variable is required") + + if not self.api_key: + raise ValueError("DOLIBARR_API_KEY environment variable is required") + + if not self.dolibarr_url.startswith(('http://', 'https://')): + raise ValueError("DOLIBARR_URL must start with http:// or https://") + + @classmethod + def from_env(cls) -> "Config": + """Create configuration from environment variables.""" + config = cls() + config.validate_config() + return config