Skip to main content
Glama
AbhinavBansal17

MCP Headless Gmail Server

gmail_get_recent_emails

Retrieve recent Gmail emails with metadata and content snippets for headless environments using OAuth tokens, supporting unread filtering and result limits.

Instructions

Get the most recent emails from Gmail (returns metadata, snippets, and first 1k chars of body)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
google_access_tokenNoGoogle OAuth2 access token
max_resultsNoMaximum number of emails to return (default: 10)
unread_onlyNoWhether to return only unread emails (default: False)

Implementation Reference

  • Core handler implementing the Gmail API calls to fetch recent emails from INBOX, extract headers, snippets, and first 1000 characters of plain text body.
    def get_recent_emails(self, max_results: int = 10, unread_only: bool = False) -> str:
        """Get the most recent emails from Gmail
        
        Args:
            max_results: Maximum number of emails to return (default: 10)
            unread_only: Whether to return only unread emails (default: False)
            
        Returns:
            JSON string with an array of emails containing metadata, snippets, and first 1k chars of body
        """
        try:
            # Check if service is initialized
            if not hasattr(self, 'service'):
                logger.error("Gmail service not initialized. No valid access token provided.")
                return json.dumps({
                    "error": "No valid access token provided. Please refresh your token first.",
                    "status": "error"
                })
                
            # Define the operation
            def _operation():
                logger.debug(f"Fetching up to {max_results} recent emails from Gmail")
                
                # Get list of recent messages
                query = 'is:unread' if unread_only else ''
                logger.debug(f"Calling Gmail API to list messages from INBOX with query: '{query}'")
                
                try:
                    response = self.service.users().messages().list(
                        userId='me',
                        maxResults=max_results,
                        labelIds=['INBOX'],
                        q=query
                    ).execute()
                    
                    logger.debug(f"API Response received: {json.dumps(response)[:200]}...")
                except Exception as e:
                    logger.error(f"Error calling Gmail API list: {str(e)}", exc_info=True)
                    return json.dumps({"error": f"Gmail API list error: {str(e)}"})
                
                messages = response.get('messages', [])
                
                if not messages:
                    logger.debug("No messages found in the response")
                    return json.dumps({"emails": []})
                
                logger.debug(f"Found {len(messages)} messages, processing details")
                
                # Fetch detailed information for each message
                emails = []
                for i, message in enumerate(messages):
                    logger.debug(f"Fetching details for message {i+1}/{len(messages)}, ID: {message['id']}")
                    msg = self.service.users().messages().get(
                        userId='me',
                        id=message['id'],
                        format='full'
                    ).execute()
                    
                    logger.debug(f"Message {message['id']} details received, extracting fields")
                    
                    # Extract headers
                    headers = {}
                    if 'payload' in msg and 'headers' in msg['payload']:
                        for header in msg['payload']['headers']:
                            name = header.get('name', '').lower()
                            if name in ['from', 'to', 'subject', 'date']:
                                headers[name] = header.get('value', '')
                    else:
                        logger.debug(f"Message {message['id']} missing payload or headers fields: {json.dumps(msg)[:200]}...")
                    
                    # Extract plain text body and size
                    body_text = ""
                    body_size_bytes = 0
                    contains_full_body = True
                    
                    if 'payload' in msg:
                        body_text, body_size_bytes = self.extract_plain_text_body(msg['payload'])
                        
                        # Check if we're returning the full body or truncating
                        if len(body_text) > 1000:
                            body_text = body_text[:1000]
                            contains_full_body = False
                    
                    # Format the email
                    email_data = {
                        "id": msg['id'],
                        "threadId": msg['threadId'],
                        "labelIds": msg.get('labelIds', []),
                        "snippet": msg.get('snippet', ''),
                        "from": headers.get('from', ''),
                        "to": headers.get('to', ''),
                        "subject": headers.get('subject', ''),
                        "date": headers.get('date', ''),
                        "internalDate": msg.get('internalDate', ''),
                        "body": body_text,
                        "body_size_bytes": body_size_bytes,
                        "contains_full_body": contains_full_body
                    }
                    
                    logger.debug(f"Successfully processed message {message['id']}")
                    emails.append(email_data)
                
                logger.debug(f"Successfully processed {len(emails)} emails")
                return json.dumps({"emails": convert_datetime_fields(emails)})
            
            # Execute the operation with token refresh handling
            return self._handle_token_refresh(_operation)
            
        except HttpError as e:
            logger.error(f"Gmail API Exception: {str(e)}")
            return json.dumps({"error": str(e)})
        except Exception as e:
            logger.error(f"Exception in get_recent_emails: {str(e)}", exc_info=True)
            return json.dumps({"error": str(e)})
  • Tool registration in MCP server's list_tools() method, including input schema definition.
        name="gmail_get_recent_emails",
        description="Get the most recent emails from Gmail (returns metadata, snippets, and first 1k chars of body)",
        inputSchema={
            "type": "object",
            "properties": {
                "google_access_token": {"type": "string", "description": "Google OAuth2 access token"},
                "max_results": {"type": "integer", "description": "Maximum number of emails to return (default: 10)"},
                "unread_only": {"type": "boolean", "description": "Whether to return only unread emails (default: False)"}
            },
            "required": []
        },
    ),
  • MCP server tool dispatch handler that instantiates GmailClient and calls the get_recent_emails method with parsed arguments.
    if name == "gmail_get_recent_emails":
        # Initialize Gmail client with just access token
        logger.debug(f"Initializing Gmail client for get_recent_emails with access token: {access_token[:10]}...")
        try:
            gmail = GmailClient(
                access_token=access_token
            )
            logger.debug("Gmail client initialized successfully")
            
            max_results = int(arguments.get("max_results", 10))
            unread_only = bool(arguments.get("unread_only", False))
            logger.debug(f"Calling get_recent_emails with max_results={max_results} and unread_only={unread_only}")
            results = gmail.get_recent_emails(max_results=max_results, unread_only=unread_only)
            logger.debug(f"get_recent_emails result (first 200 chars): {results[:200]}...")
            return [types.TextContent(type="text", text=results)]
        except Exception as e:
            logger.error(f"Exception in gmail_get_recent_emails handler: {str(e)}", exc_info=True)
            return [types.TextContent(type="text", text=f"Error: {str(e)}")]
  • Helper utility to recursively extract plain text body from Gmail message payload parts, decoding base64url data.
    def extract_plain_text_body(self, msg_payload):
        """Extract plain text body from message payload
        
        Args:
            msg_payload: Gmail API message payload
            
        Returns:
            tuple: (plain_text_body, body_size_in_bytes)
        """
        body_text = ""
        body_size = 0
        
        # Helper function to process message parts recursively
        def extract_from_parts(parts):
            nonlocal body_text, body_size
            
            if not parts:
                return
                
            for part in parts:
                mime_type = part.get('mimeType', '')
                
                # If this part is plain text
                if mime_type == 'text/plain':
                    body_data = part.get('body', {}).get('data', '')
                    if body_data:
                        # Decode base64url encoded data
                        decoded_bytes = base64.urlsafe_b64decode(body_data)
                        body_size += len(decoded_bytes)
                        body_part = decoded_bytes.decode('utf-8', errors='replace')
                        body_text += body_part
                
                # If this part has child parts, process them
                if 'parts' in part:
                    extract_from_parts(part['parts'])
        
        # If body data is directly in the payload
        if 'body' in msg_payload and 'data' in msg_payload['body']:
            body_data = msg_payload['body']['data']
            if body_data:
                decoded_bytes = base64.urlsafe_b64decode(body_data)
                body_size += len(decoded_bytes)
                body_text = decoded_bytes.decode('utf-8', errors='replace')
        
        # If message has parts, process them
        if 'parts' in msg_payload:
            extract_from_parts(msg_payload['parts'])
            
        return body_text, body_size
  • JSON schema definition for the tool's input parameters.
    inputSchema={
        "type": "object",
        "properties": {
            "google_access_token": {"type": "string", "description": "Google OAuth2 access token"},
            "max_results": {"type": "integer", "description": "Maximum number of emails to return (default: 10)"},
            "unread_only": {"type": "boolean", "description": "Whether to return only unread emails (default: False)"}
        },
        "required": []
    },
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden. It discloses the return format (metadata, snippets, and first 1k chars of body) and the scope ('most recent'), which are useful behavioral traits. However, it doesn't mention rate limits, pagination, error handling, or whether this is a read-only operation (though implied by 'Get'). The description doesn't contradict any annotations since none are provided.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core purpose and includes key details about the return data. Every part earns its place, with no redundant or vague language. It's appropriately sized for a tool with good schema coverage and no complex behavioral nuances to explain.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (3 parameters, no output schema, no annotations), the description is adequate but has gaps. It covers the purpose and return format, but lacks details on authentication requirements (beyond the parameter), error cases, or how 'most recent' is determined (e.g., sorting by date). With no output schema, it should ideally describe the response structure more fully, but the mention of metadata/snippets/body chars provides some context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all three parameters (google_access_token, max_results, unread_only) with their types and defaults. The description doesn't add any parameter-specific semantics beyond what's in the schema, such as explaining OAuth2 scopes or how 'most recent' interacts with max_results/unread_only. Baseline 3 is appropriate when schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Get the most recent emails') and resource ('from Gmail'), specifying what data is returned (metadata, snippets, and first 1k chars of body). It distinguishes from sibling tools like gmail_get_email_body_chunk (which gets specific body chunks) and gmail_send_email (which sends emails). However, it doesn't explicitly mention the sibling differentiation in the description text itself.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage for retrieving recent emails with metadata and partial body content, but doesn't provide explicit guidance on when to use this tool versus alternatives. No when-not-to-use scenarios or prerequisites (like authentication needs) are mentioned, though the need for a google_access_token is clear from the schema.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/AbhinavBansal17/mcp-headless-gmail'

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