mailgate
# mailgate
mailgate lets AI agents use Apple Mail on a Mac within limits that the mailbox owner sets. For each account, the
owner chooses what agents may do (read, mark, organise, write drafts, send, move to Trash) and which mailboxes they
can see. The agent sees nothing beyond those limits: a tool the policy does not allow is not offered, and messages
outside the visible accounts and mailboxes are never returned. The owner also decides how sending works: agents
write drafts only, or a send opens the email in Mail for the owner to send, or it waits for the owner's click in a
confirmation dialog, or it goes out without asking. Trusted recipients can get an option of their own. Moving mail to
Trash always asks, and only the owner can answer, because the agent cannot click the dialog.
mailgate is an MCP server, which means agent apps call its tools over MCP, the protocol they use for tools. It works
with Claude Code, Codex and the Claude desktop app. It reaches every account that Apple Mail holds, including IMAP
accounts such as Aruba that have no official connector of their own.
mailgate 0.1.1 is the current release. It is tested on one Mac (macOS 15.6) with Gmail, iCloud and Aruba IMAP accounts.
## Contents
- [What mailgate protects against](#what-mailgate-protects-against)
- [Getting started without a terminal](#getting-started-without-a-terminal)
- [Getting started from a terminal](#getting-started-from-a-terminal)
- [The policy](#the-policy)
- [Tools](#tools)
- [How it works](#how-it-works)
- [Known limits](#known-limits)
- [Development](#development)
## What mailgate protects against
mailgate guards against two risks: an agent that makes a mistake, and an agent misled by the text of an email. The
second risk is called prompt injection, where a message contains instructions aimed at the agent. How firmly the
limits hold depends on whether the agent also has a shell on the same Mac.
| Agent app | What the policy guarantees |
|---|---|
| Claude desktop app | Every limit holds as long as the agent has neither a shell nor a tool that controls the screen: mailgate is then its only way to Mail. A computer-use tool can click in Mail directly, including on mailgate's own confirmation dialogs, so leave Mail out of what such a tool may control. macOS gives the permission to control Mail (and Full Disk Access) to the Python interpreter that runs mailgate, so any other program run by that interpreter shares it. |
| Claude Code, Codex | Forbidden tools are absent and the scope is enforced inside mailgate, which stops mistakes. An agent determined to go around mailgate can still use its shell: run its own scripts against Mail, read the mail files, or edit the policy file, because the terminal holds the same macOS permissions. `mailgate doctor` says which apps hold them. |
| ChatGPT | Not supported: ChatGPT reaches only servers on the internet, not one on the Mac. |
Three more measures apply to every agent:
- **Mail text is marked as untrusted.** Every field that carries text written by a sender has a name ending in
`_untrusted`, and the tool descriptions tell the agent to treat that text as data. Bodies come back as plain text,
with scripts, remote images, link targets and text hidden by styling removed.
- **Sending and Trash involve the owner.** Unless the policy sends without asking, an email goes out only after the
owner presses Send, in Mail or in a native dialog that shows the sender, every recipient and the start of the text.
Trash always lists the messages in a dialog first. Only a click counts; Cancel or two minutes without an answer
refuse. In Claude Code, `mail_trash`, and `mail_send` when every send asks, also carry the marker with which Claude
Code asks the person before every call.
- **Everything is logged.** Every call adds a line to `~/.local/state/mailgate/audit.jsonl` with the tool, the
decision, the reason and the message handles. The line never holds a subject, an address or a body, and
`mailgate log` shows the recent lines.
## Getting started without a terminal
Someone comfortable with a terminal runs the installer once, as described in
[Getting started from a terminal](#getting-started-from-a-terminal). After that, everything is done in windows.
1. **Choose what agents may do.** For each Mail account, the installer asks you to pick one of five choices: not
visible, read only, read and write drafts, assistant (read, mark, organise, draft and save attachments), or full
(everything in assistant, plus sending and moving to Trash, each after your confirmation).
2. **Give Full Disk Access.** mailgate reads Mail's own index of messages, which macOS protects. The installer
names the program to switch on and opens the Full Disk Access pane of System Settings (Privacy & Security). For
the Claude desktop app the program is the Python interpreter that runs mailgate, usually `python3.12`; for Claude
Code and Codex it is your terminal app. `mailgate access`, and the settings page, show the same list later.
3. **Allow Mail control once.** The first time an agent uses mailgate, macOS asks whether that program may control
Mail. Click Allow.
4. **Change the choices later** by double-clicking **mailgate Settings** in the Applications folder of your home
folder. A page opens in the browser showing each account with its mailboxes as checkboxes, the sending options,
the recent agent activity and the Full Disk Access status. A change that gives agents more access asks you to
confirm it before it is saved. Restart the agent app afterwards so that it sees the new tools.
The settings page, shown here with example accounts: one account limited to three mailboxes for an assistant, one
hidden from agents, and a Gmail account with full access, where sending asks each time except for trusted recipients.

## Getting started from a terminal
mailgate runs on macOS with Apple Mail already set up, and it installs with [uv](https://docs.astral.sh/uv/).
```bash
# install, choose the policy, give Full Disk Access, register in the agent apps found, and check
curl -LsSf https://raw.githubusercontent.com/pablogiaccaglia/mailgate/main/install.sh | bash
# or step by step
uv tool install git+https://github.com/pablogiaccaglia/mailgate
mailgate setup # asks, account by account; writes ~/.config/mailgate/policy.json
mailgate access # names the program that needs Full Disk Access and opens the pane
mailgate install claude-code # or: codex, claude-desktop (each takes --profile and --dry-run)
mailgate doctor # checks Mail, permissions, the policy and the agent apps
```
Everyday commands:
| Command | What it does |
|---|---|
| `mailgate settings` | Opens the settings page |
| `mailgate access` | Names the programs that need Full Disk Access and opens that pane of System Settings |
| `mailgate policy show` | Prints the policy as a table |
| `mailgate log` | Shows the last agent calls and their decisions |
| `mailgate undo --last N` | Puts back the last marks and moves (moves on IMAP accounts; a Gmail move cannot be undone this way) |
| `mailgate doctor` | Reports what is set up and what an agent could still do outside mailgate |
## The policy
The policy is a JSON file at `~/.config/mailgate/policy.json` that only its owner can read. If the file contains a
key or a value that mailgate does not know, mailgate refuses the whole file. The server then offers only
`mail_policy`, which explains what is wrong.
```json
{
"version": 1,
"accounts": {
"Work": {"uuid": "6F1C2A3B-0000-4000-8000-000000000001", "max": ["read", "flag", "organize", "draft", "send"]}
},
"default_profile": "assistant",
"profiles": {
"assistant": {
"accounts": {
"Work": {
"capabilities": ["read", "flag", "organize", "draft", "send"],
"mailboxes": {"mode": "only", "names": ["INBOX", "Clients"]},
"overrides": {"Clients/Legal": ["read"]}
}
},
"send": {"mode": "confirm", "trusted": {"recipients": ["*@example.com"], "mode": "auto"}}
}
},
"save_folder": "~/Downloads/mailgate",
"limits": {"page_size": 25, "max_page_size": 100, "max_body_chars": 20000, "sends_per_hour": 10}
}
```
The top-level `accounts` block sets the upper limit for every profile. It lists the accounts agents may ever see,
pins each one to the identifier Mail uses for it, and gives the most any profile may do there. A profile can narrow
that limit and can never go beyond it. Each agent app picks a profile with `--profile` when it starts the server, so
the Claude desktop app might use one that writes drafts and Codex one that only reads. An agent with a shell can
start the server with a different profile, which means profiles keep one use apart from another and do not give
agents different levels of trust.
**Capabilities.** Each capability in the table allows one kind of action, and every capability other than `read`
needs `read` on the same message.
| Capability | Allows | Comparable to |
|---|---|---|
| `read` | Listing accounts and mailboxes, searching, reading messages and threads, listing attachments | Gmail `readonly`, Microsoft `Mail.Read` |
| `save` | Saving an attachment into `save_folder` | |
| `flag` | Marking read or unread, flagging | |
| `organize` | Moving messages between visible mailboxes, creating a mailbox | Microsoft `Mail.ReadWrite` |
| `draft` | Writing drafts, reply drafts and forward drafts | |
| `send` | Sending a draft, under the send rules | Gmail `send`, Microsoft `Mail.Send` |
| `trash` | Moving messages to Trash, after confirmation | |
**Mailboxes.** The `mailboxes` setting either makes visible `only` the listed mailboxes, or makes visible
`all_except` them. A name also covers the mailboxes below it, except `INBOX`, which covers only the inbox. Names are
written as Mail shows them. The `overrides` setting gives one mailbox, together with the mailboxes below it,
capabilities of its own. A message is visible only when every mailbox it belongs to is visible, and the profile may
do on it only what all of those mailboxes allow.
Gmail stores each message once and shows folders as labels, so mailgate works out which mailboxes a Gmail message
belongs to in a fixed order. A message belongs to its INBOX and user labels. If it has neither, it belongs to
Gmail's own labels (Important, Starred, Sent, Spam, Trash, Drafts). If it has no label at all, it belongs to All
Mail. As a result, Gmail's Important label does not hide inbox mail, and hiding a user label hides every message that
carries it. Mail's scripting cannot see labels, so on the scripting fallback a Gmail account is available only when its
rule hides nothing but Spam and Trash (the default) and gives no mailbox capabilities of its own; with any other
rule, the account stays out until Full Disk Access is on, and `mail_policy` and `mailgate doctor` say so.
**Sending.** The `send` mode sets how agents send mail on accounts whose capabilities include `send`:
| Mode | Behaviour |
|---|---|
| `off` (default) | There is no send tool; agents write drafts and the owner sends them from Mail. |
| `open_in_mail` | `mail_send` opens the draft in a Mail window; the owner reads it, can change it, and presses Send. |
| `confirm` | Every send shows the confirmation dialog. |
| `auto` | The draft is sent at once, without asking. An agent misled by an email could then send anything it can read to anyone, so `mailgate doctor` and the settings page point this out. |
`trusted` gives recipients that match its patterns (an address, `*@domain`, or `*` for anyone) an option of their
own. A draft gets the strictest option among its recipients, so the example above sends to colleagues without
asking and asks as soon as one recipient is someone else. A trusted list can also be stricter than the profile's
option, for example to never send to one address. Policies written with the older modes keep
working: `draft_only` means `off`, and `allowlist` with `recipients` means `confirm` with those recipients trusted
under `auto`. `sends_per_hour` caps sends and opened windows under every option.
## Tools
The server always offers `mail_policy`, and offers each of the other tools only when the active profile grants that
tool's capability somewhere. Tools refer to messages by handles, which the search and thread tools return, and no
tool selects a message by its subject.
| Tool | Capability | Does |
|---|---|---|
| `mail_policy` | none | Says what the agent may do, and what is wrong with the policy if it cannot be used |
| `mail_accounts`, `mail_mailboxes` | `read` | List the visible accounts and mailboxes |
| `mail_search` | `read` | Searches by text, sender, recipient, subject, dates, read and flagged state, attachments; newest first, in pages |
| `mail_get`, `mail_thread`, `mail_attachments` | `read` | Read one message as plain text, its conversation, its attachment names |
| `mail_save_attachment` | `save` | Saves one attachment into `save_folder`, never overwriting |
| `mail_mark` | `flag` | Marks messages read or unread, flagged or not |
| `mail_move`, `mail_create_mailbox` | `organize` | Move messages, create a mailbox |
| `mail_draft`, `mail_draft_reply`, `mail_draft_forward`, `mail_drafts` | `draft` | Write and list drafts; nothing is sent |
| `mail_send` | `send` | Sends a draft, opens it in Mail or asks first, as the send options say |
| `mail_trash` | `trash` | Moves messages to Trash after the owner confirms |
## How it works
mailgate reads Mail's own index of messages (`~/Library/Mail/V10/MailData/Envelope Index`, opened read-only) for
lists and searches, and Mail's message files for bodies. A page of 50 messages from an 86,525-message inbox takes
0.05 s, and a search covers all mail, body text included. When the program running mailgate lacks Full Disk Access,
mailgate falls back to Mail's scripting, which needs only the permission to control Mail: it reads one page at a
time, newest first, and a search covers the newest 200 messages of each mailbox it looks at, by subject and sender.
On the development Mac such a page took about 10 s on a 4,183-message inbox and two to three minutes on the
86,525-message one, so the fallback suits small accounts only. Every change goes through a fixed script that ships with mailgate and receives its values as arguments, so no
text from an agent or an email can become code. A journal records the state before each change, so marks and moves
can be put back.
The server checks the policy on every call. A narrower policy takes effect at the next call. A wider one needs the
agent app to restart, because the app keeps the tool list it received when it started.
## Known limits
- **Moves on Gmail add a label.** Mail's scripting cannot take a label off a Gmail message without moving the
message to Trash. On Gmail, `mail_move` therefore adds the target label, keeps the old one, and tells the agent so.
- **Drafts and sends open a compose window for about a second.** mailgate cannot keep the window hidden, because a
hidden compose window did not save drafts on an IMAP account and Mail cannot close hidden windows.
- **Reply and forward drafts are written by mailgate.** They quote the original message as text. A forward does not
carry the original's attachments, and mailgate says so. `mail_send` refuses a draft that has attachments, so a
draft like that has to be sent from Mail.
- **Sent text is the text the dialog shows.** mailgate sends the draft's plain text with invisible characters
removed, so joined emoji and the zero-width joiners some scripts need lose those characters.
- **Full Disk Access is a broad permission.** It covers far more than Mail, and every other program run by the same
interpreter or terminal shares it. Without it, the scripting fallback searches only the newest 200 messages of
each mailbox, not body text, and a Gmail account works only with the default rule (see
[The policy](#the-policy)).
- **Mail's index is undocumented.** At start, mailgate checks that every column it uses exists, and it stops with a
clear message if a macOS update has changed them.
## Development
```bash
uv sync
uv run pytest -q # unit tests, on an in-memory Mail
uv run ruff check src tests tools
uv run python tools/check_tree.py # before every push: no real addresses, account names, tokens or mail files
```
Tests against the real Mail run only when you switch them on with their pytest marker, and each one acts only in
the account and mailbox its environment variables name:
| Marker | Needs | Does |
|---|---|---|
| `live` | `MAILGATE_LIVE_ACCOUNTS=a,b` | Reads with two accounts exposed and checks that every other account stays invisible; add `MAILGATE_READ_PATH=scripts` to check the scripting path where the index is readable |
| `live_write` | `MAILGATE_TEST_ACCOUNT`, and `MAILGATE_TEST_DRAFTS=Drafts` for an account with real mail | Creates mailboxes, marks, moves and drafts only inside that account's `mailgate-test` mailbox |
| `live_send` | `MAILGATE_TEST_ACCOUNT`, a person at the Mac | Sends one email to the account's own address and moves one draft to Trash, through the dialogs |
The design is described in [docs/design.md](docs/design.md), and the plan and progress are tracked in
[program/STATUS.md](program/STATUS.md). mailgate is released under the MIT licence and is not affiliated with Apple.
TDQS
Scored across 1 tool
With only one tool there is no risk of selecting the wrong tool, so tool-to-tool ambiguity is effectively zero. However, the single tool's own scope is vague—'accounts, mailboxes, actions, send rules'—so an agent cannot easily tell whether it queries policy or performs mail operations.
The lone name 'mail_policy' is clean, readable snake_case with a sensible domain prefix. With a single tool there is no pattern to compare against, so consistency is only trivially satisfied rather than demonstrated.
A single tool is far too thin for a server framed as a mail gateway, which implies reading, sending, and mailbox management. One policy-introspection tool cannot carry the apparent scope of the domain.
The surface exposes only policy discovery, with no tools to actually list mailboxes, read messages, send mail, or manage rules. An agent would hit a dead end immediately after learning what it is permitted to do.