Skip to main content
Glama
ABMIC

ABMIC

by zany92
README.md
# ABMIC — A Better MCP Server for iCloud Mail and IMAP

A production-hardened [Model Context Protocol](https://modelcontextprotocol.io/) (MCP) server for iCloud Mail, built for real-world use with AI agents like Claude. Manage thousands of emails reliably — search, move, flag, delete, and send — without crashes or data loss.

> **Forked from [minagishl/icloud-mail-mcp](https://github.com/minagishl/icloud-mail-mcp)** — original work by [minagishl](https://github.com/minagishl). Thank you for the solid foundation!

## Why ABMIC?

The original `icloud-mail-mcp` is a great starting point, but when used in production with large mailboxes (30,000+ emails) and AI agents performing bulk operations, several critical issues surface. ABMIC addresses all of them:

### Critical Bug Fixes

| Bug | Impact | Fix |
|-----|--------|-----|
| `moveMessages` ignored provided message IDs | Used `search(['ALL'])` internally — moved **every message** in the mailbox instead of the targeted ones | Now uses the provided UIDs directly with `imap.move()` |
| `setFlags` ignored provided message IDs | Same `search(['ALL'])` bug — flagged/unflagged **all messages** | Now uses provided UIDs directly |
| `deleteMessages` ignored provided message IDs | Same bug — would delete **all messages** in the mailbox | Now uses provided UIDs with `addFlags(['\\Deleted'])` + `expunge()` |
| `getMailboxes` crashed with circular JSON | IMAP box objects contain circular parent/children references | Added `sanitizeBoxes()` to strip circular refs before serialization |

### Performance & Stability Improvements

| Improvement | Before | After |
|-------------|--------|-------|
| **Header-only fetching** | `getMessages` and `searchMessages` downloaded full email bodies + attachments (`bodies: ''`) — caused OOM crashes on large result sets | Fetches only `HEADER.FIELDS (FROM TO SUBJECT DATE MESSAGE-ID)` — 100x lighter, no more crashes |
| **IMAP auto-reconnection** | Connection drops between MCP calls caused silent failures | `ensureConnected()` checks `imap.state` before every operation; automatically destroys dead connections and reconnects |
| **Proper UID tracking** | Message IDs used `parsed.messageId` (the Message-ID header) — unusable for IMAP operations | All methods now return IMAP UIDs via `attrs.uid`, consistent across move/flag/delete operations |
| **Sorted results** | Messages returned in arbitrary IMAP fetch order | Results sorted by date (most recent first) |

### New Features

| Feature | Description |
|---------|-------------|
| **Email alias support** | Send from an alias (e.g., `user@customdomain.com`) via `ICLOUD_SEND_FROM` env var |
| **Sent folder copy** | Sent emails are automatically copied to the "Sent Messages" IMAP folder, so they appear in all mail clients |
| **Robust connection lifecycle** | Dedicated `createImapInstance()` factory, `connected` state tracking, and event-based cleanup on close/end/error |

## Installation

```bash
git clone https://github.com/zany92/abmic.git
cd abmic
npm install   # or pnpm install
npm run build
```

## Configuration

### Claude Desktop / Claude Code

Add to your MCP server configuration (`claude_desktop_config.json`):

```json
{
  "mcpServers": {
    "icloud-mail": {
      "command": "node",
      "args": ["/path/to/abmic/dist/index.js"],
      "env": {
        "ICLOUD_EMAIL": "you@icloud.com",
        "ICLOUD_APP_PASSWORD": "xxxx-xxxx-xxxx-xxxx",
        "ICLOUD_SEND_FROM": "you@yourdomain.com"
      }
    }
  }
}
```

### Environment Variables

| Variable | Required | Description |
|----------|----------|-------------|
| `ICLOUD_EMAIL` | Yes | Your iCloud email address (e.g., `user@icloud.com`) |
| `ICLOUD_APP_PASSWORD` | Yes | App-specific password ([generate one here](https://appleid.apple.com)) |
| `ICLOUD_SEND_FROM` | No | Email alias to use as the "From" address when sending |

### Prerequisites

1. An iCloud account with Mail enabled
2. Two-factor authentication enabled
3. An [app-specific password](https://support.apple.com/en-us/102654) (not your main iCloud password)

## Available Tools

### Email Operations

- **`get_messages`** — List recent emails from any mailbox (headers only, fast)
- **`search_messages`** — Search by query, sender, date range, read status
- **`send_email`** — Send emails (supports alias via `ICLOUD_SEND_FROM`)
- **`move_messages`** — Move emails between folders using IMAP UIDs
- **`delete_messages`** — Delete specific emails by UID
- **`set_flags`** — Add/remove flags (`\Seen`, `\Flagged`, `\Deleted`, etc.)
- **`mark_as_read`** — Shortcut to mark messages as read
- **`download_attachment`** — Download a specific attachment from a message
- **`auto_organize`** — Apply rule-based organization (sender/subject matching)

### Mailbox Management

- **`get_mailboxes`** — List all IMAP folders (with nested subfolder support)
- **`create_mailbox`** — Create new folders (supports paths like `Parent/Child`)
- **`delete_mailbox`** — Delete folders (with system folder protection)

### System

- **`test_connection`** — Verify IMAP and SMTP connectivity
- **`check_config`** — Check environment variable configuration

## Architecture

```
src/
  index.ts                    # MCP server entry point, tool definitions
  lib/
    icloud-mail-client.ts     # Core IMAP/SMTP client with all fixes
  types/
    config.ts                 # TypeScript type definitions
```

Key design decisions:

- **UID-based operations throughout** — all message IDs are IMAP UIDs, not sequence numbers or Message-ID headers
- **Headers-only for listing/searching** — full body fetch only when explicitly needed (attachments)
- **Defensive reconnection** — every IMAP method calls `ensureConnected()` before operating
- **No circular references** — `sanitizeBoxes()` ensures JSON-safe mailbox listings

## iCloud Mail Server Settings

| Protocol | Server | Port | Security |
|----------|--------|------|----------|
| IMAP | imap.mail.me.com | 993 | TLS/SSL |
| SMTP | smtp.mail.me.com | 587 | STARTTLS |

## Development

```bash
npm run dev          # Start with inspector
npm run build        # Build to dist/
npm run typecheck    # TypeScript checking
npm run test         # Run tests (vitest)
npm run lint         # ESLint
npm run format       # Prettier
```

## Credits

This project is a fork of [icloud-mail-mcp](https://github.com/minagishl/icloud-mail-mcp) by **[minagishl](https://github.com/minagishl)**, originally built during [Hack Club's Summer of Making](https://summer.hackclub.com/projects/7559). The original project provided the MCP server scaffold, tool definitions, IMAP/SMTP integration, and test suite.

ABMIC extends it with production-critical fixes discovered while managing a 30,000+ email iCloud mailbox with Claude as an AI agent — including the UID bugs that could silently move or delete an entire mailbox, the OOM crashes from full-body fetching, and the connection reliability issues during long-running sessions.

## License

MIT License — see [LICENSE](LICENSE) for details.

Copyright (c) 2025 Minagishl (original work), 2026 Vincent Picou (ABMIC modifications)

TDQS

B3.4/5.0

Scored across 14 tools

Disambiguation4/5

Most tools target distinct resource+action pairs (mailboxes, messages, attachments, connection diagnostics). The main overlap is mark_as_read versus set_flags, which can both toggle read state, but the dedicated convenience tool is reasonably explained. auto_organize is also somewhat broad relative to move_messages, though its rule-based intent differentiates it.

Naming Consistency4/5

All names are snake_case with a clear verb_noun pattern (get_messages, delete_mailbox, download_attachment), making the set easy to scan. Minor deviations like auto_organize and test_connection lack a resource noun, but the convention is otherwise uniform.

Tool Count5/5

Fourteen tools fit an email client's scope well: message operations, mailbox management, attachments, and diagnostics. Each tool covers a distinct workflow rather than padding the surface.

Completeness4/5

Core lifecycle is covered: read, search, send, move, delete, flag, mailbox create/delete, and attachment download. Gaps include no mailbox rename, no reply/forward, and no attachment upload or single-message detail fetch, but agents can work around these via send_email with attachments and get_messages.

Maintenance

ActivityInactive
ResponsivenessNo recent community issues