gmail-get-email
Retrieve a specific Gmail email using its message ID to access individual messages within the Google Services MCP Server.
Instructions
Get a specific email by message ID
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| messageId | Yes | Gmail message ID |
Implementation Reference
- src/gmail.ts:293-365 (handler)The handler function that retrieves a specific Gmail email by message ID using the Google Gmail API, extracts headers and body (handling multipart), truncates long body, formats as Markdown using formatEmailToMarkdown, and returns structured content.export async function getEmail(params: z.infer<typeof getEmailSchema>) { try { const auth = createGmailAuth(); const gmail = google.gmail({ version: "v1", auth }); const response = await gmail.users.messages.get({ userId: "me", id: params.messageId, format: "full", }); const message = response.data; const headers = message.payload?.headers || []; const getHeader = (name: string) => headers.find((h) => h.name?.toLowerCase() === name.toLowerCase()) ?.value || ""; // Extract body content let body = ""; if (message.payload?.body?.data) { body = Buffer.from(message.payload.body.data, "base64").toString(); } else if (message.payload?.parts) { // Handle multipart messages for (const part of message.payload.parts) { if (part.mimeType === "text/plain" && part.body?.data) { body = Buffer.from(part.body.data, "base64").toString(); break; } } } // Truncate body if it exceeds 30000 characters const truncatedBody = body.length > 20000 ? body.substring(0, 20000) + "\n\n[Email body truncated - content too long]" : body; const emailDetail = { id: message.id, threadId: message.threadId, from: getHeader("From"), to: getHeader("To"), cc: getHeader("Cc"), bcc: getHeader("Bcc"), subject: getHeader("Subject"), date: getHeader("Date"), body: truncatedBody, snippet: message.snippet, labelIds: message.labelIds, }; return { content: [ { type: "text" as const, text: formatEmailToMarkdown(emailDetail), }, ], }; } catch (error) { return { content: [ { type: "text" as const, text: `Error getting email: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } }
- src/gmail.ts:32-34 (schema)Zod schema for input validation: requires 'messageId' as a string.export const getEmailSchema = z.object({ messageId: z.string().describe("Gmail message ID"), });
- src/index.ts:200-207 (registration)MCP server registration of the 'gmail-get-email' tool, using getEmailSchema and delegating to the getEmail handler.server.tool( "gmail-get-email", "Get a specific email by message ID", getEmailSchema.shape, async (params) => { return await getEmail(params); } );