105 lines
2.8 KiB
Python
105 lines
2.8 KiB
Python
"""Configuration management for the MCP server."""
|
|
|
|
import argparse
|
|
import os
|
|
import sys
|
|
|
|
from pydantic_settings import (
|
|
BaseSettings,
|
|
PydanticBaseSettingsSource,
|
|
YamlConfigSettingsSource,
|
|
)
|
|
|
|
|
|
def _parse_args() -> argparse.Namespace:
|
|
"""Parse command-line arguments.
|
|
|
|
Returns a namespace with default values if running under pytest.
|
|
"""
|
|
# Don't parse args if running under pytest
|
|
if "pytest" in sys.modules:
|
|
parser = argparse.ArgumentParser()
|
|
return argparse.Namespace(
|
|
transport="stdio",
|
|
host="0.0.0.0", # noqa: S104
|
|
port=8080,
|
|
config=os.environ.get("CONFIG_FILE", "config.yaml"),
|
|
)
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument(
|
|
"--transport",
|
|
choices=["stdio", "sse", "streamable-http"],
|
|
default="stdio",
|
|
)
|
|
parser.add_argument("--host", default="0.0.0.0") # noqa: S104
|
|
parser.add_argument("--port", type=int, default=8080)
|
|
parser.add_argument(
|
|
"--config",
|
|
default=os.environ.get("CONFIG_FILE", "config.yaml"),
|
|
)
|
|
return parser.parse_args()
|
|
|
|
|
|
_args = _parse_args()
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
"""Server configuration populated from env vars and a YAML config file."""
|
|
|
|
model_config = {"env_file": ".env", "yaml_file": _args.config}
|
|
|
|
project_id: str
|
|
location: str
|
|
bucket: str
|
|
index_name: str
|
|
deployed_index_id: str
|
|
endpoint_name: str
|
|
endpoint_domain: str
|
|
embedding_model: str = "gemini-embedding-001"
|
|
search_limit: int = 10
|
|
log_name: str = "va_agent_evaluation_logs"
|
|
log_level: str = "INFO"
|
|
cloud_logging_enabled: bool = False
|
|
|
|
@classmethod
|
|
def settings_customise_sources(
|
|
cls,
|
|
settings_cls: type[BaseSettings],
|
|
init_settings: PydanticBaseSettingsSource,
|
|
env_settings: PydanticBaseSettingsSource,
|
|
dotenv_settings: PydanticBaseSettingsSource,
|
|
file_secret_settings: PydanticBaseSettingsSource,
|
|
) -> tuple[PydanticBaseSettingsSource, ...]:
|
|
"""Customize the order of settings sources to include YAML config."""
|
|
return (
|
|
init_settings,
|
|
env_settings,
|
|
dotenv_settings,
|
|
YamlConfigSettingsSource(settings_cls),
|
|
file_secret_settings,
|
|
)
|
|
|
|
|
|
# Lazy singleton instance of Settings
|
|
_cfg: Settings | None = None
|
|
|
|
|
|
def get_config() -> Settings:
|
|
"""Get or create the singleton Settings instance."""
|
|
global _cfg # noqa: PLW0603
|
|
if _cfg is None:
|
|
_cfg = Settings.model_validate({})
|
|
return _cfg
|
|
|
|
|
|
# For backwards compatibility, provide cfg as a property-like accessor
|
|
class _ConfigProxy:
|
|
"""Proxy object that lazily loads config on attribute access."""
|
|
|
|
def __getattr__(self, name: str) -> object:
|
|
return getattr(get_config(), name)
|
|
|
|
|
|
cfg = _ConfigProxy()
|