email-engine

Free, open-source MCP server and email engine you host yourself. Connects AI agents to any mailbox (IMAP, JMAP, Gmail, Microsoft 365) behind scoped tokens, an approval queue and an undo journal.

Documentation

email-engine

Let AI agents work your inbox. Keep the final say.

Free and open source under Apache-2.0. Self-hosted. No paid tier, no licence key, no usage limits.

Install · Connect your agent · Timeline and roadmap · Help build it

email-engine is an open-source email engine and MCP server that you host yourself. It connects an AI agent to the mailbox you already have, on any provider, and puts a gate in between: the agent gets a scoped token, its risky actions wait for your approval, and everything it does is journaled so it can be undone.

flowchart LR
    A["<b>AI agents</b><br/>MCP clients<br/>Your own code<br/>HTTP and SSE<br/>Webhooks"]
    E["<b>The engine</b><br/>Self-hosted<br/>Scoped tokens<br/>Approval queue<br/>Undo journal<br/>Event log"]
    M["<b>Your mail</b><br/>IMAP<br/>JMAP<br/>Gmail<br/>Microsoft 365<br/>Forwarded mail"]
    A <-->|"events out,<br/>requests in"| E
    E <-->|"changes in,<br/>actions out"| M

Status: 0.2.0 beta. A suite of 27 behaviour scenarios runs on 7 test targets, including real Dovecot and Stalwart servers, with no failures (not every scenario applies to every target). Gmail and Microsoft 365 are implemented over IMAP with OAuth but have not been run against the live services yet, and no run has lasted longer than an hour. Read SECURITY.md before you expose it to anything. What has been done so far and what comes next is in the timeline and roadmap.

Why agents need a gate

An agent that reads email reads text written by strangers. One crafted message can tell it to forward your invoices or empty your inbox, and no prompt reliably prevents that. The dependable fix is to limit what the agent is able to do, whatever it has been told. That is the job of this engine.

The ruleWhat it means
Send and delete always waitFrom an agent's token they go to an approval queue, whatever the token's settings. Only the admin token skips the queue.
No self-approvalA propose-only token can never approve or undo. No scoped token can approve its own proposal.
Least privilege per agentEach token names the accounts, folders, events and actions one agent may touch.
A journal and an undoEvery action is recorded. Moves, flags, tags, deletes to trash, drafts and timers can be reverted.
Secrets maskedA token can have one-time codes, card numbers and IBANs masked in message bodies and snippets.
No model insideThe engine is deterministic. It never runs a language model, so there is nothing in it to talk into anything.

This limits the damage a misled agent can do. It does not make prompt injection impossible: an agent can still read what its token allows, and moves, flags and drafts run at once unless the token is propose-only. SECURITY.md lists what is protected and what is not.

How an agent works with it

The agent never talks to the mail server. It reads events, asks the engine for an action, and the engine decides whether that happens now or waits for a person.

sequenceDiagram
    autonumber
    participant M as Mail server
    participant E as The engine
    participant A as AI agent
    participant H as You

    M-->>E: new mail arrives
    E-->>A: message.received
    A->>E: move it to Invoices
    Note over E: The agent's token is propose-only,<br/>so nothing touches the mailbox yet
    E-->>H: action.updated (proposed)
    H->>E: approve
    E->>M: move the message
    E-->>A: action.updated (executed)
    H->>E: undo
    E->>M: move it back

"You" here means an API call made with the admin token, or with a separate token that names approve. There is no approval screen yet.

What your agent gets

  • 19 MCP tools. Search, read messages, threads and attachments, list folders and tags, move, flag, tag, draft, send, delete, schedule timers, and the approve, reject and undo controls.
  • One event stream for every provider. Seven event types, numbered in order. Read them by polling, Server-Sent Events or webhook, and replay from any number after being offline.
  • The same actions everywhere. IMAP, JMAP, the Gmail API and Microsoft Graph sit behind one interface, so the agent's code does not change with the mailbox.
  • Honest freshness. Every read carries as_of, the account's last successful sync, so the agent knows how stale its view is.
  • Machine-readable contracts. JSON Schemas for events, actions, tokens and account configs are in spec/.

An event is plain JSON. This example is shortened and its values are illustrative:

{
  "cursor": 42,
  "type": "message.received",
  "account": "acc_…",
  "occurred_at": "2026-10-06T20:35:21.597Z",
  "payload": {
    "message_id": "msg_…",
    "thread_id": "thr_…",
    "subject": "Invoice 12",
    "snippet": "Please find attached…",
    "trust": "known",
    "has_attachments": true
  }
}

Measured, not promised

  • 27 scenarios, 7 targets, 0 failures. The same behaviour suite runs against Dovecot, Stalwart (IMAP, IMAP with OAuth, JMAP), the engine's own SMTP listener, and mock Gmail and Graph servers. All 27 run on Stalwart; a target skips a scenario only where the capability does not exist. It checks exact event counts, an offline catch-up, a 25-message flag storm, and the token and approval rules. See the conformance report.
  • New mail reaches the event log in about 0.2 seconds on Stalwart and 0.6 seconds on Dovecot. Measured on local test servers in the latest run.
  • 28 unit tests, including the approval and token rules above.

Install

You need Node 22 or newer. One command starts the engine, with nothing to clone or build:

npx -y -p email-engine-mcp email-engine

It stays in the foreground and is ready when it logs listening on port 8080. Its database goes in ./data under the folder you start it from. On start it prints a freshly generated admin token; to keep the same one across restarts, set ENGINE_TOKEN to a long random string of your own. The admin token is yours alone: never give it to an agent. Then connect a mailbox (Providers) and connect your agent.

Before you run it on a shared network: version 0.2.0 listens on every network interface, so other machines on the same network can reach port 8080, and /health shows the connected addresses without a login. Run it behind a firewall or on a machine you trust. The next release listens on localhost only by default.

The commands on this page are written for bash; on Windows run them in Git Bash or WSL.

Try it with the bundled test servers

To see it work before pointing it at real mail, clone the repository and use the test mail servers it ships with. This needs Docker. Followed word for word from a fresh clone, on a machine that already had the Docker images, these steps reached the first event in under a minute.

git clone https://github.com/Kadri-cloud/email-engine.git
cd email-engine
npm install
npm run build

docker compose up -d              # local test mail servers: Dovecot and Stalwart
node test/seed.mjs alice 6        # six test messages on Dovecot for user "alice", password "pass"

ENGINE_TOKEN=test node dist/index.js    # stays in the foreground; ready when it logs "listening"

In a second terminal, connect the mailbox and read the log:

curl -X POST http://127.0.0.1:8080/accounts \
  -H "authorization: Bearer test" -H "content-type: application/json" \
  -d '{"provider":"imap","address":"alice@example.com",
       "imap":{"host":"127.0.0.1","port":993,"secure":true,"user":"alice","pass":"pass","insecure_tls":true}}'

node test/events.mjs 0        # pretty-print the event log
node test/smoke.mjs           # end-to-end check, about 45 seconds: push, actions, undo, proposals, timers, SSE, tokens

To connect a real mailbox, see Providers.

Connect your agent

The MCP server speaks stdio, the transport most MCP clients launch. So far it has been driven by the official MCP SDK client, which listed all 19 tools, searched, and got a proposal back for a move; it has not yet been tried in each desktop app. It holds one scoped token, and whatever that token may see and do is exactly what the agent may see and do.

First create a token for the agent. A propose-only token can never approve or undo anything, and no scoped token can approve its own proposal, so the agent cannot let itself through.

curl -X POST http://127.0.0.1:8080/tokens -H "authorization: Bearer test" -H "content-type: application/json" -d '{
  "name": "my-agent",
  "actions": ["search", "get_message", "get_thread", "get_action", "propose", "move", "set_flags", "tag", "create_draft"],
  "redact": ["otp", "card"], "mode": "propose_only"
}'

Then add this to your MCP client's configuration. Use the secret field from the response, not the id, and keep the engine running:

{
  "mcpServers": {
    "email-engine": {
      "command": "npx",
      "args": ["-y", "email-engine-mcp"],
      "env": { "ENGINE_URL": "http://127.0.0.1:8080", "ENGINE_MCP_TOKEN": "tok_..." }
    }
  }
}

If your client on Windows cannot start npx directly, use "command": "cmd" with "args": ["/c", "npx", "-y", "email-engine-mcp"]. The first start downloads the package and takes about 10 to 15 seconds; later starts take about 2.

Every tool carries a safety label that MCP clients can act on: eight are marked read-only, and send_mail, delete_message and approve_action are marked destructive. The full tool list is in the Reference.

For AI agents reading this

If you are an AI agent setting this up for someone, these are the facts you need.

  • What it is. The npm package email-engine-mcp (Node 22+) holds two commands: email-engine, a service with an HTTP API on port 8080, and email-engine-mcp, a stdio MCP server that calls that API.
  • Install and run. Start the engine with ENGINE_TOKEN=<admin secret> npx -y -p email-engine-mcp email-engine; it is ready when it logs listening. The MCP server is npx -y email-engine-mcp, with ENGINE_URL and ENGINE_MCP_TOKEN in its environment.
  • Take the least access that does the job. Create a scoped token with POST /tokens and "mode": "propose_only", and pass it to the MCP server as ENGINE_MCP_TOKEN. The MCP server refuses to start on the admin token.
  • Expect proposals. With that token every write comes back with "status": "proposed". Tell the person what you proposed and wait for them to approve it. Do not look for a way around the queue.
  • Contracts. JSON Schemas for events, actions, tokens and account configs are in spec/. Routes and tools are in the Reference.
  • Boundaries. Connect only a mailbox the person has named, and never run the test suite against a real mailbox.

A free alternative to EmailEngine and Nylas

If you are looking for an email API you can run yourself without a licence fee, this is one. Here is how it sits next to the two best-known options, taken from their own sites on 1 December 2026.

This projectEmailEngineNylas
CostFreePaid licence key after a 14-day trial, listed at $1,450 a yearFree plan for 5 accounts, paid plans from $15 a month
LicenceApache-2.0, open sourceSource-available, proprietaryHosted service
Where it runsYour machineYour machineNylas's cloud
MaturityBeta, 0.1.0Established productEstablished product

What this project adds for AI agents is the gate described above: a scoped token per agent, an approval queue, and a journal with undo.

Two other kinds of product sit nearby. Vendor MCP servers for Gmail or Microsoft 365 cover one provider each and run in the vendor's cloud. Hosted agent-inbox services give the agent a new address of its own. This engine works with the mailbox you already have, on whichever provider hosts it.

This project is independent and is not affiliated with EmailEngine, Postal Systems or Nylas.

What it has been tested against

ServerHow it connectsResult
Dovecot 2.4IMAPEvery scenario that applies passes. Sending is not exercised on this target.
Stalwart 0.13.3IMAP, IMAP with OAuth, JMAPEvery scenario passes, including sending to another mailbox.
The engine's own SMTP listenerForwarded mailEvery scenario that applies passes. Read-only by nature.
Gmail API, Microsoft GraphMock servers onlyThe sync logic passes against mocks. No live run yet.
Gmail, Microsoft 365, other hosted providersIMAP with OAuthNot yet run. This is the most useful thing you can contribute.

Documentation

  • Reference: HTTP routes, events, actions, scoped tokens, the MCP tools, configuration.
  • Providers: JMAP, forwarded mail, OAuth for Gmail and Microsoft 365, the Gmail API and Graph, how credentials are stored.
  • Testing and conformance: the scenario suite, running it against your own provider, the code layout, and the mail-server lessons learned so far.
  • spec/: JSON Schemas for events, actions, tokens and capabilities.
  • SECURITY.md: what is protected and what is not.
  • AGENTS.md: how a coding agent should build, test and change this repository.

The road so far, and where it goes next

The aim is simple to say: any AI agent, any mailbox, and a person who keeps the final say. Email is where a great deal of real work still happens, and agents are about to do much of it. This project wants to be the free, open gate they pass through.

timeline
    title From first commit to 1.0
    section Done
        October 2026 : First working engine : MCP server and scoped tokens : Five mail adapters : Beta 0.1.0
        December 2026 : Audit and security fixes : Tool safety labels : On npm as 0.2.0
    section Ahead
        Next : Localhost by default : MCP Registry listing : Crash safety and automatic tests
        Then : Live Gmail and Microsoft 365 runs : Approval screen : Docker image
        1.0 : Stable contract : Outside security review

Done so far

WhenWhat
6 October 2026The first working engine: IMAP sync with push, the ordered event log, journaled actions with undo, Server-Sent Events.
Scoped tokens, redaction and plug-in events, then the MCP server.
JMAP, inbound SMTP, Gmail API and Microsoft Graph adapters behind one interface, and OAuth login over IMAP.
The conformance suite: the same scenarios against every server, with a public report.
Encrypted credentials, attachments and sending. Public beta 0.1.0.
1 December 2026A full audit of the code. It found that an agent could approve its own actions and that sending could add a hidden recipient. Both were fixed, with 17 new tests, before anything was published to npm.
The README rebuilt around diagrams, and the project positioned for AI agents.
Safety labels on all 19 MCP tools.
Version 0.2.0 published to npm as email-engine-mcp, with a one-command install.

Where it goes next

This is an order, not a set of dates. "Open" means nobody is on it and a pull request is welcome; open an issue first so two people do not build the same thing.

StepWhatOpen to contributors
0.2.1 Safe defaultsListen on localhost only unless told otherwise. Keep account details out of the health check. List the server in the official MCP Registry.Maintainer first
0.3 Runs unattendedTimeouts on every network call, recovery of interrupted actions, a first sync that can resume. Automatic tests on every pull request. Live runs against Gmail and Microsoft 365. A soak run measured in days.Live provider runs, the soak run
0.4 Easier to live withAn approval screen, so approving is a click and not an API call. A command to add mailboxes and create tokens. A published Docker image. A body and attachment cache. Webhook signatures and a retry queue.The cache, webhooks, the Docker image
0.5 Deeper agent safetyRecipient allow-lists per token. A provenance mark on everything handed to an agent. Redaction for subjects and attachments. Push from Gmail and Microsoft Graph in place of polling. Graph child folders. STARTTLS on the inbound listener.Push, child folders, STARTTLS
1.0 StableA frozen event and action contract, an outside security review, and a compatibility report that covers the major providers.Provider reports from everyone

Help build it

This is a young project. What makes it real is more mailboxes and more eyes.

  • Star the repository if you want agents and email to meet safely. It is how the next person finds it.
  • Run the suite against your mail provider and open an issue with the report. It needs a throwaway mailbox and one command, and it is the most useful contribution there is. How to run it.
  • Pick up anything marked open in the table above. Each item stands on its own.
  • Bring your agent. AGENTS.md tells a coding agent how to work in this repository, and CONTRIBUTING.md covers the ground rules for everyone.

Questions and ideas are welcome in Discussions.

Licence

Apache-2.0. No contributor licence agreement: you keep your copyright and license your contribution under Apache-2.0 by submitting it.