fix for gmail agent (#7)
Browse files- gmail fixes (f5f40ef65c8987af56fb1bba6ab4d56b3a2a14e9)
Co-authored-by: Dmitri Moscoglo <dimim@users.noreply.huggingface.co>
src/backend/agents/gmail/gmail_agent.py
CHANGED
|
@@ -39,11 +39,27 @@ def gmail_agent(query: str) -> str:
|
|
| 39 |
if not UV_PATH:
|
| 40 |
return "❌ Error: 'uv' executable not found. Please ensure uv is installed and in the system PATH."
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
import asyncio
|
|
|
|
|
|
|
| 44 |
async def _run_async():
|
| 45 |
# Load settings
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Initialize model
|
| 49 |
model = ChatOpenAI(model="gpt-4o", temperature=0)
|
|
@@ -56,8 +72,8 @@ def gmail_agent(query: str) -> str:
|
|
| 56 |
"args": [
|
| 57 |
"--directory", str(settings.gmail_mcp_dir),
|
| 58 |
"run", "gmail",
|
| 59 |
-
"--creds-file-path", str(
|
| 60 |
-
"--token-path", str(
|
| 61 |
],
|
| 62 |
"transport": "stdio",
|
| 63 |
}
|
|
|
|
| 39 |
if not UV_PATH:
|
| 40 |
return "❌ Error: 'uv' executable not found. Please ensure uv is installed and in the system PATH."
|
| 41 |
|
| 42 |
+
# Validate required Gmail settings before proceeding
|
| 43 |
+
import os
|
| 44 |
+
creds_env = os.getenv("GMAIL_CREDS_JSON")
|
| 45 |
+
token_env = os.getenv("GMAIL_TOKEN_JSON")
|
| 46 |
+
if not creds_env or not token_env:
|
| 47 |
+
return "❌ Gmail credentials not configured. Set GMAIL_CREDS_JSON and GMAIL_TOKEN_JSON environment variables."
|
| 48 |
+
|
| 49 |
try:
|
| 50 |
import asyncio
|
| 51 |
+
from pydantic_core import ValidationError
|
| 52 |
+
|
| 53 |
async def _run_async():
|
| 54 |
# Load settings
|
| 55 |
+
try:
|
| 56 |
+
# Pass env values directly to avoid reliance on env file loading
|
| 57 |
+
settings = GMailSettings(creds_json=creds_env, token_json=token_env)
|
| 58 |
+
except ValidationError as ve:
|
| 59 |
+
return f"❌ Gmail settings invalid: {ve}"
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return f"❌ Gmail settings error: {e}"
|
| 62 |
+
creds_path, token_path = settings.materialize_files()
|
| 63 |
|
| 64 |
# Initialize model
|
| 65 |
model = ChatOpenAI(model="gpt-4o", temperature=0)
|
|
|
|
| 72 |
"args": [
|
| 73 |
"--directory", str(settings.gmail_mcp_dir),
|
| 74 |
"run", "gmail",
|
| 75 |
+
"--creds-file-path", str(creds_path),
|
| 76 |
+
"--token-path", str(token_path),
|
| 77 |
],
|
| 78 |
"transport": "stdio",
|
| 79 |
}
|
src/mcp_servers/examples/gmail/settings.py
CHANGED
|
@@ -1,19 +1,48 @@
|
|
| 1 |
from pathlib import Path
|
| 2 |
-
from
|
|
|
|
| 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 |
-
|
| 10 |
-
|
|
|
|
| 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(
|
| 19 |
-
print(
|
|
|
|
| 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)
|