← Back to all posts
2026-08-10 · MailAnvil Team

How AI Agents Send Transactional Emails: MailAnvil MCP + LangChain Tutorial

AI agents can write code, query databases, and browse the web. But when your agent needs to send a real email — a booking confirmation, an OTP code, a payment receipt — it hits a wall. Most email APIs were built for human developers with dashboards, API keys in .env files, and manual setup.

MailAnvil's MCP server changes that. Your AI agent discovers it, authenticates, and sends email — no human in the loop.

In this tutorial, you'll connect a LangChain agent to MailAnvil MCP and have it send a booking confirmation email for a fictional Indonesian travel platform.

What is MCP?

Model Context Protocol (MCP) lets AI agents discover and use external tools — like email APIs — without hardcoded integrations. Think of it as "USB-C for AI tools." The agent queries available tools, sees what they do, and calls them.

MailAnvil exposes its full email API as an MCP server at mcp.mailanvil.com. Any MCP-compatible client (Claude Desktop, Cursor, LangChain, Mastra) can connect.

Prerequisites

Step 1: Get your API key

Sign up at mailanvil.com and grab your API key from the dashboard. The free tier gives you 500 emails/month — enough to build and test.

# Store it — your agent will use this
export MAILANVIL_API_KEY="ma_..."

Step 2: Install dependencies

npm install @langchain/core @langchain/openai @modelcontextprotocol/sdk

We'll use LangChain's MCP adapter to connect to MailAnvil's MCP server.

Step 3: Connect your agent to MailAnvil MCP

Create agent.ts:

import { ChatOpenAI } from "@langchain/openai";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import { Tool } from "@langchain/core/tools";

// Connect to MailAnvil MCP server
const transport = new StdioClientTransport({
  command: "npx",
  args: ["-y", "@mailanvil/mcp-server"],
  env: { MAILANVIL_API_KEY: process.env.MAILANVIL_API_KEY! },
});

const client = new Client(
  { name: "booking-agent", version: "1.0.0" },
  { capabilities: {} }
);

await client.connect(transport);

// Discover available tools
const tools = await client.listTools();
// tools = [
//   { name: "mailanvil_send", description: "Send a transactional email..." },
//   { name: "mailanvil_get_status", description: "Check email delivery status..." },
//   { name: "mailanvil_list_domains", description: "List verified domains..." },
// ]

// Convert MCP tools to LangChain tools
const langchainTools = tools.tools.map((t) => ({
  name: t.name,
  description: t.description,
  schema: t.inputSchema,
  func: async (input: any) => {
    const result = await client.callTool({
      name: t.name,
      arguments: input,
    });
    return result.content[0].text;
  },
}));

Step 4: Build the booking agent

Now extend the agent to handle a real scenario: a customer books a villa in Bali, and the agent sends the confirmation email.

import { AgentExecutor, createOpenAIToolsAgent } from "langchain/agents";
import { ChatPromptTemplate } from "@langchain/core/prompts";

const llm = new ChatOpenAI({ model: "gpt-4o", temperature: 0 });

const prompt = ChatPromptTemplate.fromMessages([
  ["system", `You are a booking agent for BaliVilla, an Indonesian travel platform.
When a booking is confirmed, send a transactional email to the customer.
Use the mailanvil_send tool. The from address must be [email protected].
Always include: booking ID, villa name, check-in/check-out dates, total price in IDR.`],
  ["human", "{input}"],
  ["placeholder", "{agent_scratchpad}"],
]);

const agent = await createOpenAIToolsAgent({ llm, tools: langchainTools, prompt });
const executor = new AgentExecutor({ agent, tools: langchainTools });

// Test it
const result = await executor.invoke({
  input: "New booking: ID BVL-2026-0891, Villa Sungai, Aug 15-18, 3 nights, Rp 4.500.000. Customer: [email protected]. Send confirmation."
});

console.log(result.output);
// "Sent booking confirmation BVL-2026-0891 to [email protected].
//  Email ID: em_01J5XK8N2P..."

Step 5: Run it

npx tsx agent.ts

Your AI agent just sent a real transactional email. It discovered MailAnvil's tools via MCP, understood the schema, and made the right API call — no hardcoded SDK, no manual integration.

What the agent actually did

Under the hood, client.callTool({ name: "mailanvil_send", arguments: {...} }) hit MailAnvil's API at api.mailanvil.com/v1/send with:

{
  "from": "[email protected]",
  "to": ["[email protected]"],
  "subject": "Booking Confirmation — BVL-2026-0891",
  "html": "<h1>Your Bali Villa is Confirmed!</h1>..."
}

The email was queued, processed through AWS SES (ap-southeast-1, Jakarta edge), and delivered. Your agent can now call mailanvil_get_status to track delivery.

Real Indonesian use cases

This pattern fits multiple verticals:

Travel (like Traveloka, Tiket.com): Booking confirmations, e-tickets, itinerary emails. Agent handles the template and sends automatically.

Food delivery (like GoFood, GrabFood): Order confirmations, driver assignment notifications, digital receipts. All triggered by the agent when order state changes.

Fintech (like OVO, Dana): OTP verification emails, transaction receipts, top-up confirmations. The agent calls MailAnvil when the auth service needs to send a code.

E-commerce (like Tokopedia, Shopee): Order shipped notifications, payment confirmed, review requests. Agent monitors order pipeline and emails at each stage.

Why MCP-native matters

Traditional email API integration:

  1. Read docs (30 min)
  2. Install SDK
  3. Configure API key in .env
  4. Write wrapper functions
  5. Handle errors, retries, rate limits
  6. Test with curl
  7. Deploy

MCP-native integration:

  1. Agent discovers tools automatically
  2. Agent reads schema, understands what each tool does
  3. Agent calls the right tool with the right arguments
  4. Done

Zero integration code. The agent figures it out.

Pricing: Why MailAnvil wins for Indonesian teams

Plan Volume MailAnvil (IDR) Resend (USD → IDR)
Free 500/mo Rp 0 Rp 0
Starter 10,000/mo Rp 149rb ~Rp 310rb ($20)
Growth 100,000/mo Rp 599rb ~Rp 1.5jt ($100)
Scale 1,000,000/mo Rp 2.9jt ~Rp 7.9jt ($500)

At 100K volume, MailAnvil is 2.5x cheaper than Resend. And you pay in rupiah via QRIS or GoPay — no currency conversion surprises.

What's next

Try MailAnvil free at mailanvil.com — 500 emails/month, no credit card, Rp 0.