Agent Frameworks

The Node.js SDK has ready-made email tools for the Vercel AI SDK and the OpenAI Agents SDK for TypeScript. Both get the same tools for one inbox, with zod schemas. By default the tools write drafts, and a person sends them.

Using an MCP client instead? See the MCP server. For a full example with a webhook, see a support agent that drafts replies.

Install

#
npm install @smtpfast/sdk ai @ai-sdk/openai zod

The tools need @smtpfast/sdk 0.3.0 or later, with ai 7 or @openai/agents 0.22, zod 4 and Node.js 22 or later. They are in their own entry points, @smtpfast/sdk/ai and @smtpfast/sdk/openai-agents, so the main SDK keeps no runtime dependencies.

Tools

#
ToolWhat it does
create_draftWrites a new email as a draft in the inbox. Nothing is sent.
list_inbox_threadsLists threads, newest first, with the sender, read state and a short preview. Takes a folder, a search query and unread only.
get_threadReads a thread: each message's addresses, where a reply goes, the SPF, DKIM, DMARC, spam and virus verdicts, plain-language warnings and the body.
reply_to_threadWrites a reply draft to the person who wrote the message (its Reply-To or From). The model cannot pick other recipients.
send_emailSends at once. Only there when you pass allowDirectSend: true.

Vercel AI SDK

#

smtpfastTools from @smtpfast/sdk/ai returns an object of tools by name, ready for generateText or streamText:

ts
import { generateText, stepCountIs } from "ai";
import { openai } from "@ai-sdk/openai";
import { SMTPfast } from "@smtpfast/sdk";
import { smtpfastTools } from "@smtpfast/sdk/ai";

const smtpfast = new SMTPfast(); // reads SMTPFAST_API_KEY
const tools = smtpfastTools(smtpfast, { inbox: "[email protected]" });

const { text } = await generateText({
  model: openai("gpt-5-mini"),
  tools,
  stopWhen: stepCountIs(8),
  system: "You answer support email. Write drafts. Never promise refunds.",
  prompt: "Read the unread threads and draft a reply to each.",
  // Keep customer email out of the provider's stored responses.
  providerOptions: { openai: { store: false } },
});

store: false keeps the responses off the provider's storage and makes the AI SDK send each step in full. Providers with zero data retention need it; elsewhere the only cost is that each request carries the whole conversation.

OpenAI Agents SDK

#

smtpfastTools from @smtpfast/sdk/openai-agents returns an array of function tools with strict schemas, ready for new Agent({ tools }). Each tool returns JSON text to the model.

ts
import { Agent, run } from "@openai/agents";
import { SMTPfast } from "@smtpfast/sdk";
import { smtpfastTools } from "@smtpfast/sdk/openai-agents";

const smtpfast = new SMTPfast(); // reads SMTPFAST_API_KEY

const agent = new Agent({
  name: "Support",
  instructions: "You answer support email. Write drafts. Never promise refunds.",
  tools: smtpfastTools(smtpfast, { inbox: "[email protected]" }),
});

const result = await run(agent, "Read the unread threads and draft a reply to each.");
console.log(result.finalOutput);

Drafts and sending

#

Without allowDirectSend, there is no send_email tool, and reply_to_thread writes a draft. A tool that wrote a draft returns its id, its revision and a link to the dashboard, where a person reads the draft and sends it.

ts
// Only the tools this job needs.
smtpfastTools(smtpfast, { inbox: "[email protected]", tools: ["get_thread", "reply_to_thread"] });

// Let the agent send without a person in the loop.
smtpfastTools(smtpfast, { inbox: "[email protected]", allowDirectSend: true });

The API key is the real limit. Writing drafts needs only inbound:read; sending a draft needs inbound:read and email:send. Give the agent a key without email:send, and the server refuses every send, whatever the model does. See Inboxes and API keys.

Errors the model can read

#

When the API refuses a call, the tool does not throw. It returns the reason, so the model can tell the user or change course:

json
{
  "ok": false,
  "error": {
    "status": 403,
    "code": "forbidden",
    "message": "API key does not have email:send scope",
    "hint": "The API key does not allow this. If sending was refused, write a draft instead."
  }
}
Status and codeWhen
403 forbiddenThe key lacks a scope, for example email:send. The hint tells the model to write a draft instead.
404 not_foundA wrong thread or message id.
429 rate_limitedToo many requests, with retry_after_seconds when the API sent Retry-After.
403 inbox_not_allowedThe key is limited to other inboxes, or the From is not one of its inboxes.
403 recipient_not_allowedA recipient is not on the key's allowlist. recipients lists the refused addresses.
429 key_send_limitThe key reached its own hourly or daily send limit, with retry_after_seconds.
403 key_restrictedThe key's limits do not allow this action at all, for example a key limited to inboxes reading contacts.
connection_error, timeoutThe API could not be reached. status is null.

Untrusted email content

#

Anyone can email the inbox, so a message can say "ignore your instructions". The tools limit what that text can do:

  • Subjects, previews, bodies and the full From, To, Cc and Reply-To headers go inside <untrusted_email_content id="..."> blocks, with a new random id on every call. The tag name is replaced wherever it appears in the content, so an email cannot close the block.
  • Outside the blocks, the tools return bare email addresses only. A header value that is not a plain address is left out.
  • Each result that has email content carries a security_note telling the model to treat the blocks as data.
  • get_thread adds warnings when SPF, DKIM or DMARC fail, the spam scan flags the message, it is quarantined, or replies go to a Reply-To other than the From.
  • HTML-only bodies are turned into plain text without scripts, styles or comments, and long bodies are cut to maxBodyChars.

This lowers the risk of prompt injection; it does not remove it. Keep a person in the loop for sends, and give the agent only the tools and scopes its job needs.

Options

#
OptionWhat it does
inboxRequired. The inbox the tools read and write, by id or address.
allowDirectSendfalse by default. When true, send_email is added and reply_to_thread sends at once.
fromThe From of send_email. Defaults to the inbox's address and name.
toolsOnly these tools, by name. Defaults to every tool the send mode allows.
maxBodyCharsCharacters of each message body the model sees. Defaults to 8000.
maxMessagesHow many recent messages get_thread returns. Defaults to 20, at most 100.
dashboardUrlWhere a person reviews drafts. Defaults to https://smtpfa.st.