Skip to main content
Glama
iCloud Relay

iCloud Relay

by naofumi
README.md
# icloud-relay

**Stateless MCP bridge from Grok Bot (Cursor) to iCloud Mail.**

Hosted as a [Cursor App](https://icloud-relay.apps.grok.bot) with Streamable HTTP MCP at:

`https://icloud-relay.apps.grok.bot/mcp`

The relay never stores mailbox passwords. Clients send an Apple ID and an **app-specific password** only in connector request headers for that call. A shared relay token gates `tools/call`. SMTP send is **off** unless you explicitly enable it.

[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](./LICENSE)

## Features

- List folders (Unicode NFC/NFD paths)
- Search with filters, newest-first date order, and cursor pagination
- Peek-read (decoded text/html; never sets `\Seen` on read)
- Mark read/unread
- Create drafts (plain and/or HTML `multipart/alternative`)
- Optional SMTP send (two-step confirm token, 15s delay) when `MAIL_SEND_ENABLED=true`

Out of scope: push/IDLE, attachment byte download, move/delete, folder admin. Full matrix: [docs/FEATURES.md](./docs/FEATURES.md).

## Security model

| Control | Behavior |
| --- | --- |
| Credentials | Only `X-iCloud-Mail-User` / `X-iCloud-Mail-Password` per request — not in app env or this repo |
| Relay gate | `RELAY_ACCESS_TOKEN` via `X-iCloud-Relay-Token` (fail closed) |
| Client IP | Rate limits use platform `x-vercel-forwarded-for` only; missing IP is **403** unless local `RELAY_ALLOW_INSECURE=1` |
| Concurrency | Per-mailbox, per-IP, and process caps (`withMailSlot`) |
| Send | Disabled by default; preview → wait → confirm with HMAC token |
| Logging | Passwords are never logged or echoed |

Treat the public MCP URL like any credentialed proxy: keep the relay token secret, use Apple **app-specific** passwords, and prefer drafts over send.

## Connect from Grok Bot

1. Add a Remote HTTPS connector to `https://icloud-relay.apps.grok.bot/mcp`.
2. Set secure headers in the connector only:
   - `X-iCloud-Mail-User` — Apple ID email
   - `X-iCloud-Mail-Password` — app-specific password
   - `X-iCloud-Relay-Token` — same value as the app secret `RELAY_ACCESS_TOKEN`
3. Reconnect and confirm tools load.

## Run locally

```bash
npm install
npm test
npm run build
npm start
```

Local server: `http://127.0.0.1:3847` (`/` status, `/health`, `/mcp`).

```bash
# Local tool calls without a deployed relay token:
RELAY_ALLOW_INSECURE=1 npm start

# Point IMAP at the FakeImap fixture (see tests):
IMAP_HOST=127.0.0.1 IMAP_PORT=<port> IMAP_SECURE=0
```

App secrets (Cursor Apps):

| Name | Purpose |
| --- | --- |
| `RELAY_ACCESS_TOKEN` | Shared gate for `tools/call` |
| `MAIL_SEND_ENABLED` | Set `true` only if SMTP send should be allowed |

## Architecture

```
Grok Bot  --HTTPS MCP-->  Cursor App (api/mcp.js)
                              |
                         ImapFlow / SMTP  -->  iCloud Mail
```

- `src/mcp/` — Streamable HTTP JSON-RPC (`/mcp`)
- `src/mail/` — IMAP, SMTP, MIME compose, sanitize, send-intent
- `src/limits.ts` — rate limits and concurrency slots
- `api/` + `scripts/build.mjs` — Cursor Apps / Vercel-style bundle
- `test/fixtures/fake-imap-server.ts` — offline IMAP for unit/E2E

Each `/mcp` POST is independent (no session store).

## Verification (for agents)

- **Drive the real HTTP surface:** `.cursor/skills/verify-icloud-relay/` (FakeImap + free ports)
- **Formal concurrency caps:** `.cursor/skills/verify-mail-slots/` — TLA+/TLC model of `withMailSlot` plus `formal/mail-slots/check-code.mjs`
- **Invariants and layout:** [AGENTS.md](./AGENTS.md)

```bash
node --import tsx .cursor/skills/verify-icloud-relay/bin/verify.mjs launch
node --import tsx .cursor/skills/verify-icloud-relay/bin/verify.mjs drive health-status
node --import tsx .cursor/skills/verify-icloud-relay/bin/verify.mjs cleanup

bash formal/mail-slots/run.sh
node --import tsx formal/mail-slots/check-code.mjs
```

## License

[MIT](./LICENSE) © 2026 Naofumi Kagami