Skip to main content
Glama
Soundhannes

IMAP MCP Server

by Soundhannes

get_cached_overview

Retrieve cached email summaries from IMAP mailboxes to quickly review inbox, next actions, waiting, and someday messages without fetching full content.

Instructions

Get cached email overview for INBOX, next, waiting, someday (from in-memory cache)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
mailboxNoSpecific mailbox to get (inbox, next, waiting, someday) or omit for all
limitNoMax emails per mailbox (default: 20)

Implementation Reference

  • The main logic for retrieving a cached overview of emails from IMAP folders.
    def get_cached_overview(
        self, mailbox: Optional[str] = None, limit: int = 20
    ) -> dict:
        """Get cached email overview for INBOX, next, waiting, someday (from in-memory cache)."""
        import time
    
        # If watcher is running, use its cache
        if self.watcher and self.watcher.running:
            # Wait briefly for cache to populate if empty
            for _ in range(10):
                cache = self.watcher.get_cache(mailbox)
                if cache:
                    return cache
                time.sleep(0.5)
            return self.watcher.get_cache(mailbox)
    
        # Fallback to manual fetch if watcher not running
        folders = self.config.get("folders", {})
        mailboxes = {
            "inbox": folders.get("inbox", "INBOX"),
            "next": folders.get("next", "next"),
            "waiting": folders.get("waiting", "waiting"),
            "someday": folders.get("someday", "someday"),
        }
    
        if mailbox:
            if mailbox not in mailboxes:
                return {}
            mailboxes = {mailbox: mailboxes[mailbox]}
    
        result = {}
        for key, folder in mailboxes.items():
            cache_key = f"overview_{folder}"
            if cache_key in self.cache:
                ttl = self.config.get("cache", {}).get("ttl_seconds", 300)
                cache_time = self.cache_timestamps.get(cache_key, datetime.min)
                if (datetime.now() - cache_time).total_seconds() < ttl:
                    result[key] = self.cache[cache_key]
                    continue
    
            try:
                emails = self.fetch_emails(folder, limit=limit)
                status = self.get_mailbox_status(folder)
                overview = {
                    "emails": [
                        {
                            "uid": e.uid,
                            "sender": e.from_address.email if e.from_address else "",
                            "sender_name": e.from_address.name if e.from_address else None,
                            "subject": e.subject,
                            "date": e.date.isoformat() if e.date else None,
                            "unread": "\\Seen" not in e.flags,
                        }
                        for e in emails
                    ],
                    "total": status.exists,
                    "unread": status.unseen,
                    "last_updated": datetime.now().isoformat(),
                }
                self.cache[cache_key] = overview
                self.cache_timestamps[cache_key] = datetime.now()
                result[key] = overview
            except Exception as e:
                result[key] = {"error": str(e)}
    
        return result
  • Tool registration logic for "get_cached_overview" within the MCP server handler.
    elif name == "get_cached_overview":
        return imap_client.get_cached_overview(
            mailbox=args.get("mailbox"),
            limit=args.get("limit", 20),
        )

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