imap-mcp
by emahuet
README.md
# imap-mcp
[](https://github.com/emahuet/imap-mcp/actions/workflows/tests.yml)
An MCP server that gives Claude Code access to several IMAP mailboxes. Each mailbox has a short name in a config file, and every tool takes that name as its `account` argument.
The server reads, searches, flags and moves messages, manages folders, downloads attachments and writes drafts. It never sends mail.
## Requirements
- Python 3.10 or later
- An IMAP account for each mailbox. For Gmail, create an app password.
## Installation
```bash
git clone https://github.com/emahuet/imap-mcp.git
cd imap-mcp
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt
```
On Windows, the interpreter is `.venv\Scripts\python.exe`: use it in place of `.venv/bin/python` in the commands below.
## Configuration
Copy the example files:
```bash
cp config.yaml.example config.yaml
cp .env.example .env
```
In `config.yaml`, list your accounts under `accounts`. The key of each entry (`personal`, `work` in the example) is the name you will use in Claude Code. Each account gives its host, port, username and the name of the environment variable that holds its password (`password_env`).
Put the passwords in `.env`:
```
IMAP_PERSONAL_PASSWORD=...
IMAP_WORK_PASSWORD=...
```
Both files are read from the directory of `mcp_server.py`, whatever the current directory is when Claude Code starts the server. They are listed in `.gitignore`. A password variable already set in the shell works too.
To check the connections:
```bash
.venv/bin/python scripts/check_connection.py
```
## Register in Claude Code
Use absolute paths to the virtual environment and to `mcp_server.py`:
```bash
claude mcp add imap -s user -- /path/to/imap-mcp/.venv/bin/python /path/to/imap-mcp/mcp_server.py
```
`-s user` makes the server available in every project. Run `claude mcp list` to see it, then start a new Claude Code session.
## Tools
- `list_accounts()`: configured accounts.
- `list_folders(account)`: folders of an account.
- `search_emails(account, folder, limit, headers_only, unseen, flagged, answered, from_, to, subject, body, since, before, mark_seen, text, query)`: search with filters combined by AND. `text` is a full-text search on the subject, body and headers (each word must be present, quotes are ignored). `query` is an alias of `text`. Matching depends on the server: Gmail only finds whole words. `account="*"` searches every account, with `limit` applied per account. Messages are not marked as read unless `mark_seen=True`.
- `get_email(account, uid, folder, include_html, max_chars)`: content of a message. When the message has no plain text part, `text` is extracted from the HTML. The raw HTML is returned only with `include_html=True`. Calendar invitations (Teams, Outlook, Google Calendar) are parsed into a `calendar` field.
- `set_flags(account, uids, flags, value, folder)`: set or clear flags. Aliases are `flagged`, `seen`, `answered` and `draft`. `\Deleted` is refused.
- `move_emails(account, uids, dest_folder, source_folder)`: move messages, for example to `Trash`.
- `permanent_delete_emails(account, uids, folder)`: permanent delete, allowed only in a folder flagged `\Trash` by the server.
- `create_folder(account, folder)` and `delete_folder(account, folder)`: a folder is deleted only when it is empty and has no subfolder.
- `attachments_meta(account, uids, folder)` and `download_attachments(account, uid, save_dir, folder)`: attachment metadata and download.
- `save_draft(account, to, subject, body, ...)`: write a draft in the Drafts folder, with HTML body, attachments and threading headers (`in_reply_to`, `references`).
- `unread_count(account, folder)`: number of unread messages.
Generic folder names (`Sent`, `Drafts`, `Trash`, `Junk`, `Archive`) are resolved through the server's SPECIAL-USE attributes when the account uses localized names.
## Scripts
Run them from the repository root:
- `scripts/check_connection.py [account ...]`: log in to each account and print a short summary.
- `scripts/move_once.py <account> <dest_folder> <uid> ...`: move messages from INBOX.
- `scripts/move_folder.py <account> <source_folder> <dest_folder>`: move the whole content of a folder.
## Tests
```bash
.venv/bin/python -m unittest discover -s tests
```
The tests use no network and no config file.
## Security
- Passwords come from environment variables only.
- Connections use SSL by default (port 993).
- Messages are not stored or cached on disk, except attachments you download.
## License
MIT, see [LICENSE](LICENSE).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues