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"),
        )
Behavior2/5

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

With no annotations provided, the description carries full burden for behavioral disclosure. It states what the tool does but lacks critical details: whether this is a read-only operation, if it requires authentication, potential rate limits, what happens if filters yield no results, or the format of returned data. For a tool with 5 parameters and no output schema, this is a significant gap.

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?

The description is a single, efficient sentence that immediately conveys the core functionality. Every word earns its place with no redundancy or unnecessary elaboration.

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 tool with 5 parameters, no annotations, and no output schema, the description is inadequate. It doesn't explain what 'fetch' means operationally, how results are returned, error conditions, or relationships with sibling tools. The agent would struggle to use this effectively without trial and error.

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 parameters are well-documented in the schema itself. The description adds minimal value beyond stating 'optional filters' - it doesn't explain filter relationships, precedence, or provide examples. Baseline 3 is appropriate when the schema does the heavy lifting.

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 action ('fetch emails') and resource ('from mailbox'), making the purpose immediately understandable. It distinguishes from many siblings that perform different operations (e.g., search_emails, get_email, list_mailboxes). However, it doesn't explicitly differentiate from search_emails which might also retrieve emails with filters.

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?

The description mentions 'optional filters' but provides no guidance on when to use this tool versus alternatives like search_emails, get_email, or list_mailboxes. There's no mention of prerequisites, context, or exclusions, leaving the agent to guess based on tool names alone.

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

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