trymemcp/app/config.py

66 lines
2.0 KiB
Python

"""Configuration management for the TryMe FastMCP HTTP server."""
from __future__ import annotations
import configparser
import os
from dataclasses import dataclass
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parent.parent
def get_version() -> str:
"""Read application version from version.ini."""
version_file = PROJECT_ROOT / "version.ini"
if version_file.exists():
parser = configparser.ConfigParser()
parser.read(version_file, encoding="utf-8")
if parser.has_option("version", "version"):
return parser.get("version", "version").strip()
return "0.1"
@dataclass(slots=True)
class Settings:
"""Runtime settings populated from environment variables."""
version: str
mcp_host: str
mcp_port: int
mcp_path: str
tryme_base_url: str
tryme_timeout_seconds: float
tryme_api_key: str | None
tryme_bearer_token: str | None
tryme_session_cookie: str | None
@classmethod
def from_env(cls) -> Settings:
"""Load settings from current process environment."""
raw_path = os.getenv("MCP_PATH", "/mcp").strip() or "/mcp"
if not raw_path.startswith("/"):
raw_path = f"/{raw_path}"
api_key = os.getenv("TRYME_API_KEY", "").strip() or None
bearer_token = os.getenv("TRYME_BEARER_TOKEN", "").strip() or None
session_cookie = os.getenv("TRYME_SESSION_COOKIE", "").strip() or None
return cls(
version=get_version(),
mcp_host=os.getenv("MCP_HOST", "0.0.0.0").strip() or "0.0.0.0",
mcp_port=int(os.getenv("MCP_PORT", "8000")),
mcp_path=raw_path,
tryme_base_url=(
os.getenv("TRYME_BASE_URL", "https://tryme.tpk.pw").strip().rstrip("/")
or "https://tryme.tpk.pw"
),
tryme_timeout_seconds=float(os.getenv("TRYME_TIMEOUT_SECONDS", "30")),
tryme_api_key=api_key,
tryme_bearer_token=bearer_token,
tryme_session_cookie=session_cookie,
)
settings = Settings.from_env()