get_gmail_thread_content
Retrieve and format the full content of a Gmail conversation thread, including all messages, by specifying the thread ID and user email.
Instructions
Retrieves the complete content of a Gmail conversation thread, including all messages.
Args:
thread_id (str): The unique ID of the Gmail thread to retrieve.
user_google_email (str): The user's Google email address. Required.
Returns:
str: The complete thread content with all messages formatted for reading.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| service | Yes | ||
| thread_id | Yes | ||
| user_google_email | Yes |
Implementation Reference
- gmail/gmail_tools.py:994-1020 (handler)The main handler function for the 'get_gmail_thread_content' tool. It fetches the full thread using Gmail API threads().get() and formats the output using a helper function.@server.tool() @require_google_service("gmail", "gmail_read") @handle_http_errors("get_gmail_thread_content", is_read_only=True, service_type="gmail") async def get_gmail_thread_content( service, thread_id: str, user_google_email: str ) -> str: """ Retrieves the complete content of a Gmail conversation thread, including all messages. Args: thread_id (str): The unique ID of the Gmail thread to retrieve. user_google_email (str): The user's Google email address. Required. Returns: str: The complete thread content with all messages formatted for reading. """ logger.info( f"[get_gmail_thread_content] Invoked. Thread ID: '{thread_id}', Email: '{user_google_email}'" ) # Fetch the complete thread with all messages thread_response = await asyncio.to_thread( service.users().threads().get(userId="me", id=thread_id, format="full").execute ) return _format_thread_content(thread_response, thread_id)
- gmail/gmail_tools.py:919-992 (helper)Supporting helper function that formats the raw thread data into a human-readable string, extracting subjects, senders, dates, and message bodies (text/HTML) for each message in the thread.def _format_thread_content(thread_data: dict, thread_id: str) -> str: """ Helper function to format thread content from Gmail API response. Args: thread_data (dict): Thread data from Gmail API thread_id (str): Thread ID for display Returns: str: Formatted thread content """ messages = thread_data.get("messages", []) if not messages: return f"No messages found in thread '{thread_id}'." # Extract thread subject from the first message first_message = messages[0] first_headers = { h["name"]: h["value"] for h in first_message.get("payload", {}).get("headers", []) } thread_subject = first_headers.get("Subject", "(no subject)") # Build the thread content content_lines = [ f"Thread ID: {thread_id}", f"Subject: {thread_subject}", f"Messages: {len(messages)}", "", ] # Process each message in the thread for i, message in enumerate(messages, 1): # Extract headers headers = { h["name"]: h["value"] for h in message.get("payload", {}).get("headers", []) } sender = headers.get("From", "(unknown sender)") date = headers.get("Date", "(unknown date)") subject = headers.get("Subject", "(no subject)") # Extract both text and HTML bodies payload = message.get("payload", {}) bodies = _extract_message_bodies(payload) text_body = bodies.get("text", "") html_body = bodies.get("html", "") # Format body content with HTML fallback body_data = _format_body_content(text_body, html_body) # Add message to content content_lines.extend( [ f"=== Message {i} ===", f"From: {sender}", f"Date: {date}", ] ) # Only show subject if it's different from thread subject if subject != thread_subject: content_lines.append(f"Subject: {subject}") content_lines.extend( [ "", body_data, "", ] ) return "\n".join(content_lines)