Skip to main content
Glama
Soundhannes

IMAP MCP Server

by Soundhannes

fetch_emails

Retrieve emails from an IMAP mailbox with configurable filters for date ranges, limits, and offsets to manage email access.

Instructions

Fetch emails from mailbox with optional filters

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
mailboxNoMailbox name (default: current)
limitNoMax emails to fetch (default: 20)
offsetNoSkip first N emails (default: 0)
sinceNoEmails since date (ISO format)
beforeNoEmails before date (ISO format)

Implementation Reference

  • The 'fetch_emails' method in the 'ImapClientWrapper' class handles fetching email headers from an IMAP mailbox using IMAP search criteria, sorting, and pagination. It also parses the IMAP response into EmailHeader objects.
    def fetch_emails(
        self,
        mailbox: Optional[str] = None,
        limit: int = 20,
        offset: int = 0,
        since: Optional[str] = None,
        before: Optional[str] = None,
    ) -> list[EmailHeader]:
        """Fetch emails from mailbox with optional filters."""
        self._ensure_connected()
        if mailbox:
            self.select_mailbox(mailbox)
        elif not self.current_mailbox:
            self.select_mailbox("INBOX")
    
        # Build search criteria
        criteria = ["ALL"]
        if since:
            criteria = ["SINCE", since]
        if before:
            if len(criteria) > 1:
                criteria.extend(["BEFORE", before])
            else:
                criteria = ["BEFORE", before]
    
        uids = self.client.search(criteria)
    
        # Apply offset and limit (newest first)
        uids = sorted(uids, reverse=True)
        if offset:
            uids = uids[offset:]
        if limit:
            uids = uids[:limit]
    
        if not uids:
            return []
    
        messages = self.client.fetch(uids, ["ENVELOPE", "FLAGS", "RFC822.SIZE"])
        return [self._parse_email_header(uid, data) for uid, data in messages.items()]
  • The core logic for fetching emails from the IMAP mailbox.
    def fetch_emails(
        self,
        mailbox: Optional[str] = None,
        limit: int = 20,
        offset: int = 0,
        since: Optional[str] = None,
        before: Optional[str] = None,
    ) -> list[EmailHeader]:
        """Fetch emails from mailbox with optional filters."""
        self._ensure_connected()
        if mailbox:
            self.select_mailbox(mailbox)
        elif not self.current_mailbox:
            self.select_mailbox("INBOX")
    
        # Build search criteria
        criteria = ["ALL"]
        if since:
            criteria = ["SINCE", since]
        if before:
            if len(criteria) > 1:
                criteria.extend(["BEFORE", before])
            else:
                criteria = ["BEFORE", before]
    
        uids = self.client.search(criteria)
    
        # Apply offset and limit (newest first)
        uids = sorted(uids, reverse=True)
        if offset:
            uids = uids[offset:]
        if limit:
            uids = uids[:limit]
    
        if not uids:
            return []
    
        messages = self.client.fetch(uids, ["ENVELOPE", "FLAGS", "RFC822.SIZE"])
        return [self._parse_email_header(uid, data) for uid, data in messages.items()]
  • The MCP tool handler that invokes fetch_emails when the 'fetch_emails' tool is called.
    elif name == "fetch_emails":
        return imap_client.fetch_emails(
            mailbox=args.get("mailbox"),
            limit=args.get("limit", 20),
            offset=args.get("offset", 0),
            since=args.get("since"),
            before=args.get("before"),
        )

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/Soundhannes/IMAP-MCP'

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