← All guides

Disposable Email for the Vercel AI SDK

tool() wrappers + streamText

The Vercel AI SDK is the dominant choice for TypeScript / Next.js agent apps. Wrapping myagentinbox takes three tool() definitions; pass them to streamText or generateText with maxSteps set so the model can chain calls.

Install

npm install ai zod
# plus your provider, e.g.
npm install @ai-sdk/openai
# or
npm install @ai-sdk/anthropic

Define the tools

// lib/email-tools.ts
import { tool } from "ai";
import { z } from "zod";

const BASE = "https://myagentinbox.com";

export const createInbox = tool({
  description: "Create a disposable email inbox that expires in 24 hours.",
  parameters: z.object({}),
  execute: async () => {
    const r = await fetch(`${BASE}/api/inboxes`, { method: "POST" });
    return (await r.json()).data;
  },
});

export const checkInbox = tool({
  description: "List messages in a disposable inbox.",
  parameters: z.object({ address: z.string() }),
  execute: async ({ address }) => {
    const r = await fetch(`${BASE}/api/inboxes/${address}/messages`);
    return (await r.json()).data;
  },
});

export const readMessage = tool({
  description: "Read the full body of a single message.",
  parameters: z.object({
    address: z.string(),
    messageId: z.string(),
  }),
  execute: async ({ address, messageId }) => {
    const r = await fetch(`${BASE}/api/inboxes/${address}/messages/${messageId}`);
    return (await r.json()).data;
  },
});

Use in a Next.js route handler

// app/api/chat/route.ts
import { openai } from "@ai-sdk/openai";
import { streamText } from "ai";
import { createInbox, checkInbox, readMessage } from "@/lib/email-tools";

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

  const result = streamText({
    model: openai("gpt-4o"),
    messages,
    tools: { createInbox, checkInbox, readMessage },
    maxSteps: 10,
    system:
      "You can use disposable email. Call createInbox when you need an address. " +
      "Wait 5-10 seconds between checkInbox calls.",
  });

  return result.toDataStreamResponse();
}
Why maxSteps? Without it, the AI SDK stops after the first tool call. Setting maxSteps: 10 lets the model chain createInbox → checkInbox → readMessage in one request.

One-shot generateText

import { anthropic } from "@ai-sdk/anthropic";
import { generateText } from "ai";
import { createInbox, checkInbox, readMessage } from "./email-tools";

const { text } = await generateText({
  model: anthropic("claude-opus-4-7"),
  prompt: "Create an inbox, wait for the test email I'm about to send, and read it back.",
  tools: { createInbox, checkInbox, readMessage },
  maxSteps: 8,
});

console.log(text);

Limits