Skip to main content
Glama
adamzaidi

icloud-mcp

by adamzaidi

get_email

Retrieve complete email content from iCloud Mail using the email's unique identifier (UID). Specify mailbox, character limits, and header inclusion for precise email access.

Instructions

Get full content of a specific email by UID

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
uidYesEmail UID
mailboxNoMailbox name (default INBOX)
maxCharsNoMax body characters to return (default 8000, max 50000)
includeHeadersNoIf true, include a headers object with to/cc/replyTo/messageId/inReplyTo/references/listUnsubscribe

Implementation Reference

  • The function 'getEmailContent' acts as the 'get_email' tool, responsible for fetching and parsing the content of an email (including body, metadata, and attachments) from an IMAP mailbox.
    export async function getEmailContent(uid, mailbox = 'INBOX', maxChars = 8000, includeHeaders = false, creds = null) {
      const client = createRateLimitedClient(creds);
      await client.connect();
      await client.mailboxOpen(mailbox);
    
      const fetchOpts = { envelope: true, flags: true, bodyStructure: true };
      if (includeHeaders) fetchOpts.headers = new Set(['references', 'list-unsubscribe']);
      const meta = await client.fetchOne(uid, fetchOpts, { uid: true });
      if (!meta) {
        await client.logout();
        return { uid, subject: null, from: null, date: null, flags: [], body: '(email not found)' };
      }
    
      let body = '(body unavailable)';
    
      try {
        const struct = meta.bodyStructure;
        if (!struct) throw new Error('no bodyStructure');
    
        const textPart = findTextPart(struct);
    
        if (!textPart) {
          body = '(no readable text — email may be image-only or have no text parts)';
        } else {
          // Single-part messages use 'TEXT'; multipart use dot-notation part id (e.g. '1', '1.1')
          const imapKey = textPart.partId ?? 'TEXT';
    
          // For large parts, cap the fetch at 12KB to avoid downloading multi-MB newsletters
          const fetchSpec = (textPart.size && textPart.size > 150_000)
            ? [{ key: imapKey, start: 0, maxLength: 12_000 }]
            : [imapKey];
    
          const partMsg = await Promise.race([
            client.fetchOne(uid, { bodyParts: fetchSpec }, { uid: true }),
            new Promise((_, reject) => setTimeout(() => reject(new Error('body fetch timeout')), 10_000))
          ]);
    
          // bodyParts is a Map — try the key as-is, then uppercase, then lowercase
          const partBuffer = partMsg?.bodyParts?.get(imapKey)
            ?? partMsg?.bodyParts?.get(imapKey.toUpperCase())
            ?? partMsg?.bodyParts?.get(imapKey.toLowerCase());
    
          if (!partBuffer || partBuffer.length === 0) throw new Error('empty body part');
    
          const decoded = decodeTransferEncoding(partBuffer, textPart.encoding);
          let text = await decodeCharset(decoded, textPart.charset);
    
          if (textPart.type === 'text/html') text = stripHtml(text);
    
          const clampedMaxChars = Math.min(maxChars, 50_000);
          if (text.length > clampedMaxChars) {
            text = text.slice(0, clampedMaxChars) + `\n\n[... truncated — ${text.length.toLocaleString()} chars total]`;
          }
    
          body = text.trim() || '(empty body)';
    
          if (textPart.size && textPart.size > 150_000) {
            body += `\n\n[Note: email body is large (${Math.round(textPart.size / 1024)}KB) — showing first 12KB]`;
          }
        }
      } catch {
        // Fallback: raw source slice (original behaviour)
        try {
          const sourceMsg = await Promise.race([
            client.fetchOne(uid, { source: true }, { uid: true }),
            new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), 5_000))
          ]);
          if (sourceMsg?.source) {
            const raw = sourceMsg.source.toString();
            const bodyStart = raw.indexOf('\r\n\r\n');
            body = '[raw fallback]\n' + (bodyStart > -1 ? raw.slice(bodyStart + 4, bodyStart + 2000) : raw.slice(0, 2000));
          }
        } catch { /* leave as unavailable */ }
      }
    
      await client.logout();
    
      const attachments = meta.bodyStructure ? findAttachments(meta.bodyStructure) : [];
      const result = {
        uid: meta.uid,
        subject: meta.envelope.subject,
        from: meta.envelope.from?.[0]?.address,
        date: meta.envelope.date,
        flags: [...meta.flags],
        attachments: {
          count: attachments.length,
          items: attachments.map(a => ({ partId: a.partId, filename: a.filename, mimeType: a.mimeType, size: a.size }))
        },
        body
      };
    
      if (includeHeaders) {
        // imapflow returns headers as a raw Buffer — parse it as text
        const rawRefs = extractRawHeader(meta.headers, 'references');
        const rawUnsub = extractRawHeader(meta.headers, 'list-unsubscribe');
        result.headers = {
          to: meta.envelope.to?.map(a => a.address) ?? [],
          cc: meta.envelope.cc?.map(a => a.address) ?? [],
          replyTo: meta.envelope.replyTo?.[0]?.address ?? null,
          messageId: meta.envelope.messageId ?? null,
          inReplyTo: meta.envelope.inReplyTo ?? null,
          references: rawRefs ? rawRefs.split(/\s+/).filter(s => s.startsWith('<')) : [],
          listUnsubscribe: rawUnsub || null
        };
      }
    
      return result;
    }
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure but fails to state whether this marks the email as read, what happens if the UID doesn't exist, or the return format structure. 'Full content' hints at scope but safety profile and side effects are unspecified.

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?

Single sentence, nine words, front-loaded with action verb. No filler or redundant phrases. Given the high schema coverage, this efficient length is appropriate.

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?

Minimum viable for a 4-parameter retrieval tool. While the schema covers parameters well, the absence of output schema and annotations leaves gaps regarding return structure and operational safety that the description does not fill. Adequate but could specify error handling or return format.

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%, documenting all four parameters (uid, mailbox, maxChars, includeHeaders) adequately. The description mentions 'by UID' reinforcing the required parameter, but adds no semantic detail beyond the schema regarding character limits or header structure.

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 ('Get'), resource ('full content of a specific email'), and lookup method ('by UID'), distinguishing it from bulk retrieval siblings like get_emails_by_date_range. However, it does not explicitly differentiate from get_email_raw (likely returns MIME source) or clarify if this returns structured data vs. raw content.

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 provided on when to use this tool versus alternatives like get_email_raw, get_thread, or read_inbox. Does not mention prerequisites such as needing a valid UID from list_mailboxes or search_emails first.

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/adamzaidi/icloud-mcp'

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