mail-mcp
README.md
# mail-mcp
**English** · [Italiano](README.it.md)
A local MCP server that connects Claude (Desktop or Code) to your mailboxes. It reads
mail over **IMAP** and sends over **SMTP**. It handles **several accounts at once**,
with ready-made settings for **iCloud** and **Gmail**. Any other IMAP/SMTP provider
can be configured by hand.
It runs on your Mac. Passwords stay in the macOS Keychain and no port is exposed.
## Tools
Every tool takes an `account` parameter (`"icloud"`, `"gmail"`, ...). Searches and
folder listings cover **all** accounts when no account is given. Actions on a message
and sending always require an account when more than one is configured.
| Tool | What it does |
|---|---|
| `list_accounts` | Lists the configured accounts |
| `list_folders` | Lists folders with total and unread counts |
| `search_messages` | Searches by sender, recipient, subject, text, dates, unread or flagged, newest first. On Gmail it also accepts `gmailQuery` in Gmail search syntax (`has:attachment`, `label:invoices`, `newer_than:7d`, `category:primary`, ...) |
| `read_message` | Returns headers, plain-text body and the list of attachments. It does **not** mark the message as read unless `markAsRead` is set |
| `save_attachment` | Saves an attachment to `~/Downloads/mail-mcp`. Small text attachments are also returned inline |
| `set_flags` | Marks messages read/unread and flagged/unflagged |
| `move_messages` | Moves messages to another folder in the same account |
| `send_email` | Sends over SMTP (STARTTLS required), with attachments and threaded replies (`In-Reply-To`/`References`) |
| `save_draft` | Saves a draft without sending it, so you can review it in Mail or Gmail |
### Common folder names
iCloud and Gmail name their folders differently (`Sent Messages` versus
`[Gmail]/Sent Mail`). Wherever a folder is expected you can use these aliases. They are
resolved through IMAP special-use folders (RFC 6154), so they also work with localized
Gmail folder names.
| Alias | iCloud | Gmail |
|---|---|---|
| `sent` | Sent Messages | [Gmail]/Sent Mail |
| `drafts` | Drafts | [Gmail]/Drafts |
| `trash` | Deleted Messages | [Gmail]/Trash |
| `junk` / `spam` | Junk | [Gmail]/Spam |
| `archive` | Archive | [Gmail]/All Mail (Gmail's "archive") |
### Provider differences handled for you
- **Copy in Sent:** iCloud does not keep one for mail sent over SMTP, so the server
appends it over IMAP. Gmail keeps one automatically, so the server does not append
another and you get no duplicates.
- **Archiving** in Gmail means moving the message to All Mail, which removes the Inbox
label. The `archive` alias does exactly that.
- **Gmail labels** appear as folders over IMAP. To search by label, use
`gmailQuery: "label:name"`.
## Quick setup (recommended)
Run this in Terminal on your Mac:
```sh
bash <(curl -fsSL https://raw.githubusercontent.com/fz4q8ggzrh-ux/MCP_IMAP_Connector/main/setup.sh)
```
The script guides you step by step. It:
1. checks for Node.js 20+ and offers to install it with Homebrew;
2. downloads and builds the server in `~/mail-mcp`;
3. asks for your iCloud and Gmail addresses, opens the page where you create the app
password, and **tests the login before saving anything**. If the password is wrong
it tells you why and lets you try again. For iCloud it also tries the short IMAP
username automatically;
4. stores the passwords in the macOS Keychain;
5. asks whether Claude may send email or only read it, and optionally which recipients
are allowed;
6. writes `~/.config/mail-mcp/config.json` and adds the connector to Claude Desktop and
Claude Code.
Run it again at any time to change accounts or passwords. It reuses the passwords
already in the Keychain. To test the saved configuration at any time:
```sh
node ~/mail-mcp/dist/index.js --check
```
## Manual setup
You need Node.js 20 or later (`brew install node`).
```sh
git clone https://github.com/fz4q8ggzrh-ux/MCP_IMAP_Connector.git ~/mail-mcp
cd ~/mail-mcp
npm install
npm run build
```
### 1. Create app passwords
Neither iCloud nor Gmail accepts your normal password over IMAP/SMTP.
- **iCloud:** go to <https://account.apple.com> → **Sign-In and Security** →
**App-Specific Passwords**, and create one named "mail-mcp".
- **Gmail:** **2-Step Verification** must be on. Then go to
<https://myaccount.google.com/apppasswords>, create one named "mail-mcp" and copy
the 16 letters without spaces. Google Workspace admins can disable app passwords.
### 2. Store them in the Keychain
Add one entry per account, with service `mail-mcp` and your email address as the
account:
```sh
security add-generic-password -s mail-mcp -a name@icloud.com -w
security add-generic-password -s mail-mcp -a name@gmail.com -w
```
Each command prompts for the password. Paste the app password.
### 3. Configuration file
Create `~/.config/mail-mcp/config.json`:
```json
{
"accounts": [
{ "name": "icloud", "user": "name@icloud.com", "from": "First Last <name@icloud.com>" },
{ "name": "gmail", "user": "name@gmail.com", "from": "First Last <name@gmail.com>" }
]
}
```
The provider is detected from the domain (`icloud.com`, `me.com`, `mac.com`,
`gmail.com`). For a custom domain hosted by iCloud or Google Workspace, add
`"provider": "icloud"` or `"provider": "gmail"`.
### 4. Connect it to Claude
**Claude Desktop:** edit `~/Library/Application Support/Claude/claude_desktop_config.json`
(Settings → Developer → Edit Config):
```json
{
"mcpServers": {
"mail": {
"command": "/opt/homebrew/bin/node",
"args": ["/Users/YOUR_USER/mail-mcp/dist/index.js"]
}
}
}
```
Use the full path to `node` (see `which node`), because Claude Desktop does not read
your shell's PATH. Then restart Claude Desktop.
**Claude Code:**
```sh
claude mcp add mail --scope user -- node ~/mail-mcp/dist/index.js
```
Try asking: *"What unread email do I have in iCloud and Gmail?"*
## Full configuration
Fields for each account in `config.json`:
| Field | Default | Notes |
|---|---|---|
| `user` | required | Full address. Used for SMTP login and the Keychain lookup |
| `name` | the provider (`icloud`, `gmail`) | Account name used by the tools. Required if you have two accounts with the same provider |
| `provider` | from the domain | `icloud`, `gmail` or `custom` |
| `from` | `user` | Sender shown to recipients, e.g. `"Name <alias@icloud.com>"`. On Gmail a different address only works if it is set up under "Send mail as"; otherwise Gmail replaces it with yours |
| `imapUser` | `user` | iCloud: if it rejects the full address, try only the part before the `@` |
| `imap` / `smtp` | provider preset | `{ "host": "...", "port": 993, "secure": true }`. Required with `custom` |
| `saveSentCopy` | `true` (iCloud, custom), `false` (Gmail) | Appends a copy to Sent after sending |
| `sentFolder` / `draftsFolder` | detected | Only needed if automatic detection fails |
| `readOnly` | `false` | Read-only for this account |
| `allowedRecipients` | everyone | Allowed addresses or domains, e.g. `["boss@work.com", "@family.org"]` |
| `keychainService` | `mail-mcp` | Keychain service to read the password from |
At the top level of the file you can also set `downloadDir`, `readOnly` and
`allowedRecipients`, which apply to every account.
Environment variables, useful for tests or if you prefer not to use the file:
| Variable | Use |
|---|---|
| `MAIL_CONFIG` | Alternative path to the configuration file |
| `MAIL_ACCOUNTS` | The account list as JSON, instead of the file |
| `MAIL_PASSWORD_<NAME>` | Password for account `<name>` (e.g. `MAIL_PASSWORD_GMAIL`), instead of the Keychain |
| `MAIL_USER`, `MAIL_PASSWORD`, `MAIL_PROVIDER`, ... | Single-account configuration |
| `MAIL_READ_ONLY=1` | Makes every account read-only |
| `MAIL_ALLOWED_RECIPIENTS` | Global allow-list, comma-separated |
| `MAIL_DOWNLOAD_DIR` | Folder for saved attachments |
## Security
- Claude asks for confirmation before each tool call unless you allowed it permanently.
Keep that confirmation on for `send_email`.
- `send_email` is marked as destructive. Its description tells Claude to show you the
sending account, recipients, subject and body before sending. For maximum caution,
use `save_draft` and send the message yourself.
- Sending always requires an explicit account, so Claude cannot pick the sending
address on its own.
- Email content is external text, and a message can contain instructions aimed at the
assistant. Consider `readOnly` or `allowedRecipients` for mailboxes that receive a lot
of mail from unknown senders.
- Each operation opens one IMAP connection and closes it. No session stays open.
## Tests
```sh
npm test # unit tests
```
The end-to-end test uses two local Dovecot IMAP servers over TLS (port 1993 with
iCloud's folder layout, port 1994 with Gmail's) and two SMTP test servers with STARTTLS,
which the test starts itself:
```sh
MAIL_E2E=1 npm test
```
The expected credentials and folders are described at the top of `test/e2e.test.mjs`.
## License
[MIT](LICENSE)
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues