Skip to main content
Glama
email-mcp

email-mcp

by a1exsun
README.md
<div align="center">

<h1>📬 email-mcp</h1>

<p><b>Give your AI assistant a real mailbox.</b><br>
An MCP server that lets Claude Code, Codex and other MCP clients read, search, organize and send email over plain IMAP/SMTP.</p>

<p>
  <a href="LICENSE"><img alt="License: MIT" src="https://img.shields.io/badge/license-MIT-blue.svg"></a>
  <img alt="Node.js 22.18+" src="https://img.shields.io/badge/node-%E2%89%A5%2022.18-339933?logo=node.js&logoColor=white">
  <img alt="TypeScript" src="https://img.shields.io/badge/TypeScript-strict-3178C6?logo=typescript&logoColor=white">
  <img alt="MCP stdio server" src="https://img.shields.io/badge/MCP-stdio%20server-8A2BE2">
  <a href="https://github.com/a1exsun/email-mcp/pulls"><img alt="PRs welcome" src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg"></a>
</p>

<p>
  <a href="#-quick-start">Quick start</a> ·
  <a href="#-tools">Tools</a> ·
  <a href="#%EF%B8%8F-configuration">Configuration</a> ·
  <a href="#-providers">Providers</a> ·
  <a href="#-connect-your-client">Connect your client</a> ·
  <a href="#-safety">Safety</a>
</p>

<p><b>English</b> · <a href="README.zh-CN.md">简体中文</a></p>

</div>

<br>

<table>
<tr>
<td width="33%" valign="top">
<h3>🌐 Any provider</h3>
Gmail, Outlook / Microsoft 365, iCloud, Yahoo, Fastmail, QQ, 163 / 126 and any self-hosted or company server that speaks IMAP and SMTP.
</td>
<td width="33%" valign="top">
<h3>🗂️ Several mailboxes</h3>
One server instance serves all your accounts. Every tool takes an <code>account</code> parameter, so the assistant picks the right mailbox.
</td>
<td width="33%" valign="top">
<h3>🛡️ Safe by design</h3>
No permanent delete: mail goes to Trash, only empty folders can be removed, and <code>save_draft</code> lets a human press Send.
</td>
</tr>
</table>

## 🚀 Quick start

### Let your AI set it up

Paste this one line into Claude Code, Codex or any assistant that can run shell commands:

```text
Read https://raw.githubusercontent.com/a1exsun/email-mcp/main/skills/setup-email-mcp/SKILL.md and follow it to set up email-mcp for me.
```

The [setup skill](skills/setup-email-mcp/SKILL.md) clones and builds the project, works out your provider's servers from your address, writes `.env`, verifies the login and registers the server with your client. It never asks for a password in chat: you paste the app password into `.env` yourself.

### Or do it by hand

<table>
<tr><td><b>1</b></td><td>

Clone and build (requires Node.js ≥ 22.18):

```bash
git clone https://github.com/a1exsun/email-mcp.git && cd email-mcp && npm install
```

</td></tr>
<tr><td><b>2</b></td><td>

Create your configuration and fill it in (see [Configuration](#%EF%B8%8F-configuration)):

```bash
cp .env.example .env
```

</td></tr>
<tr><td><b>3</b></td><td>

Check that every mailbox can log in to IMAP and SMTP:

```bash
node --env-file=.env dist/index.js check
```

</td></tr>
<tr><td><b>4</b></td><td>

[Connect your client](#-connect-your-client) and restart it.

</td></tr>
</table>

## 🧰 Tools

<table>
<thead>
<tr><th align="left">Tool</th><th align="left">What it does</th></tr>
</thead>
<tbody>
<tr><td colspan="2"><b>📖 Read</b></td></tr>
<tr><td><code>list_folders</code></td><td>List folders and identify Inbox, Sent, Drafts, Trash, Junk and Archive</td></tr>
<tr><td><code>search_emails</code></td><td>Filter by sender, recipient, subject, body, date, read state, star or size. Gmail also accepts its native search syntax</td></tr>
<tr><td><code>read_email</code></td><td>Read one email: headers, body (HTML converted to text) and attachment list</td></tr>
<tr><td><code>download_attachment</code></td><td>Save an attachment to a local folder</td></tr>
<tr><td colspan="2"><b>✉️ Write</b></td></tr>
<tr><td><code>send_email</code></td><td>Send with cc / bcc, HTML and attachments. Pass <code>reply_to_uid</code> to reply inside the original thread</td></tr>
<tr><td><code>forward_email</code></td><td>Forward an email together with its attachments</td></tr>
<tr><td><code>save_draft</code></td><td>Save to Drafts without sending, so a human can review it first</td></tr>
<tr><td colspan="2"><b>🗃️ Organize</b></td></tr>
<tr><td><code>move_emails</code></td><td>Move emails to another folder (archive or sort)</td></tr>
<tr><td><code>delete_emails</code></td><td>Move emails to Trash. Recoverable; there is no permanent delete</td></tr>
<tr><td><code>set_flags</code></td><td>Mark read / unread and starred / unstarred</td></tr>
<tr><td><code>create_folder</code><br><code>rename_folder</code><br><code>delete_folder</code></td><td>Manage folders. <code>delete_folder</code> only removes empty folders</td></tr>
</tbody>
</table>

Then just ask in plain language:

> *"Summarize the unread mail in my work inbox from this week."*<br>
> *"Find the invoice Alice sent in October and save the PDF to ~/Downloads."*<br>
> *"Draft a reply to the latest message from my landlord, but don't send it."*

## ⚙️ Configuration

The server is configured through environment variables. The simplest way is a `.env` file loaded with Node's built-in `--env-file` flag; [`.env.example`](.env.example) documents every variable.

<table>
<thead>
<tr><th align="left">Variable</th><th align="center">Required</th><th align="left">Description</th></tr>
</thead>
<tbody>
<tr><td><code>EMAIL_ADDRESS</code></td><td align="center">✅</td><td>Your address, also the login username</td></tr>
<tr><td><code>EMAIL_PASSWORD</code></td><td align="center">✅ <sup>1</sup></td><td>App password or authorization code</td></tr>
<tr><td><code>IMAP_HOST</code></td><td align="center">✅</td><td>IMAP server</td></tr>
<tr><td><code>SMTP_HOST</code></td><td align="center">✅</td><td>SMTP server</td></tr>
<tr><td><code>IMAP_PORT</code></td><td align="center"></td><td>Default <code>993</code> (always TLS)</td></tr>
<tr><td><code>SMTP_PORT</code></td><td align="center"></td><td>Default <code>465</code> (implicit TLS); any other port uses STARTTLS</td></tr>
<tr><td><code>EMAIL_FROM</code></td><td align="center"></td><td>From header when it differs from the login, e.g. <code>"Alex &lt;me@mydomain.com&gt;"</code></td></tr>
<tr><td><code>EMAIL_SAVE_SENT</code></td><td align="center"></td><td><code>true</code> / <code>false</code>: save a copy of sent mail to Sent. Default: off for Gmail and Outlook (they save one themselves), on elsewhere</td></tr>
<tr><td><code>EMAIL_TRASH_FOLDER</code></td><td align="center"></td><td>Folder used by <code>delete_emails</code>. Default: the server's Trash</td></tr>
<tr><td><code>OUTLOOK_CLIENT_ID</code></td><td align="center"></td><td>Outlook only. Setting it switches the mailbox to OAuth2</td></tr>
<tr><td><code>OUTLOOK_TENANT</code></td><td align="center"></td><td>Default <code>common</code></td></tr>
<tr><td><code>OUTLOOK_TOKEN_CACHE</code></td><td align="center"></td><td>Token cache path. Default <code>~/.email-mcp/msal-cache.json</code></td></tr>
<tr><td><code>EMAIL_ACCOUNTS</code></td><td align="center"></td><td>Names of several mailboxes, see below</td></tr>
</tbody>
</table>

<sup>1</sup> Not needed when <code>OUTLOOK_CLIENT_ID</code> is set.

### Several mailboxes

List a short name for each mailbox in `EMAIL_ACCOUNTS`, then prefix every variable with the upper-cased name:

```ini
EMAIL_ACCOUNTS=work,home

WORK_EMAIL_ADDRESS=me@company.com
WORK_EMAIL_PASSWORD=...
WORK_IMAP_HOST=imap.company.com
WORK_SMTP_HOST=smtp.company.com

HOME_EMAIL_ADDRESS=me@icloud.com
HOME_EMAIL_PASSWORD=...
HOME_IMAP_HOST=imap.mail.me.com
HOME_SMTP_HOST=smtp.mail.me.com
HOME_SMTP_PORT=587
```

Every tool then accepts `account` (`work` or `home` here) and defaults to the first name in the list. Names may contain letters, digits and underscores.

## 📮 Providers

<table>
<thead>
<tr><th align="left">Provider</th><th align="left">IMAP</th><th align="left">SMTP</th><th align="left">Credential</th></tr>
</thead>
<tbody>
<tr><td>Gmail / Google Workspace</td><td><code>imap.gmail.com:993</code></td><td><code>smtp.gmail.com:465</code></td><td><a href="https://myaccount.google.com/apppasswords">App password</a> (needs 2-step verification)</td></tr>
<tr><td>Outlook / Microsoft 365</td><td><code>outlook.office365.com:993</code></td><td><code>smtp.office365.com:587</code></td><td>OAuth2, see below</td></tr>
<tr><td>iCloud</td><td><code>imap.mail.me.com:993</code></td><td><code>smtp.mail.me.com:587</code></td><td><a href="https://account.apple.com">App-specific password</a></td></tr>
<tr><td>Yahoo</td><td><code>imap.mail.yahoo.com:993</code></td><td><code>smtp.mail.yahoo.com:465</code></td><td>App password</td></tr>
<tr><td>Fastmail</td><td><code>imap.fastmail.com:993</code></td><td><code>smtp.fastmail.com:465</code></td><td>App password</td></tr>
<tr><td>QQ Mail</td><td><code>imap.qq.com:993</code></td><td><code>smtp.qq.com:465</code></td><td>Authorization code (Settings → Account → enable IMAP/SMTP)</td></tr>
<tr><td>163 / 126 Mail</td><td><code>imap.163.com:993</code><br><code>imap.126.com:993</code></td><td><code>smtp.163.com:465</code><br><code>smtp.126.com:465</code></td><td>Authorization code (Settings → POP3/SMTP/IMAP)</td></tr>
<tr><td>Self-hosted / company</td><td colspan="2" align="center">ask your administrator</td><td>Account password</td></tr>
</tbody>
</table>

<details>
<summary><b>iCloud with a custom domain</b></summary>
<br>

`EMAIL_ADDRESS` must be your `@icloud.com` address, because that is the login. Put the custom-domain sender in `EMAIL_FROM`:

```ini
EMAIL_ADDRESS=me@icloud.com
EMAIL_FROM="Alex <me@mydomain.com>"
```

</details>

<details>
<summary><b>Outlook / Microsoft 365 OAuth2 (one-time setup)</b></summary>
<br>

Microsoft disabled password login for IMAP and SMTP, so Outlook mailboxes sign in with OAuth2.

1. In the [Azure portal → App registrations](https://portal.azure.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade), create an app:
   - Supported account types: **any organizational directory + personal Microsoft accounts**.
   - Under **Authentication**, turn on **Allow public client flows**.
   - Under **API permissions**, add the delegated permissions `IMAP.AccessAsUser.All` and `SMTP.Send`.
   - Copy the **Application (client) ID** into `OUTLOOK_CLIENT_ID`.
2. Log in once from a terminal (device-code flow: open the link it prints and enter the code):
   ```bash
   node --env-file=.env dist/index.js login
   ```
   With several mailboxes, add the account name: `node --env-file=.env dist/index.js login work`.
3. The token is cached in `~/.email-mcp/` with permissions `600` and refreshes automatically.

On Microsoft 365 business accounts an administrator may need to enable SMTP AUTH for the mailbox.

</details>

## 🔌 Connect your client

Point your MCP client at `dist/index.js` and let Node load the `.env` file. Use absolute paths.

<details open>
<summary><b>Claude Code</b></summary>
<br>

```bash
claude mcp add email -s user -- node --env-file=/absolute/path/to/email-mcp/.env /absolute/path/to/email-mcp/dist/index.js
```

</details>

<details>
<summary><b>Codex</b> (<code>~/.codex/config.toml</code>)</summary>
<br>

```toml
[mcp_servers.email]
command = "node"
args = ["--env-file=/absolute/path/to/email-mcp/.env", "/absolute/path/to/email-mcp/dist/index.js"]
```

</details>

<details>
<summary><b>Claude Desktop, Cursor and other clients</b> (<code>mcpServers</code> JSON)</summary>
<br>

```json
{
  "mcpServers": {
    "email": {
      "command": "node",
      "args": ["--env-file=/absolute/path/to/email-mcp/.env", "/absolute/path/to/email-mcp/dist/index.js"]
    }
  }
}
```

</details>

You can also pass the variables through the client's own `env` setting instead of a `.env` file.

### Command line

| Command | Purpose |
|---|---|
| `node dist/index.js` | Start the MCP server on stdio (what your client runs) |
| `node dist/index.js check [account]` | Log in to IMAP and SMTP for each mailbox and report `OK` / `FAILED` |
| `node dist/index.js login [account]` | One-time Outlook OAuth2 device-code login |

## 🔒 Safety

> [!IMPORTANT]
> `send_email` and `forward_email` really send mail. Keep your client's confirmation prompt on for them, and prefer `save_draft` for unattended runs.

- **Use an app password or authorization code**, never your main account password. It is stored in plain text in `.env`, so keep that file private (`chmod 600 .env`). It is git-ignored.
- **Nothing is deleted permanently.** `delete_emails` moves mail to Trash, and `delete_folder` refuses folders that still contain mail.
- **Your mail stays between you and your provider.** The server talks only to the IMAP/SMTP hosts you configure (and Microsoft's login endpoint for Outlook); there is no telemetry.
- **Email is untrusted input.** A message can contain text written to manipulate an AI assistant. Review what the assistant intends to do before approving actions triggered by mail content.

## 🛠️ Development

```bash
npm install     # install and build
npm test        # run the tests (node --test, runs the .ts sources directly)
npm run build   # compile to dist/
```

<table>
<tr><td><code>src/index.ts</code></td><td>MCP server, tool definitions and the <code>check</code> / <code>login</code> commands</td></tr>
<tr><td><code>src/mail.ts</code></td><td>IMAP / SMTP helpers and message composition</td></tr>
<tr><td><code>src/auth.ts</code></td><td>Outlook OAuth2 (MSAL device-code flow and token cache)</td></tr>
<tr><td><code>src/env.ts</code></td><td>Environment lookup with per-account prefixes</td></tr>
</table>

Built on the official [`@modelcontextprotocol/sdk`](https://github.com/modelcontextprotocol/typescript-sdk), [`imapflow`](https://imapflow.com), [`nodemailer`](https://nodemailer.com), [`mailparser`](https://nodemailer.com/extras/mailparser/) and [`@azure/msal-node`](https://github.com/AzureAD/microsoft-authentication-library-for-js).

Issues and pull requests are welcome. Please run `npm test` before opening a PR.

## 📄 License

[MIT](LICENSE) © Alex Sun

TDQS

B3.2/5.0

Scored across 13 tools

Disambiguation5/5

Each tool has a clearly distinct operation: folder listing/creation/renaming/deletion, email search/read/send/save/forward/move/delete, flag updates, and attachment download. There is minimal overlap because reply is handled as an option on send_email, while save_draft and forward_email remain clearly separate workflows.

Naming Consistency5/5

All tool names use a consistent snake_case verb_noun pattern such as list_folders, read_email, move_emails, and delete_folder. This makes the set predictable and easy to scan.

Tool Count5/5

The server exposes 13 tools, which is well-scoped for email management. Folders, messages, drafts, attachments, and flags each get focused operations without an excessive number of tools.

Completeness4/5

The tool set covers the main email lifecycle: folder CRUD, search/read, send/reply, save draft, forward, move, delete, flags, and attachment download. Minor gaps exist, such as explicit draft editing or reply-all, but core workflows are well supported.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues