No description
  • Python 97.6%
  • Shell 1.5%
  • Makefile 0.9%
Find a file
2026-11-03 02:27:52 +00:00
.woodpecker Add Woodpecker validation lane 2026-10-30 17:56:29 +11:00
docs Add optional client-certificate authentication per account 2026-10-30 18:31:48 +11:00
scripts Add GreenMail integration harness and tests 2026-10-30 17:12:36 +11:00
src/imap_blade_mcp Release 0.1.2: keep forwarded messages in trim_quoted 2026-11-03 13:25:15 +11:00
tests Release 0.1.2: keep forwarded messages in trim_quoted 2026-11-03 13:25:15 +11:00
.gitignore Add project scaffold, config model and error types 2026-10-30 17:12:35 +11:00
.python-version Add project scaffold, config model and error types 2026-10-30 17:12:35 +11:00
CHANGELOG.md Release 0.1.2: keep forwarded messages in trim_quoted 2026-11-03 13:25:15 +11:00
CLAUDE.md Document the read surface, security model and Linux deployment 2026-10-30 17:12:36 +11:00
LICENSE Bootstrap repository with README and MIT licence 2026-10-30 16:32:52 +11:00
Makefile Add project scaffold, config model and error types 2026-10-30 17:12:35 +11:00
pyproject.toml Release 0.1.2: keep forwarded messages in trim_quoted 2026-11-03 13:25:15 +11:00
README.md Add optional client-certificate authentication per account 2026-10-30 18:31:48 +11:00
SECURITY.md Add optional client-certificate authentication per account 2026-10-30 18:31:48 +11:00
uv.lock Release 0.1.2: keep forwarded messages in trim_quoted 2026-11-03 13:25:15 +11:00

IMAP Blade MCP

Read-only IMAP access for agents over the Stallari email-v1 contract. Eleven tools, stdio only, TLS always, BODY.PEEK everywhere, and output shaped for a model's context window rather than for a human mail client.

Status: v0.1.0, planning-stage build for the Linux grokbot hosts. Not for third-party use until the marketplace ships. Configuration keys, output shapes and the message id format may still change before 1.0.

Built on FastMCP 3.x, imapclient 4.x and stallari-mcp-helpers for the _meta audit envelope.

Why IMAP Blade?

IMAP is the only API that iCloud Mail, Migadu, Purelymail, self-hosted Dovecot and Proton (via Bridge) have in common. This blade gives every one of them an email-v1 read surface that runs headless on Linux with no Full Disk Access, no Mail.app and no vendor SDK.

It is a paddle, not a Swiss army knife. The closest community alternative bundles SMTP send, five mutation classes granted by default, a plaintext mode and three network transports. This blade removes all of that on purpose.

Comparison (v0.1 read-only)

Capability imap-blade-mcp v0.1 Wh1isper/mcp-email-server
Protocol scope IMAP only. No SMTP, no send path anywhere in the repo IMAP plus SMTP send
Write surface None. No write tools, no gated stubs, no STORE/COPY/MOVE/APPEND code Five mutation classes, granted by default
Read side effects EXAMINE plus BODY.PEEK only. \Seen is never set, proven against GreenMail PEEK
Transport security Implicit TLS or required STARTTLS, TLS 1.2+, hostname check, CERT_REQUIRED, optional private ca_file, optional client certificate. No plaintext mode, no verify=false Plaintext / no-TLS mode allowed
Credential handling Per-account credential file, mode 0600, read at connect time only, scrubbed from every log and error path. Never env, never CLI, never a tool argument Keyring or plaintext TOML
Connection target Config only. No tool takes a host, port, path, URL or credential Config
Transports stdio only. No HTTP, SSE or socket code stdio, SSE, Streamable HTTP, admin UI
Untrusted content Bodies, snippets and attachments wrapped in [untrusted-email-content] markers; C0/C1/ANSI/bidi controls stripped; HTML reduced to text with hidden elements dropped Raw
Token efficiency Pipe-delimited rows, fields=, cursor paging, quote and signature trim, max_chars caps, _meta audit tail Body truncation plus offset paging
Parser bounds 32 nesting levels, 100 MB message, 25 MB part. BODYSTRUCTURE first, then per-part partial fetches Whole-message fetch
Tool surface 11 tools, fixed; expose allowlist narrows it Open
Multi-account Yes, one account per call (domain_scope: single) Yes
Stallari integration email-v1 read ops, _meta envelope, DD-338 determinism test None
Runtime Python 3.12+, uv Python

Requirements

  • Linux or macOS
  • Python 3.12 or newer
  • uv
  • An IMAP account reachable over implicit TLS (993) or STARTTLS (143), plus an app password

Quick start

git clone https://git.groupthink.asia/dev/imap-blade-mcp
cd imap-blade-mcp
make install

mkdir -p -m 0700 ~/.config/imap-blade-mcp/credentials
printf '%s\n' 'app-password-here' > ~/.config/imap-blade-mcp/credentials/work
chmod 0600 ~/.config/imap-blade-mcp/credentials/work
$EDITOR ~/.config/imap-blade-mcp/config.toml   # see below

uv run imap-blade-mcp --check     # validate config, TLS, login and capabilities
uv run imap-blade-mcp             # stdio MCP server

Linux deployment for the grokbot hosts is in docs/deploy-linux.md.

Configuration

The config file is resolved in this order: --config PATH, then $IMAP_BLADE_CONFIG, then $XDG_CONFIG_HOME/imap-blade-mcp/config.toml, then ~/.config/imap-blade-mcp/config.toml. It must be a regular file owned by the running user and not group- or world-writable. Unknown keys are rejected.

# Optional. Tools not listed are not registered at all. Default: all 11.
expose = ["imap_accounts", "imap_info", "mail_mailboxes", "mail_search", "mail_read"]

[[accounts]]
id = "work"                       # ^[a-z0-9][a-z0-9_-]{0,63}$; the value every tool's `account` takes
display_name = "Work (Migadu)"
host = "imap.migadu.com"
port = 993                        # default 993 for implicit, 143 for starttls
tls = "implicit"                  # "implicit" (default) or "starttls"; nothing else exists
username = "bot@example.org"
credential_file = "/home/bot/.config/imap-blade-mcp/credentials/work"   # absolute, 0600, one line
# ca_file = "/etc/ssl/private-ca.pem"   # optional extra trust anchor (private CA, Proton Bridge, relay CA)
# client_cert_file = "/home/bot/.config/imap-blade-mcp/tls/client.pem"   # optional mTLS client certificate
# client_key_file = "/home/bot/.config/imap-blade-mcp/tls/client.key"    # optional; 0600; defaults to the cert file (combined PEM)
# mailboxes = ["INBOX", "Archive"]      # optional allowlist; everything else is invisible

client_cert_file and client_key_file enable mutual TLS for a server that requires a client certificate, such as the companion imap-blade-relay on a host without IMAP egress. The key file is checked like a credential (regular file, owned by the running user, mode 0600) before any socket is opened. Set host and port to the relay and ca_file to the CA that signed the relay's server certificate.

The credential file holds the app password on one line. One trailing newline is stripped. It is read at connect time only, never at startup, and never from the environment, the command line or a tool argument.

Logging goes to stderr as JSON lines. IMAP_BLADE_LOG_LEVEL (default INFO) is the only environment variable the server reads. Logs carry account ids, tool names, mailbox roles, counts, durations and response codes. They never carry subjects, addresses, bodies or filenames.

--check

imap-blade-mcp --check [--config PATH] [--account ID] validates the config and file modes, connects, logs in and prints a one-line summary per account. It prints no message content. Exit codes:

Code Meaning
0 OK
2 config error (missing file, bad key, wrong mode or owner)
3 TLS failure (handshake, certificate, hostname, STARTTLS not offered)
4 authentication failure
5 capability shortfall (no IMAP4rev1/rev2)

Tools (11)

Every tool takes account (a config id) where it touches a mailbox. No tool takes a host, port, path, URL or credential. Every tool carries the MCP annotations readOnlyHint=true, destructiveHint=false, idempotentHint=true, openWorldHint=true and ends its output with a _meta audit line.

Tool Parameters Returns
mail_search account, mailbox, query, since, before, flags, limit, cursor, fields Rows id | date | from | subject | flags | size, newest first, footer next_cursor=… more=N
mail_read account, message_id, max_chars, include_quoted From/To/Cc/Date/Subject/Message-ID, attachment list, wrapped text body
mail_mailboxes account name | role | messages | unseen, inbox first
mail_snippets account, mailbox, query, chars, limit, cursor, fields Search rows plus a partial-fetch preview per row
mail_threads account, message_id or mailbox+query, limit, fields Indented thread tree
mail_flag_bitmask_get account, message_id, namespace Keywords and namespace_bits (email-v1 flag_bitmask_get)
mail_state account, mailbox UIDVALIDITY, UIDNEXT, HIGHESTMODSEQ and a state token
mail_changes account, state, mailbox, limit, cursor, fields state=… partial=… new=N updated=M then rows
imap_accounts none id | display_name | tls | status
imap_info account TLS mode, extensions, namespaces, quota, degrade paths
mail_attachment account, message_id, part, max_chars Text parts inline and wrapped; binary parts as metadata plus SHA-256

Search grammar

from: to: subject: keyword: since:YYYY-MM-DD before:YYYY-MM-DD unseen seen flagged unflagged answered draft deleted, and free text (sent as TEXT). Every literal is quoted or sent as an IMAP literal, so OR, NOT, ALL and parentheses in user text can never become search keys. Gmail X-GM-RAW is used only when the server advertises X-GM-EXT-1.

Message ids

A message id is base64url of <account>/<mailbox>/<uidvalidity>/<uid>. If the mailbox's UIDVALIDITY changes, the id is rejected with stale_id. It is never silently resolved to a bare UID.

Errors

Errors come back as one line: Error: code=<code> retryable=<bool> <message>. Codes: config_error, tls_error, auth_error, connection_error, not_found, stale_id, invalid_argument, limit, unavailable, authenticationfailed, parse_bounds, unsupported, server_error. RFC 5530 response codes ([LIMIT], [UNAVAILABLE], [AUTHENTICATIONFAILED]) map to the matching code with retryable set. Credentials are scrubbed from every error path.

Degrade paths

The server records which extensions are missing and reports them in imap_info and --check:

Missing Behaviour
NAMESPACE One personal namespace with an empty prefix
SPECIAL-USE Roles inferred from mailbox names
LIST-STATUS One STATUS per mailbox
ESEARCH Match counts from the full UID list
CONDSTORE mail_changes reports new messages only, partial=true
THREAD=REFERENCES Threads built client-side from References headers
QUOTA Quota not reported

Token budget

The serialised tools/list response with all 11 tools is 13004 bytes (mcp.types.ListToolsResult, by_alias=True, exclude_none=True). A test fails if it grows by more than 10%. Tool descriptions stay at or under 400 characters and parameter descriptions at or under 120.

Development

make install          # uv sync with dev + test groups
make check            # lint, format-check, type-check (mypy --strict), unit tests, pip-audit
make test             # unit tests: no network, no Docker
make test-integration # GreenMail in Podman/Docker over IMAPS with the test CA

Unit tests exercise TLS ordering, certificate failure, credential scrubbing, schema guards, parser bounds and determinism against scripted loopback servers and a fake session. Integration tests run against a digest-pinned GreenMail container with a preloaded corpus (multipart, HTML-only, hidden-text HTML, bidi subject, 40-level nesting, 1.5 MB attachment, non-ASCII mailbox name).

See SECURITY.md for the threat model and CHANGELOG.md for history.

Licence

MIT. See LICENSE.