owenkaplinsky commited on
Commit
03e88bc
·
verified ·
1 Parent(s): 27edc31

Update src/mcp_servers/examples/gmail/settings.py

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