Skip to content

Commit 695f060

Browse files
committed
fix (gmail): fixing gmail mcp tools
1 parent 519f3d8 commit 695f060

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

src/server/main/chat/prompts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
STAGE_1_SYSTEM_PROMPT = """
22
You are an expert Triage AI. You have two primary responsibilities:
33
1. Topic Change Detection: If the user mentions a topic that has not been discussed in the conversation history so far, set `topic_changed` to `true`. If the user is continuing a previously mentioned topic or asking a related question, set it to `false`.
4-
2. Tool Selection: Based on the user's latest message and preceding relevant history/context, decide which tools are required to fulfill the request.
4+
2. Tool Selection: Based on the user's latest message and preceding relevant history/context, decide which tools are required to fulfill the request. If the topic hasn't changed, keep the previous tools in your `tools` list.
55
66
CRITICAL INSTRUCTIONS:
77
- `topic_changed` (boolean): Set to `true` if the latest user message mentions a topic that has never been mentioned in the conversation history.

src/server/mcp_hub/gmail/main.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,20 @@ async def _execute_tool(ctx: Context, func, *args, **kwargs) -> Dict[str, Any]:
7373
# --- Sync Tool Implementations ---
7474

7575
def _send_email_sync(service, to: str, subject: str, body: str):
76-
message_raw = base64.urlsafe_b64encode(MIMEText(body).as_bytes()).decode()
77-
message_body = {"raw": message_raw, "to": to, "subject": subject}
76+
msg = MIMEText(body)
77+
msg["to"] = to
78+
msg["subject"] = subject
79+
80+
# Optional: add From header to avoid confusion
81+
msg["from"] = "me"
82+
83+
raw = base64.urlsafe_b64encode(msg.as_bytes()).decode()
84+
message_body = {"raw": raw}
85+
7886
service.users().messages().send(userId="me", body=message_body).execute()
7987
return {"message": "Email sent successfully."}
8088

89+
8190
def _reply_to_email_sync(service, message_id: str, body: str, reply_all: bool = False):
8291
original_msg = service.users().messages().get(userId="me", id=message_id, format="metadata", metadataHeaders=["subject", "from", "to", "cc", "message-id", "references"]).execute()
8392
headers = {h['name'].lower(): h['value'] for h in original_msg['payload']['headers']}
@@ -262,8 +271,11 @@ async def applyLabels(ctx: Context, message_id: str, label_ids: List[str]) -> Di
262271
async def createDraft(ctx: Context, to: str, subject: str, body: str) -> Dict[str, Any]:
263272
"""Create a new draft email that can be edited before sending."""
264273
def _sync(service, to, subject, body):
265-
message_raw = base64.urlsafe_b64encode(MIMEText(body).as_bytes()).decode()
266-
message = {"message": {"raw": message_raw, "to": to, "subject": subject}}
274+
msg = MIMEText(body)
275+
msg["to"] = to
276+
msg["subject"] = subject
277+
message_raw = base64.urlsafe_b64encode(msg.as_bytes()).decode()
278+
message = {"message": {"raw": message_raw}}
267279
draft = service.users().drafts().create(userId="me", body=message).execute()
268280
return {"draft_id": draft['id'], "message": "Draft created successfully."}
269281
return await _execute_tool(ctx, _sync, to=to, subject=subject, body=body)
@@ -371,8 +383,13 @@ async def removeLabels(ctx: Context, message_id: str, label_ids: List[str]) -> D
371383
async def updateDraft(ctx: Context, draft_id: str, to: Optional[str] = None, subject: Optional[str] = None, body: Optional[str] = None) -> Dict[str, Any]:
372384
"""Update an existing draft email with new content."""
373385
def _sync(service, draft_id, to, subject, body):
374-
message_raw = base64.urlsafe_b64encode(MIMEText(body).as_bytes()).decode()
375-
message = {"message": {"raw": message_raw, "to": to, "subject": subject}}
386+
msg = MIMEText(body)
387+
if to:
388+
msg["to"] = to
389+
if subject:
390+
msg["subject"] = subject
391+
message_raw = base64.urlsafe_b64encode(msg.as_bytes()).decode()
392+
message = {"message": {"raw": message_raw}}
376393
updated_draft = service.users().drafts().update(userId="me", id=draft_id, body=message).execute()
377394
return {"draft_id": updated_draft['id'], "message": "Draft updated."}
378395
return await _execute_tool(ctx, _sync, draft_id=draft_id, to=to, subject=subject, body=body)

0 commit comments

Comments
 (0)