Skip to main content
Glama
mundume
by mundume

getEmailContent

Retrieve full email content from Gmail by specifying the email index to access complete message details.

Instructions

Retrieve the full content of an email from Gmail.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
emailIndexYesThe index of the email to retrieve.

Implementation Reference

  • The getEmailContent tool handler that retrieves full email content. It validates the emailIndex parameter, fetches the list of messages to find the message at the specified index, retrieves the full message content from Gmail API, parses it, and returns the result.
    case "getEmailContent": { if (!GMAIL_API_KEY) { return { content: [{ type: "text", text: "API Key not set." }] }; } try { const { emailIndex } = EmailIndexSchema.parse(request.params.arguments); // 1. Get List of Message IDs (up to the requested index) const messageListResponse = await fetch( `https://gmail.googleapis.com/gmail/v1/users/${GMAIL_USER_ID}/messages?maxResults=${emailIndex}`, { headers: { Authorization: `Bearer ${GMAIL_API_KEY}` } } ); if (!messageListResponse.ok) { const errorData = await messageListResponse.json(); const errorMessage = errorData.error?.message || messageListResponse.statusText; return { content: [{ type: "text", text: `Error: ${errorMessage}` }], }; } const messageList = await messageListResponse.json(); if (!messageList.messages || messageList.messages.length < emailIndex) { return { content: [ { type: "text", text: "Email not found at the specified index." }, ], }; } const messageId = messageList.messages[emailIndex - 1].id; // Get the ID at the requested index // 2. Get Full Message Content const messageResponse = await fetch( `https://gmail.googleapis.com/gmail/v1/users/${GMAIL_USER_ID}/messages/${messageId}?format=full`, { headers: { Authorization: `Bearer ${GMAIL_API_KEY}` } } ); if (!messageResponse.ok) { const errorData = await messageResponse.json(); const errorMessage = errorData.error?.message || messageResponse.statusText; return { content: [{ type: "text", text: `Error: ${errorMessage}` }], }; } const fullMessage = await messageResponse.json(); const emailContent = await parseMessage(fullMessage); return { content: [{ type: "text", text: JSON.stringify(emailContent) }], }; } catch (error: any) { return { content: [{ type: "text", text: `Error: ${error.message}` }] }; } }
  • src/index.ts:65-69 (registration)
    Tool registration in the MCP server that defines the getEmailContent tool with its name, description, and input schema.
    { name: "getEmailContent", description: "Retrieve the full content of an email from Gmail.", inputSchema: zodToJsonSchema(EmailIndexSchema), },
  • Zod schema definition for the getEmailContent tool parameters, validating the emailIndex input.
    const EmailIndexSchema = z.object({ emailIndex: z.number().describe("The index of the email to retrieve."), });
  • Helper function parseMessage that extracts and decodes the email subject, sender, and body from the Gmail API message format, handling both multipart and simple messages.
    async function parseMessage(message: { payload: { headers: { name: string; value: string }[]; parts: { mimeType: string; body: { data: WithImplicitCoercion<string> } }[]; body: { data: WithImplicitCoercion<string> }; }; id: string; }) { const headers = message.payload.headers; const subject = headers.find( (header: { name: string }) => header.name === "Subject" )?.value; const from = headers.find( (header: { name: string }) => header.name === "From" )?.value; let body = ""; if (message.payload.parts) { const textPart = message.payload.parts.find( (part) => part.mimeType === "text/plain" ); if (textPart) { body = Buffer.from(textPart.body.data, "base64").toString("utf-8"); } else { const htmlPart = message.payload.parts.find( (part) => part.mimeType === "text/html" ); if (htmlPart) { body = Buffer.from(htmlPart.body.data, "base64").toString("utf-8"); } } } else if (message.payload.body.data) { body = Buffer.from(message.payload.body.data, "base64").toString("utf-8"); } return { id: message.id, subject: subject, from: from, body: body, }; }
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/mundume/gmail-mcp'

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