Skip to main content
Glama
MadLlama25

Fastmail MCP Server

by MadLlama25

reply_email

Compose a reply to an existing email, automatically adding In-Reply-To and References headers. Fetches the original message for context. Send now or save as draft.

Instructions

Reply to an existing email with proper threading headers (In-Reply-To, References). Automatically fetches the original email to build the reply chain. By default sends immediately; set send=false to save as a draft instead.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
originalEmailIdYesID of the email to reply to
toNoRecipient email addresses (optional, defaults to the original sender)
ccNoCC email addresses (optional)
bccNoBCC email addresses (optional)
fromNoSender email address (optional, defaults to account primary email)
textBodyNoPlain text body (optional)
htmlBodyNoHTML body (optional)
sendNoWhether to send the reply immediately (default: true). Set to false to save as draft instead.
replyToNoReply-To email addresses (replies go here instead of to the sender)

Implementation Reference

  • src/index.ts:240-289 (registration)
    Tool registration for 'reply_email' in ListToolsRequestSchema (inputSchema with description and properties like originalEmailId, to, cc, bcc, from, textBody, htmlBody, send, replyTo)
    {
      name: 'reply_email',
      description: 'Reply to an existing email with proper threading headers (In-Reply-To, References). Automatically fetches the original email to build the reply chain. By default sends immediately; set send=false to save as a draft instead.',
      inputSchema: {
        type: 'object',
        properties: {
          originalEmailId: {
            type: 'string',
            description: 'ID of the email to reply to',
          },
          to: {
            type: 'array',
            items: { type: 'string' },
            description: 'Recipient email addresses (optional, defaults to the original sender)',
          },
          cc: {
            type: 'array',
            items: { type: 'string' },
            description: 'CC email addresses (optional)',
          },
          bcc: {
            type: 'array',
            items: { type: 'string' },
            description: 'BCC email addresses (optional)',
          },
          from: {
            type: 'string',
            description: 'Sender email address (optional, defaults to account primary email)',
          },
          textBody: {
            type: 'string',
            description: 'Plain text body (optional)',
          },
          htmlBody: {
            type: 'string',
            description: 'HTML body (optional)',
          },
          send: {
            type: ['boolean', 'string'],
            description: 'Whether to send the reply immediately (default: true). Set to false to save as draft instead.',
          },
          replyTo: {
            type: 'array',
            items: { type: 'string' },
            description: 'Reply-To email addresses (replies go here instead of to the sender)',
          },
        },
        required: ['originalEmailId'],
      },
    },
  • Handler for 'reply_email' in CallToolRequestSchema. Fetches original email to build threading headers (In-Reply-To, References), prepends 'Re:' to subject, defaults recipients to original sender, and either sends immediately or saves as draft based on the 'send' parameter.
    case 'reply_email': {
      const { originalEmailId, to, cc, bcc, from, textBody, htmlBody, send, replyTo } = args as any;
      const shouldSend = coerceBool(send) ?? true;
      if (!originalEmailId) {
        throw new McpError(ErrorCode.InvalidParams, 'originalEmailId is required');
      }
      if (shouldSend && !textBody && !htmlBody) {
        throw new McpError(ErrorCode.InvalidParams, 'Either textBody or htmlBody is required');
      }
    
      // Fetch the original email to get threading headers
      const originalEmail = await client.getEmailById(originalEmailId);
    
      // Build threading headers
      const originalMessageId = originalEmail.messageId?.[0];
      if (!originalMessageId) {
        throw new McpError(ErrorCode.InternalError, 'Original email does not have a Message-ID; cannot thread reply');
      }
    
      const inReplyToHeader = [originalMessageId];
      const referencesHeader = [
        ...(originalEmail.references || []),
        originalMessageId,
      ];
    
      // Build subject with Re: prefix
      let replySubject = originalEmail.subject || '';
      if (!/^Re:/i.test(replySubject)) {
        replySubject = `Re: ${replySubject}`;
      }
    
      // Default recipients to the original sender
      const toArray = coerceStringArray(to);
      const replyRecipients = (toArray && toArray.length > 0)
        ? toArray
        : (Array.isArray(originalEmail.from) ? originalEmail.from.map((addr: any) => addr.email).filter(Boolean) : []);
    
      if (replyRecipients.length === 0) {
        throw new McpError(ErrorCode.InvalidParams, 'Could not determine reply recipient. Please provide "to" explicitly.');
      }
    
      const replyParams = {
        to: replyRecipients,
        cc,
        bcc,
        from,
        subject: replySubject,
        textBody,
        htmlBody,
        inReplyTo: inReplyToHeader,
        references: referencesHeader,
        replyTo,
      };
    
      if (!shouldSend) {
        const emailId = await client.createDraft(replyParams);
        return {
          content: [
            {
              type: 'text',
              text: `Reply draft saved successfully (Email ID: ${emailId}). Subject: ${replySubject}`,
            },
          ],
        };
      }
    
      const submissionId = await client.sendEmail(replyParams);
    
      return {
        content: [
          {
            type: 'text',
            text: `Reply sent successfully. Submission ID: ${submissionId}`,
          },
        ],
      };
    }
  • Helper function coerceBool used in reply_email handler to coerce the 'send' parameter (boolean or string 'true'/'false') to a boolean value.
    export function coerceBool(value: unknown): boolean | undefined {
      if (typeof value === 'boolean') return value;
      if (value === 'true') return true;
      if (value === 'false') return false;
      return undefined;
    }
  • Helper function coerceStringArray used in reply_email handler to coerce the 'to' parameter (array or comma-separated string) to a string array for recipients.
    export function coerceStringArray(value: unknown): string[] | undefined {
      if (value === undefined || value === null) return undefined;
      if (Array.isArray(value)) return value.map(String);
      if (typeof value !== 'string') return undefined;
      const trimmed = value.trim();
      if (!trimmed) return [];
      if (trimmed.startsWith('[') && trimmed.endsWith(']')) {
        try {
          const parsed = JSON.parse(trimmed);
          if (Array.isArray(parsed)) return parsed.map(String);
        } catch { /* fall through to comma-split */ }
      }
      return trimmed.split(',').map(s => s.trim()).filter(Boolean);
    }
  • sendEmail method on JmapClient - called by reply_email handler when shouldSend is true. Creates the email via Email/set and submits via EmailSubmission/set JMAP methods.
    async sendEmail(email: {
      to: string[];
      cc?: string[];
      bcc?: string[];
      subject: string;
      textBody?: string;
      htmlBody?: string;
      from?: string;
      mailboxId?: string;
      inReplyTo?: string[];
      references?: string[];
      replyTo?: string[];
    }): Promise<string> {
      const session = await this.getSession();
    
      // Get all identities to validate from address
      const identities = await this.getIdentities();
      if (!identities || identities.length === 0) {
        throw new Error('No sending identities found');
      }
    
      // Determine which identity to use
      let selectedIdentity;
      if (email.from) {
        // Validate that the from address matches an available identity
        selectedIdentity = identities.find(id => matchesIdentity(id.email, email.from!));
        if (!selectedIdentity) {
          throw new Error('From address is not verified for sending. Choose one of your verified identities.');
        }
      } else {
        // Use default identity
        selectedIdentity = identities.find(id => id.mayDelete === false) || identities[0];
      }
    
      // Use the requested from address (not the identity email, which may be a wildcard like *@domain)
      const fromEmail = email.from || selectedIdentity.email;
    
      // Get the mailbox IDs we need
      const mailboxes = await this.getMailboxes();
      const draftsMailbox = this.findMailboxByRoleOrName(mailboxes, 'drafts', 'draft');
      const sentMailbox = this.findMailboxByRoleOrName(mailboxes, 'sent', 'sent');
    
      if (!draftsMailbox) {
        throw new Error('Could not find Drafts mailbox to save email');
      }
      if (!sentMailbox) {
        throw new Error('Could not find Sent mailbox to move email after sending');
      }
    
      // Use provided mailboxId or default to drafts for initial creation
      const initialMailboxId = email.mailboxId || draftsMailbox.id;
    
      // Ensure we have at least one body type
      if (!email.textBody && !email.htmlBody) {
        throw new Error('Either textBody or htmlBody must be provided');
      }
    
      const initialMailboxIds: Record<string, boolean> = {};
      initialMailboxIds[initialMailboxId] = true;
    
      const sentMailboxIds: Record<string, boolean> = {};
      sentMailboxIds[sentMailbox.id] = true;
    
      const emailObject = {
        mailboxIds: initialMailboxIds,
        keywords: { $draft: true },
        from: [{ name: selectedIdentity.name, email: fromEmail }],
        to: email.to.map(addr => ({ email: addr })),
        cc: email.cc?.map(addr => ({ email: addr })) || [],
        bcc: email.bcc?.map(addr => ({ email: addr })) || [],
        subject: email.subject,
        ...(email.inReplyTo && { inReplyTo: email.inReplyTo }),
        ...(email.references && { references: email.references }),
        ...(email.replyTo?.length && { replyTo: email.replyTo.map(addr => ({ email: addr })) }),
        textBody: email.textBody ? [{ partId: 'text', type: 'text/plain' }] : undefined,
        htmlBody: email.htmlBody ? [{ partId: 'html', type: 'text/html' }] : undefined,
        bodyValues: {
          ...(email.textBody && { text: { value: email.textBody } }),
          ...(email.htmlBody && { html: { value: email.htmlBody } })
        }
      };
    
      const request: JmapRequest = {
        using: ['urn:ietf:params:jmap:core', 'urn:ietf:params:jmap:mail', 'urn:ietf:params:jmap:submission'],
        methodCalls: [
          ['Email/set', {
            accountId: session.accountId,
            create: { draft: emailObject }
          }, 'createEmail'],
          ['EmailSubmission/set', {
            accountId: session.accountId,
            create: {
              submission: {
                emailId: '#draft',
                identityId: selectedIdentity.id,
                envelope: {
                  mailFrom: { email: fromEmail },
                  rcptTo: [
                    ...email.to.map(addr => ({ email: addr })),
                    ...(email.cc || []).map(addr => ({ email: addr })),
                    ...(email.bcc || []).map(addr => ({ email: addr })),
                  ]
                }
              }
            },
            onSuccessUpdateEmail: {
              '#submission': {
                mailboxIds: sentMailboxIds,
                keywords: { $seen: true }
              }
            }
          }, 'submitEmail']
        ]
      };
    
      const response = await this.makeRequest(request);
    
      const emailResult = this.getMethodResult(response, 0);
      if (emailResult.notCreated?.draft) {
        const err = emailResult.notCreated.draft;
        throw new Error(`Failed to create email: ${err.type}${err.description ? ' - ' + err.description : ''}`);
      }
    
      const emailId = emailResult.created?.draft?.id;
      if (!emailId) {
        throw new Error('Email creation returned no email ID');
      }
    
      const submissionResult = this.getMethodResult(response, 1);
      if (submissionResult.notCreated?.submission) {
        const err = submissionResult.notCreated.submission;
        throw new Error(`Failed to submit email: ${err.type}${err.description ? ' - ' + err.description : ''}`);
      }
    
      const submissionId = submissionResult.created?.submission?.id;
      if (!submissionId) {
        throw new Error('Email submission returned no submission ID');
      }
    
      return submissionId;
    }
Behavior3/5

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

Discloses key behaviors like threading and draft saving, but lacks details on error scenarios, permissions, or side effects, which is notable given no annotations.

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?

Three focused sentences, no fluff, front-loaded with the core action.

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

Completeness4/5

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

Covers essential behavior but omits output format and error handling; acceptable for a reply function with no output schema.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Adds meaning beyond schema by explaining default sending behavior and that 'to' defaults to original sender; schema coverage is 100% so baseline is met with added value.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

Clearly states it replies to an existing email with threading headers, distinguishing from sending new emails or creating standalone drafts.

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

Usage Guidelines4/5

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

Provides clear context on when to use (reply to existing email) and the option to save as draft via send parameter, but does not explicitly mention alternatives.

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/MadLlama25/fastmail-mcp'

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