File size: 1,642 Bytes
363cda9 1b2d294 363cda9 1b2d294 363cda9 1b2d294 363cda9 1b2d294 363cda9 1b2d294 363cda9 1b2d294 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
from pathlib import Path
from tempfile import NamedTemporaryFile
from pydantic import Field, PrivateAttr
from pydantic_settings import BaseSettings
BASE_PATH = Path(__file__).resolve().parents[4] # goes up to project root
class GMailSettings(BaseSettings):
"""Settings for Gmail MCP server."""
creds_json: str = Field(..., env="GMAIL_CREDS_JSON")
token_json: str = Field(..., env="GMAIL_TOKEN_JSON")
gmail_mcp_dir: Path = Field(default=BASE_PATH / "src/mcp_servers/gmail-mcp")
_creds_file: Path | None = PrivateAttr(default=None)
_token_file: Path | None = PrivateAttr(default=None)
model_config = {
"env_file": ".env",
"env_file_encoding": "utf-8",
}
@staticmethod
def _write_temp(prefix: str, content: str) -> Path:
with NamedTemporaryFile(delete=False, prefix=f"{prefix}_", suffix=".json") as tmp:
tmp.write(content.encode("utf-8"))
return Path(tmp.name)
def materialize_files(self) -> tuple[Path, Path]:
"""
Writes the credential and token JSON from env to temp files
and returns their paths for downstream tooling that expects paths.
"""
if self._creds_file is None:
self._creds_file = self._write_temp("gmail_creds", self.creds_json)
if self._token_file is None:
self._token_file = self._write_temp("gmail_token", self.token_json)
return self._creds_file, self._token_file
if __name__ == "__main__":
settings = GMailSettings()
creds_path, token_path = settings.materialize_files()
print(settings)
print(creds_path)
print(token_path) |