AI Tools & MCP
First-class support for AI agents. Use the built-in MCP server, LangChain tools, LlamaIndex functions, or OpenAI function definitions to give any LLM email superpowers.
MCP Server (Claude, Cursor, Windsurf)
The Mailgrid MCP server lives at packages/mcp/ and implements the
Model Context Protocol
as a JSON-RPC 2.0 stdio bridge. Install it once and every MCP-compatible host
(Claude Code, Claude Desktop, Cursor, Windsurf) can drive your email stack directly.
Available tools
The server exposes 18 tools:
# Core sending send_email — send a single transactional email send_batch — send up to 50 emails in one call schedule_email — schedule delivery at a future UTC timestamp smart_send_at — AI-optimised send-time per recipient # Templates create_template — save a Handlebars template generate_template — generate HTML email from a plain-text brief # Domains & infrastructure verify_domain — trigger DKIM/SPF verification for a domain register_dedicated_ip — provision a warm dedicated IP create_stream — create a sending stream (transactional / marketing) # Events & analytics get_event_log — fetch recent delivery events with filters get_analytics — open / click / bounce rates for a date range # AI utilities summarize — summarise an email thread or body ask_ai — ask an open question about your email data auto_label — classify & label an incoming message # Contacts & team list_contacts — list / search contacts with pagination invite_user — invite a team member to your workspace # Files upload_file — upload an attachment (base64 or URL) # Utilities ping — health-check the MCP bridge
Claude Code (.mcp.json)
Add to .mcp.json in your project root (or ~/.claude/mcp.json globally):
{
"mcpServers": {
"mailgrid": {
"command": "npx",
"args": ["-y", "@mailgrid/mcp"],
"env": {
"MAILGRID_API_KEY": "mb_live_..."
}
}
}
}
Cursor (.cursor/mcp.json)
{
"mcpServers": {
"mailgrid": {
"command": "npx",
"args": ["-y", "@mailgrid/mcp"],
"env": {
"MAILGRID_API_KEY": "mb_live_..."
}
}
}
}
Claude Desktop (claude_desktop_config.json)
On macOS the config lives at
~/Library/Application Support/Claude/claude_desktop_config.json:
{
"mcpServers": {
"mailgrid": {
"command": "npx",
"args": ["-y", "@mailgrid/mcp"],
"env": {
"MAILGRID_API_KEY": "mb_live_..."
}
}
}
}
Restart Claude Desktop after saving. You'll see a hammer icon confirming the server is connected.
LangChain (Python)
Source: packages/sdk-langchain/. Install from PyPI:
pip install mailgrid-langchain
Three individual tools are available for fine-grained control:
SendEmailTool— send a single email with full header supportGetEmailStatsTool— retrieve open / click / bounce metricsCreateContactTool— upsert a contact into your list
Use MailgridToolkit to hand all three tools to a ReAct agent in one call:
from langchain.agents import create_react_agent, AgentExecutor from langchain_anthropic import ChatAnthropic from mailgrid_langchain import MailgridToolkit toolkit = MailgridToolkit( api_key="mb_live_...", from_email="you@yourdomain.com", ) llm = ChatAnthropic(model="claude-sonnet-4-6") tools = toolkit.get_tools() agent = create_react_agent(llm, tools) executor = AgentExecutor(agent=agent, tools=tools, verbose=True) result = executor.invoke({ "input": "Send a welcome email to alice@example.com and report the open rate for last week." }) print(result["output"])
LlamaIndex (Python)
Source: packages/sdk-llamaindex/. Install from PyPI:
pip install mailgrid-llamaindex
MailgridAgentTools wraps three FunctionTool instances —
send_email, get_contacts, and get_analytics —
and returns them as a list ready for any LlamaIndex agent:
from llama_index.core.agent import ReActAgent from llama_index.llms.anthropic import Anthropic from mailgrid_llamaindex import MailgridAgentTools tools = MailgridAgentTools( api_key="mb_live_...", from_email="you@yourdomain.com", ).as_tools() llm = Anthropic(model="claude-sonnet-4-6") agent = ReActAgent.from_tools(tools, llm=llm, verbose=True) response = agent.chat( "How many contacts opened emails in the last 30 days? " "Send a re-engagement campaign to those who haven't opened in 14 days." ) print(response)
OpenAI Functions
Source: packages/sdk-openai-functions/. Install from npm:
npm install @mailgrid/openai-functions
The package exports a functions array (JSON Schema definitions) and an
executeFunction handler. Drop them directly into any OpenAI-compatible
chat completion loop. Six functions are included:
send_emailschedule_emailcreate_contactget_analyticslist_templatessend_template
import OpenAI from "openai"; import { functions, executeFunction } from "@mailgrid/openai-functions"; const client = new OpenAI(); const messages: OpenAI.Chat.ChatCompletionMessageParam[] = [ { role: "system", content: "You are an email assistant. Use the available tools to help the user.", }, { role: "user", content: "Send a confirmation email to bob@example.com and show me last week's stats.", }, ]; while (true) { const response = await client.chat.completions.create({ model: "gpt-4o", messages, tools: functions.map((f) => ({ type: "function", function: f })), tool_choice: "auto", }); const choice = response.choices[0]; messages.push(choice.message); if (choice.finish_reason === "stop") { console.log(choice.message.content); break; } for (const call of choice.message.tool_calls ?? []) { const result = await executeFunction( call.function.name, JSON.parse(call.function.arguments), { apiKey: process.env.MAILGRID_API_KEY! } ); messages.push({ role: "tool", tool_call_id: call.id, content: JSON.stringify(result), }); } }
Alerting integrations
Route delivery events to your existing on-call and chat stack. Each integration is a lightweight webhook receiver that translates Mailgrid events into native notifications.
| Integration | Package path | What it does |
|---|---|---|
| Slack | integrations/slack/ |
Posts bounce, complaint, and delivery-failure alerts to a Slack channel via Incoming Webhooks. |
| Discord | integrations/discord/ |
Sends embed messages to a Discord channel for configurable event types. |
| Microsoft Teams | integrations/teams/ |
Posts Adaptive Card alerts to a Teams channel via the webhook connector. |
| Telegram | integrations/telegram/ |
Bot alerts for bounces and complaints — configure a bot token and chat ID. |
| PagerDuty | integrations/pagerduty/ |
Opens an incident automatically when your bounce rate crosses a configurable threshold. |
| Datadog | integrations/datadog/ |
Ships custom metrics (mailgrid.sent, mailgrid.bounced, …) and structured logs. Includes a pre-built dashboard.json. |
All alerting integrations consume the same Mailgrid outbound webhook. Point it to your integration's receiver URL and set the shared secret as MAILGRID_WEBHOOK_SECRET in its environment.
Analytics
Push Mailgrid events into your product analytics pipeline so opens, clicks, and bounces show up alongside the rest of your funnel data.
| Integration | Package path | How it maps |
|---|---|---|
| Segment | integrations/segment/ |
onIdentify → upsert contact; onTrack → trigger email for matching event names. |
| PostHog | integrations/posthog/ |
Event export webhook — delivery events appear as PostHog events on the contact's person profile. |
| Mixpanel | integrations/mixpanel/ |
Cohort export syncs to contact lists; conversion events (email clicked → purchased) tracked automatically. |
| RudderStack | integrations/rudderstack/ |
Webhook destination — supports both cloud-mode and device-mode delivery of Mailgrid events. |