MailKite
Get your API key
All guides Agent email

Agent email with Nylas

Give an AI agent email and it can hold a real conversation — field a support request, follow up on a thread, confirm a booking. Nylas connects an existing Gmail, Outlook, or IMAP mailbox to one API so your agent can send and read from it. This guide sets that up, then shows the same thing the MailKite way, where the agent gets email as native tools over MCP.

What you'll need

  • A Nylas API key and application from dashboard-v3.nylas.com.
  • An agent runtime that can call tools (function calling or MCP).
  • For inbound: a public HTTPS endpoint to receive webhooks.

Part 1 — Connect a mailbox to your agent with Nylas

1. Connect an account for a grant

Nylas fronts Gmail, Microsoft 365, and IMAP behind one v3 API. Send the user through hosted auth; they sign in with their provider and Nylas hands you back a grant_id — the mailbox handle you pass on every later request:

connect account
# Send the user to Nylas hosted auth; they sign in with their
# email provider and Nylas redirects back to your callback with a code.
GET https://api.us.nylas.com/v3/connect/auth?client_id=$NYLAS_CLIENT_ID
&redirect_uri=https://yourapp.com/callback&response_type=code

# Exchange the code for a grant — grant_id is the mailbox handle
# you pass on every later request.
# → { "grant_id": "...", "email": "ada@gmail.com", ... }

2. Send from the agent

Expose sending as a tool your agent can call. Under the hood it's a POST to the grant's send endpoint, authed with your Bearer key (use api.eu.nylas.com for the EU region):

send
curl -X POST https://api.us.nylas.com/v3/grants/$GRANT_ID/messages/send \
-H "Authorization: Bearer $NYLAS_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": [{ "email": "ada@example.com" }],
"subject": "Re: your order #1042",
"body": "<p>Shipped — tracking is attached.</p>"
}'

3. Receive

Read the mailbox on demand, or — better for an agent loop — subscribe a webhook so replies reach it in real time. List messages when you need history:

list messages
# Read the mailbox — newest messages, filterable and searchable
curl "https://api.us.nylas.com/v3/grants/$GRANT_ID/messages?limit=20" \
-H "Authorization: Bearer $NYLAS_API_KEY"

Then register a webhook on the message.created trigger. Nylas POSTs a notification for each new message that you feed back into the model:

handle-inbound.js
// Subscribe a webhook to the message.created trigger; Nylas POSTs
// a notification whenever a new message lands in the connected mailbox.

app.post("/hooks/nylas", (req, res) => {
const { type, data } = req.body;
if (type === "message.created") {
const message = data.object;
// Hand it to your agent's loop
agent.handle({
from: message.from,
subject: message.subject,
body: message.body,
});
}
res.sendStatus(200);
});

That's an agent on a Nylas mailbox: connect the account for a grant, wrap send and the inbound webhook as tools, and let your agent run the conversation.

Part 2 — The same, the MailKite way

With MailKite the agent gets its own address on a domain you own — nothing to connect, no end-user OAuth grant to broker. MailKite is MCP-native, so your agent doesn't call a REST API you wrapped by hand — it speaks MCP and gets email as first-class tools. Connect the server once and the same send-and-reply loop is a few tool calls.

1. Connect the MCP server

Add the hosted server. On the first connect it signs you in over OAuth in the browser — no key to copy — or pass your account key for headless runs:

connect
# Hosted MCP server — OAuth in the browser on first connect, no key to copy
claude mcp add --transport http mailkite https://mcp.mailkite.dev/mcp

# Headless / CI — pass your account key instead of the browser flow
claude mcp add --transport http mailkite https://mcp.mailkite.dev/mcp \
--header "Authorization: Bearer mk_live_..."

Any MCP client — Cursor, Claude Desktop, Cline, Zed — takes the same URL:

mcp config
{
"mcpServers": {
"mailkite": { "url": "https://mcp.mailkite.dev/mcp" }
}
}

2. Send from your agent

The agent calls mailkite_send over any verified domain — HTML, text, cc/bcc, attachments, and in-thread replies. Pick AI in the card for the agent's view, or any language to make the same send from your own code:

send
import { MailKite } from "mailkite";

const mk = new MailKite(process.env.MAILKITE_API_KEY);

const { id, status } = await mk.send({
from: "hello@myapp.ai",
to: "ada@example.com",
subject: "Your invoice #1042",
html: "<p>Thanks! Receipt attached.</p>",
});
Install Docs →
response
202 Accepted
{ "id": "msg_2Hk9…", "status": "queued" }

3. Receive — or hand the whole inbox to the agent

Every address on the domain is receivable. The agent can read inbound with its own tools, or you turn an address into an inbox agent with one route — the model reads and replies on its own:

create the agent route
import { MailKite } from "mailkite";

const mk = new MailKite(process.env.MAILKITE_API_KEY);

// One route turns an address into an AI inbox agent — the prompt
// is the program, and unresolved mail escalates to a human.
await mk.createRoute({
match: "support@myapp.ai",
action: "agent",
agentPrompt:
"Answer billing and account questions for myapp.ai. Reply in-thread. " +
"Escalate anything you can't resolve to humans@myapp.ai.",
agentForwardTo: ["humans@myapp.ai"],
});
Install Docs →
read inbound
// Or read inbound yourself — the agent has tools for it
mailkite_list_messages({ limit: 20 }) // newest first
mailkite_get_message({ id: "msg_2Hk9…" }) // full body, headers, attachment links

Same key manages the domain too — mailkite_create_domain, mailkite_verify_domain — so the agent can provision its own inbox end to end. See Connect your agent for the full tool list.

A little about MailKite

MailKite is programmable email for developers and AI agents. Connect the MCP server once and your agent can send over a verified domain, read its inbound as clean JSON, spin up new inboxes, manage domains, and become an inbox agent that answers mail on its own — all as native tools, no REST wrapper to maintain. It works the same over the SDKs, the CLI, or raw HTTP, and agent inboxes are on the free tier.

Next: Connect your agent for every MCP tool, or Set up your agent's email to go from zero to a working inbox.