Skip to main content
Glama
jmap-mcp-worker

jmap-mcp-worker

by re-x-agent
README.md
# jmap-mcp-worker

An [MCP](https://modelcontextprotocol.io) server for a mailbox on a JMAP server. It lists mailboxes, searches mail, reads message bodies and sends mail to an allowlist of recipients.

It is written for [Mailflare](https://github.com/hieunc229/mailflare) first (multi-mailbox logins, Mailflare's JMAP ids and rights model) and follows RFC 8620/8621 closely enough to work with other JMAP servers.

It runs two ways:

- **Cloudflare Worker**: a stateless remote MCP endpoint (Streamable HTTP) at `/mcp`.
- **stdio**: `npx jmap-mcp-worker` for local MCP clients.

## Tools

| Tool | What it does |
| --- | --- |
| `list_accounts` | JMAP accounts of the login (primary first) |
| `list_mailboxes` | Mailboxes with roles, counts and your rights |
| `search_emails` | Text/from/to/subject/mailbox/date/attachment/unread filters, paged |
| `read_email` | Headers, plain-text body (HTML only if there is no text part), attachment list |
| `send_email` | Plain-text mail. **Off unless `SEND_ALLOWLIST` is set**, and only to the recipients it lists |

## Authentication

The server holds no credentials. Each request carries the caller's `Authorization` header, which is forwarded as it is to the JMAP server, and only to the origin of `JMAP_SESSION_URL` (a session whose `apiUrl` points elsewhere is refused). Use whatever your JMAP server accepts as a Bearer or Basic credential:

- **Now**: an app password or API key (Mailflare accepts an app password as a Bearer token).
- **With an identity provider**: an OIDC access token from your IdP, if the JMAP server accepts OIDC bearer tokens. Set `OAUTH_ISSUER` and the Worker answers `401` with a `WWW-Authenticate: Bearer resource_metadata=…` challenge and serves `/.well-known/oauth-protected-resource`, so MCP clients that speak the MCP authorization flow can discover the issuer and sign in.

## Run it on Cloudflare

```sh
npm install
# edit wrangler.jsonc: JMAP_SESSION_URL (and optionally OAUTH_ISSUER, SEND_ALLOWLIST)
npx wrangler deploy
```

Connect an MCP client to `https://<your-worker>/mcp` with an `Authorization: Bearer <token>` header.

Local trial: put `JMAP_SESSION_URL=…` in `.dev.vars` (git-ignored), `npx wrangler dev`, then `JMAP_TOKEN=… node test/live.mjs`.

## Run it over stdio

```json
{
  "mcpServers": {
    "mail": {
      "command": "npx",
      "args": ["-y", "jmap-mcp-worker"],
      "env": {
        "JMAP_SESSION_URL": "https://mail.example.com/.well-known/jmap",
        "JMAP_TOKEN": "your-token",
        "SEND_ALLOWLIST": "you@example.com"
      }
    }
  }
}
```

`JMAP_USERNAME` and `JMAP_PASSWORD` (Basic) work in place of `JMAP_TOKEN`. Node 22.6 or newer.

## Mailflare notes

- A Mailflare login can own or be delegated several mailboxes. The server reads the account from the session (`primaryAccounts`); pass `accountId` to use another one. Older Mailflare versions expose one account per login with the mailboxes as top-level nodes, newer ones one account per mailbox; both work.
- `send_email` puts the draft in the Drafts folder of the sending identity's own mailbox before submitting it.

## Safety

- Sending is opt-in and recipient-limited. An unlisted recipient is refused before anything is created.
- Message bodies are untrusted input to the model. Treat mail content as data, not instructions.

## Development

```sh
npm test        # node --test, no network
npm run typecheck
```

## License

MIT. See [LICENSE](LICENSE).