Back to Blog
/5 min read/By SMTPfast Team

How to Send Email From a Vercel AI SDK Agent With MCP Tools

Load the email API's MCP tools with createMCPClient from @ai-sdk/mcp, pass only the tools the job needs to generateText, and set a toolApproval policy that runs read-only tools and asks a person before anything else. A send then comes back as a tool-approval-request; you answer it with a tool-approval-response and call generateText again. We ran the script below against the SMTPfast MCP server with ai 7.0.184 on 28 November 2026.

ai-agentsmcpnodejstypescripttutorial
Share:𝕏in

The short answer: load the email API's MCP tools with createMCPClient, pass only the ones the job needs, and set a toolApproval policy so a person approves each send. Read-only tools run on their own; a send comes back as a tool-approval-request until someone answers it.

We ran the script below against the SMTPfast MCP server on 28 November 2026, with ai 7.0.184 and @ai-sdk/mcp 2.0.106, using the AI SDK's mock model in place of the OpenAI call. list_domains ran without a question, the run stopped at send_email, and a denied send was not sent. We build SMTPfast; any email API with a Streamable HTTP MCP server works the same way.

For everything SMTPfast offers agents on one page (the MCP server, inboxes, drafts a person approves, REST and SMTP), see email API for AI agents.

What you need

  • Node.js 22 or later (the current ai packages require it) and npm install ai @ai-sdk/mcp @ai-sdk/openai.
  • An OPENAI_API_KEY, or any other provider the AI SDK supports.
  • An SMTPfast account with a verified sending domain, and an API key with the scopes the agent needs: email:send, email:read and domain:read here. Create it under API keys in the dashboard; the free plan works.

The script

// npm install ai @ai-sdk/mcp @ai-sdk/openai
import readline from "node:readline/promises";
import { generateText, stepCountIs } from "ai";
import { createMCPClient } from "@ai-sdk/mcp";
import { openai } from "@ai-sdk/openai";

const smtpfast = await createMCPClient({
  transport: {
    type: "http",
    url: "https://smtpfa.st/api/mcp",
    headers: { Authorization: `Bearer ${process.env.SMTPFAST_API_KEY}` },
  },
});

try {
  const all = await smtpfast.tools();
  // Give the model only the tools this job needs.
  const tools = { list_domains: all.list_domains, check_before_send: all.check_before_send, send_email: all.send_email };
  // Run tools the server marks read-only; ask a person before anything else.
  const toolApproval = ({ toolCall }) =>
    toolCall.toolMetadata?.annotations?.readOnlyHint === true ? "not-applicable" : "user-approval";

  const messages = [
    { role: "user", content: "Email [email protected] from [email protected] that the nightly backup finished." },
  ];
  const run = () => generateText({ model: openai("gpt-5-mini"), tools, toolApproval, stopWhen: stepCountIs(5), messages });

  let result = await run();
  let approvals = result.content.filter((part) => part.type === "tool-approval-request");
  const rl = readline.createInterface({ input: process.stdin, output: process.stdout });

  while (approvals.length > 0) {
    const answers = [];
    for (const request of approvals) {
      console.log(request.toolCall.toolName, JSON.stringify(request.toolCall.input));
      const yes = (await rl.question("Send it? [y/N] ")).trim().toLowerCase() === "y";
      answers.push({ type: "tool-approval-response", approvalId: request.approvalId, approved: yes });
    }
    messages.push(...result.response.messages, { role: "tool", content: answers });
    result = await run();
    approvals = result.content.filter((part) => part.type === "tool-approval-request");
  }

  rl.close();
  console.log(result.text);
} finally {
  await smtpfast.close();
}

Save it as mailer.mjs and run it:

export SMTPFAST_API_KEY=sf_...
export OPENAI_API_KEY=sk-...
node mailer.mjs

When the model decides to send, you see the full input first:

send_email {"from":"[email protected]","to":"[email protected]","subject":"...","text":"..."}
Send it? [y/N]

How the pieces work

  • createMCPClient connects to the hosted server over Streamable HTTP (type: "http"). The key goes in the Authorization header and stays in your process.
  • smtpfast.tools() turns the server's tools into AI SDK tools. The SMTPfast server has 109; this job needs three. Passing a short list costs fewer tokens and leaves the model fewer wrong choices.
  • toolApproval decides per call. @ai-sdk/mcp puts the server's annotations on toolCall.toolMetadata, and SMTPfast marks every tool that sends, deletes or changes something as not read-only. So lookups run, and everything else waits for a person.
  • The approval round trip: the paused call comes back as a tool-approval-request part. You add the response messages and a tool message with one tool-approval-response per request, then call generateText again. An approved call runs; a denied one is reported to the model as denied and never reaches the API.
  • stepCountIs(5) lets the model call a tool and then use its result, up to five steps per call. In ai 7 the same function is also exported as isStepCount.

The AI SDK docs describe annotations as untrusted hints from the server. Here they only decide when to ask; the tool list and the API key's scopes are the hard limits.

Make it safer

  1. Scope the key. The agent can do whatever its key allows. For notifications, email:send and domain:read are enough. A key can never create keys with more access than itself.
  2. Draft instead of send. For replies to people, pass save_inbox_draft instead of send_email, with a key that has inbound:read but no email:send. The server then refuses any send, and a person sends the draft. See how to build an AI agent that answers support email.
  3. Check before sending. check_before_send tests the sender domain, the content and the links. Tell the model to call it first.
  4. Close the client. Call close() when the job is done. In a route handler with streamText, close it when the stream ends, as the AI SDK docs show.

Need the whole API?

If the agent needs most of the API, connect to https://smtpfa.st/api/mcp/compact instead. It has two tools, search_api and call_api, in place of the full list (about 650 tokens of tool definitions instead of about 34,000). With an approval policy on call_api, every call asks, reads included. We measured the trade-off in 109 MCP tools vs 2.

Sources

Checked on 28 November 2026:

Frequently asked questions

How do I let a Vercel AI SDK agent send email?

Create an MCP client with createMCPClient from @ai-sdk/mcp, using transport { type: 'http', url: 'https://smtpfa.st/api/mcp', headers: { Authorization: 'Bearer <key>' } }. Call client.tools() and pass the tools you want (for example send_email and list_domains) to generateText or streamText. Close the client when you are done.

How do I make the AI SDK ask before it sends an email?

Pass a toolApproval policy to generateText or streamText. Returning 'user-approval' for a tool makes the call come back as a tool-approval-request part instead of running. Send back a tool message with a tool-approval-response (approvalId and approved true or false) and call generateText again. A denied call never reaches the email API. In ai 7 you can also set needsApproval: true on a single tool.

Can I use the MCP tool annotations to decide what needs approval?

Yes. @ai-sdk/mcp passes the server's annotations on toolCall.toolMetadata. SMTPfast marks lookups as read-only and sends, deletes and changes as not read-only, so a policy that runs only readOnlyHint tools and asks about the rest asks before every send. The AI SDK docs call annotations untrusted hints, so combine them with a tool list you choose and a scoped API key.

Does this work in a Next.js route handler?

Yes. The same client and tools work in a route handler with streamText. Create the MCP client per request, close it when the stream ends, and handle tool-approval-request parts in your UI. Keep the SMTPfast API key on the server; never send it to the browser.

Ready to get started?

Start sending transactional email today. Free to start, no credit card required.

Start free