Skip to main content
Glama
taylorwilsdon

Google Workspace MCP Server - Control Gmail, Calendar, Docs, Sheets, Slides, Chat, Forms & Drive

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
NameRequiredDescriptionDefault
serviceYes
thread_idYes
user_google_emailYes

Implementation Reference

  • 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)
  • 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)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/taylorwilsdon/google_workspace_mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server