# Email assistant and MCP Use the built-in AI assistant, or connect your own AI tool through **MCP** (Model Context Protocol, a standard way for AI tools to use other apps). ## Connect an external AI tool MCP mail tools need no AI provider in Mailflare. 1. Open **Settings → API keys** and create a key with **Allow MCP access**. 2. Choose the mailboxes and mail permissions. 3. Copy the key or the ready-made agent instructions. **The key is shown only once.** 4. Connect the client with: | Setting | Value | | --- | --- | | URL | `https:///mcp` | | Transport | Streamable HTTP | | Header | `Authorization: Bearer ` | Keys can also be created, listed and revoked through the signed-in endpoint `/api/agent/mcp-keys`. The key can use only the chosen mailboxes, with your current permissions. A mailbox includes its aliases. `request_send` creates a review that you confirm in Mailflare. For trusted automation, enable **Allow this key to approve its own sends** in **Settings → API keys** and verify your identity. The setting starts off for every key. A key with the setting on can use `approve_send` only when it created both the draft and its review request. Drafts from before this feature still need human review. You can turn the setting off without verifying again. MySQL deployments use the Mailflare review page for delivery. ## Set up the built-in assistant 1. In **Admin → Agent**, set a provider: Cloudflare Workers AI or an OpenAI-compatible service. You can add several models. 2. In the assistant, open **Settings** and choose a model for each mailbox. - A custom provider URL saved in the app must be public HTTPS. For a private endpoint, such as a local model, set `AI_BASE_URL` and `AI_API_KEY`. - Chat is on once a provider is set. Admins can turn it off in **Admin → Agent**. - The assistant works only in the selected mailbox. - Mailbox managers set writing instructions, a draft reviewer and a daily auto-draft limit. - **Auto-drafts** are off until enabled per mailbox. A person must review each draft before it is sent. - **Admin → Agent → View Usage** shows requests, estimated cost, daily use and an activity log. ## Work with drafts - Ask follow-up changes in chat: body, subject, sender, recipients, send time, or attachments (copied from an email in the mailbox). - A send time does not schedule anything. The assistant creates a review link, and you confirm the draft and time. - In the composer you can also edit the draft, add local files and request the review. - Scheduled assistant drafts need the latest database migration. While the assistant replies, new messages wait in a queue and run in order. Edit or remove them, or select **Steer** to stop the reply and send one next. Switching chats or closing the assistant clears the queue. ## What the assistant can do ### Read the open email It reads the email you have open, and its conversation when needed, only when it uses the `get_current_email` tool. Nothing about the email reaches the AI provider before that. ### Questions about Mailflare Ask how to do something or where a setting is. It searches these docs with the `search_docs` tool (chat assistant only). ### Templates, files and bulk email - **Templates:** reads your saved composer templates. Links appear as `text (address)`. The template tools also work over MCP. - **Files:** attach with the paperclip. CSV, TSV, text formats (txt, md, JSON, XML, YAML, HTML, eml, ics, vcf) and `.docx`. No PDF, images or Excel; save a spreadsheet as CSV. Up to 5 files, 5 MB each, about 1,000,000 characters in total. - **Privacy:** your browser turns files into text. Parts the assistant reads go to the AI provider. Files are gone when you switch chats or reload. - **File and bulk tools** (`read_file`, `propose_emails`) are in the chat assistant only. - **Several emails:** a table of up to 50. Open a row to edit it, untick rows to skip, then **Send**, **Save as drafts** or **Cancel**. Send asks for confirmation and names new recipients. Only these buttons send. A failed row can be retried. Edits last until reload. Saved drafts need the usual review. ### Read tools (chat only) | Tool | Reads | | --- | --- | | `read_attachment` | An email attachment (CSV, TSV, text, `.docx`; up to 5 MB). The text goes to the AI provider. | | `search_contacts` | Contacts by name or address | | `list_folders` | Custom folders of the mailbox | | `list_follow_ups` | Reminders on sent emails with no reply | ### Changes you approve (chat only) Nothing happens until you press **Approve**. Permissions are checked again then. | Tool | Change | | --- | --- | | `star_emails` | Star or unstar up to 20 emails | | `snooze_emails` | Snooze inbox emails or end a snooze | | `move_to_folder` | Move received emails to a custom folder | | `set_follow_up` | Set (1–30 days) or remove a reminder | | `create_inbox_rule` | File future mail to a folder, Spam or Trash (no forward or reject) | | `unsubscribe_sender` | Unsubscribe; web-page-only lists give you a link | | `save_template` | Save a template | | `add_note` | Add a conversation note, visible to all mailbox readers | ## MCP tool reference ### Mail tools | Permission | Tools | | --- | --- | | Read | `list_mailboxes`, `list_emails`, `search_emails`, `get_email`, `get_thread`, `list_templates`, `get_template`, `get_scheduled_send`, `list_conversation_notes` | | Draft | `draft_email`, `draft_reply`, `update_draft`, `discard_draft`, `cancel_scheduled_send` (also needs a sending right on the mailbox) | | Organize | `mark_email_read`, `move_email`, `move_emails`, `add_conversation_note`, `update_conversation_note`, `delete_conversation_note` | | Request send | `request_send`, `get_send_request` | - Template tools return only templates saved under the key's mailboxes. - Use `update_draft`, then `request_send`, for immediate or scheduled delivery. Attachments are copied from emails in the allowed mailbox; add local files in the composer. - `list_emails` pages with a cursor. Omit `cursor` (or use `null`) first, then pass the returned `nextCursor` with the same mailbox and folder. `null` means the end. A cursor is a message ID; an invalid one returns retry instructions. ### Calendar tools Needs the **Read calendar** or **Manage calendar** permission. Calendar-only keys need no mailbox. Tools: `get_calendars`, `get_schedule`, `search_events`, `get_event`, `find_free_time`, `create_event`, `update_events`, `delete_events`. - With an MCP key, `create_event` saves at once. In the assistant, it is a proposal you approve. Updates and deletes are always proposals. - Updates and deletes affect the **whole recurring series**. - No invitations or cancellations are sent. - Use ISO 8601 times with offsets. Schedule and free-time ranges are limited to 90 days. ### Admin tools Create a separate key in **Admin → API keys**. It manages domains, accounts, mailboxes and storage, but cannot read mail. See [API](api.md#account-and-mailbox-management). ## Add outside MCP servers to the assistant The chat assistant can use remote MCP servers (Streamable HTTP, public `https://`). - **Add:** **Settings → MCP credentials** (edit and test before saving), or paste details into chat: `mcpServers` JSON, `url:` / `token:` lines, or an address in a message mentioning MCP. - **Tokens:** removed from chat messages before storage or AI use. A token-like text with no server address is not sent. - **Saving:** Mailflare asks whether to save. Unsaved servers last for that chat. **Always save** (per browser) stops asking. Up to 20 per account. Saved tokens are never shown again. An empty token on edit keeps the old one, unless the address changed. - **Approval:** a tool not marked read-only runs only after **Run**; **Deny** refuses. **Always allow this tool** works for saved servers; **Ask again** in settings undoes it. ### Token encryption 1. Create a key of at least 16 characters: ```bash openssl rand -base64 32 ``` 2. Set it as `MCP_ENCRYPTION_KEY`. On Cloudflare: ```bash npx wrangler secret put MCP_ENCRYPTION_KEY ``` Self-hosted or Vercel: set an environment variable. 3. Apply database migration `0107_mcp_credentials` (PostgreSQL and MySQL: `0031`). Tokens are then stored with AES-256-GCM, so a leaked database or backup does not reveal them. It does not protect against someone who can read the server's environment. > **Warning:** Without the key, tokens are stored as plain text (and in backups). Old tokens stay plain until added again. If you lose or change the key, remove and re-add the servers. ## Troubleshooting | Problem | Cause | Fix | | --- | --- | --- | | No chat | No provider, or turned off | Check **Admin → Agent** | | Private model URL refused | App allows public HTTPS only | Use `AI_BASE_URL` | | MCP key sees no mail | Mailbox not selected or no permission | Create a key with the right mailboxes | | MCP cannot send | By design | Confirm the review link | | Attachment not readable | PDF, image or Excel | Convert to a supported format | ## Related - [API](api.md) - [Deployment](deploy-cloudflare.md#email-assistant-and-mcp)