Skip to main content
Glama

google_gmail_get_email_by_index

Retrieve a specific email by its index from recent Gmail search results, with options to fetch it in full, metadata, minimal, or raw format.

Instructions

Get email by its index from the most recent search results

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
formatNoFormat to return the email in (full, metadata, minimal, raw)
indexYesIndex of the email from search results (starting from 1)

Implementation Reference

  • The handler function that validates input arguments, retrieves the message ID by index from recent emails, fetches the email content, and returns formatted text response or error.
    export async function handleGmailGetEmailByIndex( args: any, googleGmailInstance: GoogleGmail ) { if (!isGetEmailByIndexArgs(args)) { throw new Error("Invalid arguments for google_gmail_get_email_by_index"); } const { index, format } = args; try { const messageId = googleGmailInstance.getMessageIdByIndex(index); const result = await googleGmailInstance.getEmail(messageId, format); return { content: [{ type: "text", text: result }], isError: false, }; } catch (error) { return { content: [ { type: "text", text: `Error: ${ error instanceof Error ? error.message : String(error) }`, }, ], isError: true, }; } }
  • Tool schema definition including name, description, and input schema with required 'index' and optional 'format' parameters.
    export const GET_EMAIL_BY_INDEX_TOOL: Tool = { name: "google_gmail_get_email_by_index", description: "Get email by its index from the most recent search results", inputSchema: { type: "object", properties: { index: { type: "number", description: "Index of the email from search results (starting from 1)", }, format: { type: "string", description: "Format to return the email in (full, metadata, minimal, raw)", }, }, required: ["index"], }, };
  • Dispatch registration in the main server request handler switch statement that routes the tool call to the specific Gmail handler function.
    case "google_gmail_get_email_by_index": return await gmailHandlers.handleGmailGetEmailByIndex( args, googleGmailInstance );
  • Type guard function for input validation matching the tool's input schema.
    export function isGetEmailByIndexArgs(args: any): args is { index: number; format?: string; } { return ( args && typeof args.index === "number" && (args.format === undefined || typeof args.format === "string") ); }
  • Helper method in GoogleGmail class that retrieves the message ID from the cached recentEmails list based on the provided index.
    getMessageIdByIndex(index: number): string { if (index < 1 || index > this.recentEmails.length) { throw new Error( `Invalid email index: ${index}. Available range: 1-${this.recentEmails.length}` ); } return this.recentEmails[index - 1].id; }

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/vakharwalad23/google-mcp'

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