imap-mcp-server
AGENTS.md141 starsChanged 47 days agoMITView on GitHub
Cross-agent project guide for imap-mcp-server — a Model Context Protocol (MCP) server that gives AI assistants (Claude, ChatGPT, Cursor, …) access to IMAP/SMTP mailboxes. This file is the shared source of truth for any agent or contributor working in this repository. - Entry point — src/index.ts boots an McpServer (MCP SDK) over stdio and registers all tools via src/tools/index.ts. - Services (src/services/): - ImapService — IMAP protocol via imapflow, with connection pooling, folder operations, search, fetch, move/delete, append (Sent/Drafts). -…
- Reads credentials
- Installs packages
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about code style.
Of the AGENTS.md files we read, the 35,266 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repositoryOne other file it gives its agents.
- CLAUDE.mdCLAUDE.md
The file
We split this AGENTS.md into the rules it gives an agent
4 rules. About 1 is common, 1 is distinctive, and 2 are very specific to this project.
≈1
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- could be automatic: a setting could enforce it
- Distinctive: line 45
All tools return JSON-formatted text content.
- Very specific: line 60
Always run npm run build and npm test before committing changes that touch src/.
- Very specific: line 79
Tool schema changes require docs + tests: Do not rename existing tools or change their input/output shape without (a) updating the tool …
- Common: line 83
Credentials stay local: Do not add telemetry, analytics, crash reporting, or any third-party network calls.
≈ “Common” is an estimate: at least 1 in 5 of the 45,344 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
AGENTS.md
Cross-agent project guide for imap-mcp-server — a Model Context Protocol (MCP) server that gives AI assistants (Claude, ChatGPT, Cursor, …) access to IMAP/SMTP mailboxes. This file is the shared source of truth for any agent or contributor working in this repository.
Architecture
- Entry point —
src/index.tsboots anMcpServer(MCP SDK) over stdio and registers all tools viasrc/tools/index.ts. - Services (
src/services/):ImapService— IMAP protocol viaimapflow, with connection pooling, folder operations, search, fetch, move/delete, append (Sent/Drafts).SmtpService— outbound mail vianodemailer; composes raw MIME and sends.AccountManager— account CRUD with AES-256-CBC encrypted credential storage at~/.imap-mcp/accounts.json(key at~/.imap-mcp/.key). Credentials can be overridden at read time via environment variables keyed by the account's normalized name (uppercase, non-alphanumeric →_):IMAP_MCP_ACCOUNT_<NAME>_IMAP_USERNAME/_IMAP_PASSWORDandIMAP_MCP_ACCOUNT_<NAME>_SMTP_USERNAME/_SMTP_PASSWORD. Overrides are in-memory only (never persisted) and apply only to existing accounts. The variables are consumed at startup (constructor): captured into an AES-256-encrypted in-memory cache and deleted fromprocess.envso the plaintext secret does not linger in the environment. An empty credential is the marker for "env-managed";assertCredentialsResolved(src/utils/env-credentials.ts) is called fromImapService.connectandSmtpService.createTransporterand fails with the missing variable's name instead of dialing out blank. KeepenvVarName()in sync with its copy inpublic/js/app.js(the wizard is a static asset and cannot import it) —tests/env-credentials.test.tsasserts the two agree.SpamService— disposable/known-spam domain detection.
- Tools (
src/tools/), grouped by area:account-tools.ts— add / update / list / remove / connect / disconnect / test.email-tools.ts— search, get, latest, send, reply, forward, save draft, mark read/unread, delete, bulk delete, move, attachments, upload, threads.folder-tools.ts— list, status, create, unread counts.spam-tools.ts— spam analysis, domain stats, allow/deny lists.
- Web setup wizard —
src/web/server.ts(Express) servespublic/for account onboarding (npm run setup/imap-setup). Binds127.0.0.1and rejects non-loopbackHost/Origin. With--host/IMAP_MCP_BINDbeyond loopback, every request needs the per-start access token instead (cookie orAuthorization: Bearer); keep both paths covered when touching middleware. - Types —
src/types/index.ts. - All tools return JSON-formatted text content; errors are returned as structured JSON where practical rather than thrown for caller-facing failures.
Build / Test commands
npm install # install dependencies
npm run build # bundle to dist/ via esbuild (build.mjs)
npm test # run the vitest suite (run mode)
npm run test:watch # vitest in watch mode
npm run lint # tsc --noEmit type-check
npm run dev # run the server from source (tsx watch)
npm run setup # launch the web setup wizardAlways run npm run build and npm test before committing changes that
touch src/. Keep the suite green (currently 451 tests).
Note:
npm run lint(tsc --noEmit) is memory-hungry on this project — the MCP SDK'sregisterToolgenerics are deep enough to surface a pre-existingTS2589and can OOM on low-RAM machines. Run it with a larger heap if needed (node --max-old-space-size=8192 ./node_modules/typescript/bin/tsc --noEmit).
Security rules (must follow)
- Never log secrets. Passwords, encryption keys,
accounts.jsoncontents, raw auth tokens, and full message bodies must not be written to stdout/stderr or to disk outside the user's mailbox/download directories. When adding logs, log identifiers (account id, folder, uid), not credentials. - No destructive mail operations without explicit guard logic. Deletes,
bulk deletes, and moves must be driven by explicit caller input. Bulk/criteria
deletion must keep its
dryRunpath and require concrete criteria — never delete a whole folder by default, and never widen a delete beyond what the caller specified. - Tool schema changes require docs + tests. Do not rename existing tools or
change their input/output shape without (a) updating the tool
description, (b) updatingREADME.md, and (c) adding/adjusting tests. Prefer additive, backward-compatible changes (new optional fields) over breaking ones. - Credentials stay local. Do not add telemetry, analytics, crash reporting, or any third-party network calls. The only outbound connections are to the user's own IMAP/SMTP servers.
- Validate and sanitize file paths for attachment upload/download (already
done via
path.basename); keep writes confined to the configured directories.
Conventions
- TypeScript, ESM (
"type": "module"), Node ≥ 22.12 (declared inpackage.jsonengines.node; CI runs 22.x and 24.x).- npm checks a dependency's
enginesagainst the Node doing the install, not against the floor we declare — so a dependency needing a newer Node installs silently and only breaks on a user's older runtime. This is how #108 happened.tests/node-engines.test.tswalks the runtime dependency tree and fails if any package needs more than we advertise. When it fires, either raise the floor (CI matrix, AGENTS.md and README with it) or pin the package back via npmoverrides.
- npm checks a dependency's
- Tool names are stable public API:
imap_*. Do not rename without a strong reason and a migration note. - Zod schemas describe every tool input; every field gets a
.describe()that tells an LLM when and how to use it. - Match the surrounding code style; keep error handling and connection cleanup consistent with existing tools.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

