The Vercel AI SDK makes it easy to add tool use to AI-powered applications. Adding email tools lets your app create inboxes, wait for verification codes, and send messages.

Define Email Tools

import { tool } from "ai";
import { z } from "zod";

const BASE = "https://api.agentmailr.com";
const KEY = process.env.AGENTMAILR_API_KEY!;
const h = { "X-API-Key": KEY, "Content-Type": "application/json" };

export const emailTools = {
  createInbox: tool({
    description: "Create a new email inbox for an AI agent",
    parameters: z.object({
      name: z.string().optional().describe("Optional name for the inbox"),
    }),
    execute: async ({ name }) => {
      const r = await fetch(`${BASE}/v1/inboxes`, {
        method: "POST", headers: h, body: JSON.stringify({ name })
      });
      return r.json();
    },
  }),

  waitForOtp: tool({
    description: "Wait for an OTP verification code to arrive in an inbox",
    parameters: z.object({
      inbox_id: z.string().describe("The inbox ID to wait on"),
      timeout: z.number().optional().default(60),
    }),
    execute: async ({ inbox_id, timeout }) => {
      const r = await fetch(
        `${BASE}/v1/inboxes/${inbox_id}/otp?timeout=${timeout}`,
        { headers: h, signal: AbortSignal.timeout((timeout! + 5) * 1000) }
      );
      return r.json();
    },
  }),

  sendEmail: tool({
    description: "Send an email from an inbox",
    parameters: z.object({
      inbox_id: z.string(),
      to: z.string(),
      subject: z.string(),
      text: z.string(),
    }),
    execute: async ({ inbox_id, ...body }) => {
      const r = await fetch(`${BASE}/v1/inboxes/${inbox_id}/send`, {
        method: "POST", headers: h, body: JSON.stringify(body)
      });
      return r.json();
    },
  }),
};

Use in a Route Handler

import { streamText } from "ai";
import { anthropic } from "@ai-sdk/anthropic";
import { emailTools } from "@/lib/email-tools";

export async function POST(req: Request) {
  const { messages } = await req.json();
  const result = streamText({
    model: anthropic("claude-sonnet-4-6"),
    system: "You are an agent that can create email inboxes and handle verification flows.",
    tools: emailTools,
    messages,
    maxSteps: 10,
  });
  return result.toDataStreamResponse();
}

What the Agent Can Do

With these tools, your Vercel AI SDK app can autonomously register for services, complete email verification, and send notification emails. The tools work with any AI model that supports function calling.