Skip to main content
Glama

gmail-read-emails

Retrieve and list emails from Gmail accounts using search queries, label filters, and result limits for email management.

Instructions

Read/list emails from Gmail with optional search query

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryNoGmail search query (e.g., 'is:unread', 'from:example@gmail.com')
maxResultsNoMaximum number of emails to return
labelIdsNoArray of label IDs to filter by

Implementation Reference

  • Main handler function that fetches emails from Gmail API using search query or labels, retrieves metadata, formats to markdown, and returns structured content or error.
    export async function readEmails(params: z.infer<typeof readEmailsSchema>) {
      try {
        const auth = createGmailAuth();
        const gmail = google.gmail({ version: "v1", auth });
    
        const listParams: any = {
          userId: "me",
          maxResults: params.maxResults,
        };
    
        if (params.query) listParams.q = params.query;
        if (params.labelIds) listParams.labelIds = params.labelIds;
    
        const response = await gmail.users.messages.list(listParams);
    
        if (!response.data.messages || response.data.messages.length === 0) {
          return {
            content: [
              {
                type: "text" as const,
                text: "# No Emails Found\n\nNo emails found matching your search criteria.",
              },
            ],
          };
        }
    
        // Get detailed information for each message
        const emailDetails = await Promise.all(
          response.data.messages
            .slice(0, params.maxResults)
            .map(async (message) => {
              const detail = await gmail.users.messages.get({
                userId: "me",
                id: message.id!,
                format: "metadata",
                metadataHeaders: ["From", "To", "Subject", "Date"],
              });
    
              const headers = detail.data.payload?.headers || [];
              const getHeader = (name: string) =>
                headers.find((h) => h.name?.toLowerCase() === name.toLowerCase())
                  ?.value || "";
    
              return {
                id: message.id,
                threadId: message.threadId,
                from: getHeader("From"),
                to: getHeader("To"),
                subject: getHeader("Subject"),
                date: getHeader("Date"),
                snippet: detail.data.snippet,
                labelIds: detail.data.labelIds,
              };
            })
        );
    
        return {
          content: [
            {
              type: "text" as const,
              text: formatEmailListToMarkdown(emailDetails),
            },
          ],
        };
      } catch (error) {
        return {
          content: [
            {
              type: "text" as const,
              text: `Error reading emails: ${
                error instanceof Error ? error.message : String(error)
              }`,
            },
          ],
        };
      }
    }
  • Zod schema defining the input parameters for the gmail-read-emails tool: optional query, maxResults (default 10), optional labelIds.
    export const readEmailsSchema = z.object({
      query: z
        .string()
        .optional()
        .describe(
          "Gmail search query (e.g., 'is:unread', 'from:example@gmail.com')"
        ),
      maxResults: z
        .number()
        .min(1)
        .max(100)
        .default(10)
        .describe("Maximum number of emails to return"),
      labelIds: z
        .array(z.string())
        .optional()
        .describe("Array of label IDs to filter by"),
    });
  • src/index.ts:191-198 (registration)
    Registers the 'gmail-read-emails' tool on the MCP server using the readEmails handler and readEmailsSchema.
    server.tool(
      "gmail-read-emails",
      "Read/list emails from Gmail with optional search query",
      readEmailsSchema.shape,
      async (params) => {
        return await readEmails(params);
      }
    );
  • Helper function to create authenticated Gmail client using environment variables for OAuth2.
    function createGmailAuth() {
      const clientId = process.env.GOOGLE_CLIENT_ID;
      const clientSecret = process.env.GOOGLE_CLIENT_SECRET;
      const redirectUri =
        process.env.GOOGLE_REDIRECT_URI || "http://localhost:3000/oauth2callback";
    
      if (!clientId || !clientSecret) {
        throw new Error(
          "GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET are required. Run oauth-setup.js to configure."
        );
      }
    
      const oauth2Client = new google.auth.OAuth2(
        clientId,
        clientSecret,
        redirectUri
      );
    
      const accessToken = process.env.GOOGLE_ACCESS_TOKEN;
      const refreshToken = process.env.GOOGLE_REFRESH_TOKEN;
    
      if (!accessToken || !refreshToken) {
        throw new Error("OAuth2 tokens missing. Run oauth-setup.js to get tokens.");
      }
    
      oauth2Client.setCredentials({
        access_token: accessToken,
        refresh_token: refreshToken,
      });
    
      return oauth2Client;
    }
  • Helper function to format the list of retrieved emails into a markdown string for the tool response.
    function formatEmailListToMarkdown(emails: any[]): string {
      if (!emails.length) return "No emails found.";
      
      let markdown = `# Inbox (${emails.length} emails)\n\n`;
      
      emails.forEach((email, index) => {
        const date = new Date(email.date).toLocaleDateString();
        const from = email.from.replace(/[<>]/g, '');
        const subject = email.subject || '(No Subject)';
        const snippet = email.snippet || '';
        
        markdown += `## ${index + 1}. ${subject}\n`;
        markdown += `From: ${from}  \n`;
        markdown += `Date: ${date}  \n`;
        markdown += `ID: \`${email.id}\`\n\n`;
        
        if (snippet) {
          markdown += `${snippet}\n\n`;
        }
        
        markdown += `---\n\n`;
      });
      
      return markdown;
    }
Behavior2/5

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

With no annotations provided, the description carries full burden but only states the basic action. It doesn't disclose behavioral traits like authentication requirements, rate limits, pagination behavior, what fields are returned, error conditions, or whether this is a read-only operation (though implied by 'read/list').

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?

Extremely concise single sentence with zero waste. Every word earns its place: 'Read/list' establishes the action, 'emails from Gmail' specifies the resource, and 'with optional search query' highlights key functionality.

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

Completeness2/5

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

For a 3-parameter tool with no annotations and no output schema, the description is insufficient. It doesn't explain what information is returned, how results are structured, authentication requirements, or error handling. The agent would need to guess about the output format and behavioral characteristics.

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 thoroughly. The description adds minimal value beyond the schema by mentioning 'optional search query' which aligns with the 'query' parameter, but doesn't provide additional context about parameter interactions or usage patterns.

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 verb ('read/list') and resource ('emails from Gmail') with the optional search capability. It distinguishes from siblings like 'gmail-get-email' (singular) and 'gmail-send-email' (write operation), but doesn't explicitly contrast with 'gmail-get-labels' or other non-email tools.

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

Usage Guidelines2/5

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

No guidance on when to use this tool versus alternatives like 'gmail-get-email' (for a specific email) or 'gmail-get-labels' (for labels). The description mentions optional search query but doesn't provide context for choosing between search parameters or when to use this tool over others.

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/CaptainCrouton89/maps-mcp'

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