Vercel AI SDK
Getting started
The Vercel AI SDK is the most widely used TypeScript toolkit for building AI agents. The agentmail-toolkit package exposes the full AgentMail API as AI SDK tools, so an agent built with generateText, streamText, or ToolLoopAgent can create inboxes, send, receive, reply, search, and manage threaded email with no glue code.
To give your AI SDK agent an email inbox, install agentmail-toolkit, import AgentMailToolkit from agentmail-toolkit/ai-sdk, and pass new AgentMailToolkit().getTools() as the tools option. The agent can then call create_inbox to provision its own address and use the other tools to send and read mail from it.
Use cases
- Give agents their own inboxes: Provision a dedicated email address per agent so it can send and receive mail independently.
- Triage and reply: Read recent threads and reply inside the same thread, with threading handled automatically.
- Stage and send: Compose drafts iteratively and send them when they’re ready.
- Inbound-driven agents: Pair the toolkit with AgentMail webhooks so your agent reacts the moment mail arrives.
Prerequisites
- An AgentMail account with an API key from the AgentMail Console.
- Node.js 18+ and the AI SDK (
aiv6 or later).
Setup
Install the toolkit alongside the AI SDK and Zod:
Set your API key:
Quickstart
Pass the toolkit’s tools to generateText and let the agent loop through tool calls:
On ai v6, import stepCountIs instead of isStepCount; it was renamed in v7.
Plain model strings such as 'anthropic/claude-sonnet-4-5' are routed through the Vercel AI Gateway. To call a provider directly instead, pass a model from its AI SDK provider package, for example openai('gpt-4o') from @ai-sdk/openai.
The same tools work with streamText and ToolLoopAgent:
Configuring the toolkit
Authentication
new AgentMailToolkit() reads AGENTMAIL_API_KEY from the environment. For custom configuration, pass your own AgentMailClient from the agentmail SDK:
Selecting tools
getTools() returns an object keyed by tool name, ready for the tools option of generateText, streamText, or ToolLoopAgent. Pass an array of names to get only that subset:
Production agents should usually pass only the tools they need. A smaller tool set keeps the model focused and limits what it can do, for example leaving out delete_inbox and delete_thread.
Tool names are snake_case (send_message), but tool parameters are camelCase (inboxId, threadId). An inboxId is the inbox’s full email address, such as my-agent@agentmail.to.
Error handling
A failed tool call throws, which the AI SDK surfaces as a native tool-error part. The model can distinguish it from a successful result and decide whether to retry or change course.
Available tools
The toolkit exposes more than 40 tools covering the AgentMail API. The main ones are below.
Inboxes
Messages
Threads
Drafts
Attachments
Additional tools cover files, allow and block lists, and the rest of the API. Call getTools() and inspect the returned keys for the full list.
Resources
- Source code: github.com/agentmail-to/agentmail-toolkit
- npm: npmjs.com/package/agentmail-toolkit
- Vercel AI SDK docs: ai-sdk.dev
- Other frameworks: The same package exposes LangChain and MCP bindings through
agentmail-toolkit/langchainandagentmail-toolkit/mcp. See the MCP integration for a hosted server.
