agentleFS
nikolausmnikolausm/imap-mcp-server/AGENTS.md

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.

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
  1. Distinctive:

    All tools return JSON-formatted text content.

    line 45
  2. Very specific:

    Always run npm run build and npm test before committing changes that touch src/.

    line 60
  3. Very specific:

    Tool schema changes require docs + tests: Do not rename existing tools or change their input/output shape without (a) updating the tool …

    line 79
  4. Common:

    Credentials stay local: Do not add telemetry, analytics, crash reporting, or any third-party network calls.

    line 83

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
AGENTS.md

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.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).
    • SmtpService — outbound mail via nodemailer; 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_PASSWORD and IMAP_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 from process.env so 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 from ImapService.connect and SmtpService.createTransporter and fails with the missing variable's name instead of dialing out blank. Keep envVarName() in sync with its copy in public/js/app.js (the wizard is a static asset and cannot import it) — tests/env-credentials.test.ts asserts 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) serves public/ for account onboarding (npm run setup / imap-setup). Binds 127.0.0.1 and rejects non-loopback Host/Origin. With --host / IMAP_MCP_BIND beyond loopback, every request needs the per-start access token instead (cookie or Authorization: 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 wizard

Always 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's registerTool generics are deep enough to surface a pre-existing TS2589 and 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)

  1. Never log secrets. Passwords, encryption keys, accounts.json contents, 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.
  2. 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 dryRun path and require concrete criteria — never delete a whole folder by default, and never widen a delete beyond what the caller specified.
  3. Tool schema changes require docs + tests. Do not rename existing tools or change their input/output shape without (a) updating the tool description, (b) updating README.md, and (c) adding/adjusting tests. Prefer additive, backward-compatible changes (new optional fields) over breaking ones.
  4. 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.
  5. 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 in package.json engines.node; CI runs 22.x and 24.x).
    • npm checks a dependency's engines against 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.ts walks 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 npm overrides.
  • 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.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.