Email Tools for Vercel AI SDK Agents

Add email capabilities to your TypeScript AI agents with MultiMail's API. Send, read, and reply to email with configurable human oversight.


The Vercel AI SDK is a TypeScript-first toolkit for building AI applications. Its tool system lets you define typed functions that LLMs can call, making it straightforward to add email capabilities through MultiMail's REST API.

MultiMail gives your Vercel AI SDK agents a complete email backend: sending, receiving, replying, thread tracking, and contact management. The default gated_send oversight mode means your agent composes emails but delivery waits for human approval, which fits naturally into the AI SDK's streaming UI patterns.

Whether you are building a Next.js chatbot that handles customer email or a standalone agent that manages an inbox, the combination of Vercel AI SDK's type-safe tools and MultiMail's email API provides a production-ready foundation.

Built for Vercel AI SDK developers

TypeScript-Native Integration

Both the Vercel AI SDK and MultiMail's API are TypeScript-friendly. Define email tools with Zod schemas for full type safety, and get structured responses you can render directly in your UI.

Streaming-Compatible

The Vercel AI SDK supports streaming tool calls. Your agent can stream its reasoning while making MultiMail API calls, showing users the email being composed in real time before it enters the approval queue.

Human-in-the-Loop by Default

MultiMail's gated_send mode pairs well with the AI SDK's UI components. Show users a preview of the composed email and let them approve it through your app's interface or the MultiMail dashboard.

Edge-Ready

The Vercel AI SDK runs on edge runtimes. MultiMail's REST API is edge-compatible with no Node.js-specific dependencies, so your email-capable agent works in serverless and edge environments.

Structured Tool Outputs

MultiMail returns structured JSON from every endpoint. The AI SDK can parse these into typed objects, letting your agent reason about inbox contents, thread history, and contact data with full type information.


Get started in minutes

Define Email Tools with Zod Schemas
typescript
import { generateText, tool } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';

const MULTIMAIL_API = 'https://api.multimail.dev/v1';
const headers = {
  'Authorization': 'Bearer mm_live_your_api_key',
  'Content-Type': 'application/json',
};

const sendEmail = tool({
  description: 'Send an email. In gated_send mode, the email is queued for human approval.',
  parameters: z.object({
    mailbox_id: z.string().describe('The mailbox to send from'),
    to: z.string().email().describe('Recipient email address'),
    subject: z.string().describe('Email subject line'),
    body: z.string().describe('Email body content'),
  }),
  execute: async ({ mailbox_id, to, subject, body }) => {
    const resp = await fetch(`${MULTIMAIL_API}/send`, {
      method: 'POST',
      headers,
      body: JSON.stringify({ mailbox_id, to, subject, body }),
    });
    return resp.json();
  },
});

const checkInbox = tool({
  description: 'Check a mailbox inbox for recent emails.',
  parameters: z.object({
    mailbox_id: z.string().describe('The mailbox to check'),
    limit: z.number().optional().default(10),
  }),
  execute: async ({ mailbox_id, limit }) => {
    const resp = await fetch(
      `${MULTIMAIL_API}/mailboxes/${mailbox_id}/inbox?limit=${limit}`,
      { headers }
    );
    return resp.json();
  },
});

Create type-safe MultiMail tools using the Vercel AI SDK's tool function and Zod for parameter validation.

Run an Email Agent
typescript
const result = await generateText({
  model: openai('gpt-4o'),
  tools: { sendEmail, checkInbox, replyEmail },
  maxSteps: 5,
  system: `You are an email assistant. You can send emails, check inboxes,
    and reply to messages. The mailbox uses gated_send mode, so all
    outgoing emails are queued for human approval before delivery.
    Mailbox ID: mbx_main`,
  prompt: 'Check my inbox and draft a reply to any unanswered emails.',
});

console.log(result.text);

Use generateText with tools to create an agent that can send and read email.

Next.js Route Handler with Email Tools
typescript
"cm">// app/api/chat/route.ts
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

export async function POST(req: Request) {
  const { messages } = await req.json();

  const result = streamText({
    model: openai('gpt-4o'),
    tools: { sendEmail, checkInbox, replyEmail, getThread },
    maxSteps: 5,
    system: `You are an email assistant for the user's MultiMail account.
      Always confirm the recipient and subject before sending.
      The mailbox uses gated_send mode — emails need approval.`,
    messages,
  });

  return result.toDataStreamResponse();
}

Create an API route that streams AI responses with email tool capabilities.

Thread and Reply Tools
typescript
const getThread = tool({
  description: 'Get all messages in an email thread for context.',
  parameters: z.object({
    thread_id: z.string().describe('The thread ID to retrieve'),
  }),
  execute: async ({ thread_id }) => {
    const resp = await fetch(
      `${MULTIMAIL_API}/threads/${thread_id}`,
      { headers }
    );
    return resp.json();
  },
});

const replyEmail = tool({
  description: 'Reply to an email within its thread.',
  parameters: z.object({
    message_id: z.string().describe('The message ID to reply to'),
    body: z.string().describe('Reply body content'),
  }),
  execute: async ({ message_id, body }) => {
    const resp = await fetch(`${MULTIMAIL_API}/reply`, {
      method: 'POST',
      headers,
      body: JSON.stringify({ message_id, body }),
    });
    return resp.json();
  },
});

Add thread retrieval and reply capabilities to your email agent.


Step by step

1

Create a MultiMail Account

Sign up at multimail.dev, create a mailbox, and generate an API key. Your key starts with mm_live_.

2

Install the AI SDK

Install the Vercel AI SDK and your preferred model provider.

bash
npm install ai @ai-sdk/openai zod
3

Define Email Tools

Create tool definitions using the AI SDK's tool function with Zod schemas for parameters. Implement the execute function to call MultiMail's REST API.

4

Create Your Agent

Use generateText or streamText with your email tools and a system prompt that explains the mailbox context and oversight mode.

bash
const result = await generateText({
  model: openai(&"cm">#039;gpt-4o'),
  tools: { sendEmail, checkInbox },
  maxSteps: 5,
  prompt: &"cm">#039;Check my inbox',
});
5

Handle Oversight

In gated_send mode, inform users when emails are pending approval. Optionally build approval UI in your app or direct users to the MultiMail dashboard.


Common questions

Can I use the MultiMail MCP server with the Vercel AI SDK?
The Vercel AI SDK currently focuses on its own tool format rather than MCP. The recommended approach is to define MultiMail tools using the AI SDK's tool function with direct REST API calls. This gives you full type safety with Zod schemas and works seamlessly with streaming.
Does this work with Next.js App Router?
Yes. Define your email tools in a server-side module and use them in route handlers under app/api/. The streamText function returns a response compatible with the AI SDK's useChat hook on the client side. Your email agent streams its reasoning while making API calls.
How do I store the MultiMail API key securely?
Store your API key in an environment variable (MULTIMAIL_API_KEY) and access it server-side only. In Next.js, add it to .env.local without the NEXT_PUBLIC_ prefix to ensure it is never exposed to the client. The tool execute functions run server-side, so the key stays secure.
Can the agent handle multiple mailboxes?
Yes. Add a listMailboxes tool that calls GET /mailboxes, and have the agent select the appropriate mailbox based on the user's request. You can also hardcode mailbox IDs in the system prompt if the agent always uses the same mailboxes.
What model providers work with this setup?
Any model provider supported by the Vercel AI SDK works, including OpenAI, Anthropic, Google, and Mistral. The email tools are model-agnostic — they are plain functions that any tool-calling model can invoke. Choose a model that supports tool use for best results.
How do I show email approval status in my UI?
After a send_email tool call returns a pending status, you can poll the MultiMail API for updates or use webhooks to get notified when emails are approved or rejected. Display this status in your chat UI so the user knows which emails are awaiting their review.

Explore more

The only agent email with a verifiable sender

Email infrastructure built for AI agents. Verifiable identity, graduated oversight, and a 38-tool MCP server. Formally verified in Lean 4.