Email for a Vercel AI SDK agent
Give the agent its own inbox in one call, then load Mailgi's tools with createMCPClient. About 25 lines of TypeScript, any model provider.
To give a Vercel AI SDK agent email, register an address with one POST to Mailgi, then call createMCPClient from @ai-sdk/mcp with an http transport to https://api.mailgi.xyz/mcp and the key as a Bearer header. Pass mail_send, mail_list and mail_get to generateText or streamText, and the agent can receive verification codes, answer mail and write to people from its own address.
Give the agent an address
One call, no signup form. Do this once per agent and keep the key in your secrets store.
Register
The response carries the address and an API key that is shown exactly once.
An MCP client can also do this itself: register_agent is one of the tools that work with no key. The key still has to reach your config once, so for a server-side agent the one-off call below is simpler.
# Once, outside the agent: give it an address and keep the key
curl -s -X POST https://api.mailgi.xyz/v1/agents/register \
-H "Content-Type: application/json" -d '{"label":"my-agent"}'
# => {"emailAddress":"buzzing-falcon@mailgi.xyz","apiKey":"amb_..."}
export MAILGI_API_KEY=amb_... # shown once: store it
Load the tools over MCP
The AI SDK's MCP client turns every tool on the server into an AI SDK tool, schema included. Nothing to host, no tool definitions to write.
The agent
The http transport is Streamable HTTP, which is what Mailgi serves. The key goes in as an ordinary header.
tools() returns all 67 tools: domains, keys, webhooks, billing. Destructuring the three mail tools keeps the model focused; add mail_move or mail_delete if it should file or clean up.
Close the client when the request is done. In a route handler, close it in onFinish when you use streamText.
The client and tool calls were run against the live API with ai 7.0 and @ai-sdk/mcp 2.0, and the example type-checks. Swap the model line for any provider.
// npm install ai @ai-sdk/mcp @ai-sdk/anthropic
import { generateText, stepCountIs } from 'ai';
import { createMCPClient } from '@ai-sdk/mcp';
import { anthropic } from '@ai-sdk/anthropic';
const mailgi = await createMCPClient({
transport: {
type: 'http',
url: 'https://api.mailgi.xyz/mcp',
headers: { Authorization: `Bearer ${process.env.MAILGI_API_KEY}` },
},
});
try {
const { mail_send, mail_list, mail_get } = await mailgi.tools();
const { text } = await generateText({
model: anthropic('claude-sonnet-5-5'),
tools: { mail_send, mail_list, mail_get },
stopWhen: stepCountIs(5),
prompt: 'Check my inbox and summarise anything new.',
});
console.log(text);
} finally {
await mailgi.close();
}
What the tools can't do yet
Better to know before the model tries.
No search, no threads
There is no search parameter; mail_list pages newest first and the model matches messages itself. Replies are new messages with “Re:” in the subject.
No attachments
Send links instead. Incoming attachments are not exposed.
Mail to strangers is metered
Sending to other @mailgi.xyz agents is free; to outside addresses it costs $0.0005 per recipient plus $0.02 the first time you write to a new address. With no balance, a send comes back as a tool result starting “HTTP 402” with the top-up link, not as a thrown error, so the model can pass the link on.
Questions, answered
01How do I give a Vercel AI SDK agent an email address?
Register one with a single POST to https://api.mailgi.xyz/v1/agents/register, then load Mailgi's tools with createMCPClient from @ai-sdk/mcp, using an http transport to https://api.mailgi.xyz/mcp and the key as a Bearer header.
02Does it work with streamText and any model?
Yes. The MCP tools are ordinary AI SDK tools, so they work with generateText, streamText and agents, on any provider the AI SDK supports.
03Can the agent receive verification codes?
Yes. Once it has its own address it can use mail_list and mail_get to find the code. Poll every few seconds after triggering the signup; most codes arrive within seconds.
04What does it cost?
Registering and receiving are free, and so is mail between Mailgi agents. Mail to outside addresses costs $0.0005 per recipient plus $0.02 the first time an agent writes to a new address, from a prepaid balance.
One POST to start.
No signup form, no OAuth, no card to start. Receiving is free.
Open the dashboard →