imap-mcp-server
README.md
# imap-mcp-server
An [MCP](https://modelcontextprotocol.io) server that gives AI assistants (Claude, Grok, Gemini and any other MCP client) a **real work mailbox**: any IMAP/SMTP account, plus an optional CalDAV calendar. It was built for day-to-day export sales work on a regular hosted mailbox, with no Gmail or Microsoft 365 API involved, and it has been in production use every day since 2026.
It runs over **stdio** (local) or **Streamable HTTP** (self-hosted, always on), and also exposes a small **REST API**, so tools that don't speak MCP (Google Apps Script, n8n, cron jobs) can send mail through the same account.
## Tools
| Tool | What it does |
|---|---|
| `email_list_folders` | Browse the folder tree one level at a time |
| `email_search_messages` | Search by text, sender, subject, date range, unread |
| `email_get_message` | Read a message (text + HTML + attachment list) |
| `email_get_attachment` | Fetch an attachment (text files returned inline, up to 500 KB) |
| `email_send_message` | Send, and save a copy in the Sent folder (SMTP doesn't do that by itself) |
| `email_create_draft` | Save a **draft** instead of sending, for human review |
| `email_reply_message` | Reply in the same thread: `RE:`, `In-Reply-To`/`References`, history quoted Outlook-style |
| `email_create_reply_draft` | Same as reply, but as a draft, with optional attachments and an `.ics` meeting invite |
| `email_move_message` | Move messages between folders (fails clearly if the folder doesn't exist) |
| `calendar_list_calendars` / `calendar_list_events` | Read a CalDAV calendar (tested with Nextcloud) |
| `calendar_create_event` / `calendar_update_event` / `calendar_delete_event` | Manage events |
## What makes it different
- **Drafts first.** Agents can prepare replies in the right thread and leave them in Drafts for a human to approve. That's the safe default for anything client-facing.
- **Replies look like a human wrote them.** Same thread, the right recipients (replying to your *own* last message goes to the people you wrote to, never back to you), history quoted below like Outlook.
- **Consistent formatting and signature.** Plain-text bodies are turned into Outlook-like HTML (one `<p style="margin:0">` per line) and your HTML signature is added if the caller forgot it. Signature images can be embedded inline (`cid:`) so Outlook doesn't block them.
- **Meeting invites.** Reply drafts can carry an `.ics` invite with Accept/Decline buttons.
- **Sent folder kept in sync.** Messages sent over SMTP are appended to Sent via IMAP.
## Quick start
```bash
git clone https://github.com/kibakuryuuzaki/imap-mcp-server.git
cd imap-mcp-server
npm install
npm run build
cp .env.example .env # fill in your mailbox
```
### Local (stdio)
```json
{
"mcpServers": {
"email": {
"command": "node",
"args": ["/path/to/imap-mcp-server/dist/mcp-server/index.js"],
"env": {
"EMAIL_USER": "you@example.com",
"EMAIL_PASS": "your-password",
"IMAP_HOST": "imap.example.com",
"SMTP_HOST": "smtp.example.com"
}
}
}
}
```
To keep the password out of the JSON file, start it through [`env_launcher.mjs`](https://github.com/kibakuryuuzaki/mcp-stdio-kit) instead.
### Self-hosted (HTTP)
```bash
MCP_HTTP_PORT=8766 MCP_AUTH_TOKEN=long-random-string node dist/mcp-server/http-server.js
```
- MCP endpoint: `POST /mcp?token=<MCP_AUTH_TOKEN>`
- Health check: `GET /health`
- Put it behind HTTPS (Caddy, nginx, Cloudflare Tunnel) before exposing it.
### REST API (optional)
Enabled when `SEND_API_TOKEN` is set. Every call needs `Authorization: Bearer <SEND_API_TOKEN>`.
| Route | Body / query | Does |
|---|---|---|
| `POST /api/send` | `to`, `cc`, `bcc`, `subject`, `text`, `html`, `fromName`, `attachments[{filename, contentType, base64}]` | Sends (max 10 attachments, 20 MB) |
| `POST /api/draft` | same as `/api/send` | Saves a draft |
| `POST /api/reply-draft` | `uid`, `folder`, `text`, `html`, `replyAll`, `attachments`, `invite` | Draft reply in the thread |
| `GET /api/sent?since=YYYY-MM-DD` | — | Sent messages with recipients (read-only) |
## Configuration
| Variable | Required | Default | Meaning |
|---|---|---|---|
| `EMAIL_USER` / `EMAIL_PASS` | ✅ | — | Mailbox login |
| `IMAP_HOST` / `SMTP_HOST` | ✅ | — | Mail servers |
| `IMAP_PORT` / `SMTP_PORT` | | `993` / `465` | TLS ports |
| `SENT_FOLDER` / `DRAFTS_FOLDER` | | `Sent` / `Drafts` | Exact folder paths on your server (e.g. `INBOX.Sent`) |
| `FROM_NAME` | | — | Sender display name |
| `MAIL_LOCALE` / `MAIL_TIME_ZONE` | | `en-US` / `UTC` | How the "Sent:" date is written in quoted replies |
| `BODY_FONT` / `BODY_FONT_SIZE` | | `Aptos,Calibri,Arial,sans-serif` / `12pt` | Font for generated HTML bodies |
| `SIGNATURE_HTML_FILE` | | — | HTML signature added to drafts and replies |
| `SIGNATURE_MARKER` | | first 60 chars of the signature | Text that shows the signature is already in the body |
| `SIGNATURE_STRIP_LINES` | | — | Sign-off lines to drop before adding the signature, regex alternatives (e.g. `jane doe\|sales team`) |
| `SIGNATURE_IMG_URL_PREFIX` | | — | Signature images under this URL are embedded inline from `ASSETS_DIR` |
| `ASSETS_DIR` | | `./assets` | Folder with the signature images |
| `INVITE_UTC_OFFSET` | | `+00:00` | Offset for meeting-invite times |
| `CALDAV_URL` | for calendar tools | — | e.g. `https://cloud.example.com/remote.php/dav` |
| `CALDAV_USER` / `CALDAV_PASS` | | the email login | CalDAV login |
| `CALDAV_CALENDAR` | | first calendar | Default calendar name |
| `CALENDAR_TZID` / `CALENDAR_UTC_OFFSET` | | `UTC` / `+00:00` | Calendar time zone (fixed offset; DST zones not supported yet) |
| `MCP_HTTP_PORT` | for HTTP | — | Port of the HTTP server |
| `MCP_AUTH_TOKEN` | | — | Token required on `/mcp` (strongly recommended) |
| `SEND_API_TOKEN` | | — | Turns on the REST API |
## Safety notes
- Ask your agent to **draft, not send**, for anything that goes to customers.
- Use an app password if your provider supports one.
- Never commit `.env`; it's in `.gitignore`.
## License
[MIT](LICENSE)
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues