Email for Cloudflare Agents SDK agents

Agents SDK has a built-in MCP client. One addMcpServer call and your Durable Object agent has an inbox.

Setup

  1. 01

    Create a mailbox

    One per agent, so its address and its key are the same identity.

  2. 02

    Put the key in a secret

    wrangler secret put MCPMAILER_KEY, so it is not in your config.

  3. 03

    Connect once, not per request

    addMcpServer in onStart; the connection is part of the agent state.

  4. 04

    Deploy

    The Durable Object keeps the connection across requests and hibernation.

Install
npm install agents
Agents SDK
await this.addMcpServer('mcpmailer', 'https://connect.mcpmailer.com/mcp', {
  transport: {
    type: 'streamable-http',
    headers: { Authorization: 'Bearer mmk_live_...' }
  }
});

Connect on start, then use the tools

Headers go inside transport. Passed at the top level they are silently dropped, and every call reaches us unauthenticated.

Connect on start, then use the tools
import { Agent } from 'agents';

export class SupportAgent extends Agent<Env> {
  async onStart() {
    await this.addMcpServer('mcpmailer', 'https://connect.mcpmailer.com/mcp', {
      transport: {
        type: 'streamable-http',
        headers: { Authorization: `Bearer ${this.env.MCPMAILER_KEY}` }
      }
    });
  }

  async onRequest(request: Request) {
    const { tools } = this.getMcpServers();
    return Response.json({ tools: tools.length });
  }
}

Worth knowing

01

Headers belong under transport

The third argument is an options object. An older signature took callbackHost as a bare third argument, so { headers } at the top level is accepted and then ignored.

02

Connect in onStart

Calling addMcpServer per request reconnects every time. onStart runs once per Durable Object instance, which is the lifetime you want.

03

We use a bearer key, so no OAuth callback

addMcpServer can return state "authenticating" with an authUrl for servers that need OAuth. With a key you get "ready" straight away and never handle the redirect.

What Cloudflare Agents SDK can reach

The endpoint speaks Streamable HTTP MCP with 67 tools across mail, contacts, notes, and a vault, including send_email, read_message, search_inbox, and wait_for_reply. Errors are structured so Cloudflare Agents SDK can react: a quota rejection includes the reset time.