Combine LlamaIndex's retrieval-augmented generation with MultiMail's email infrastructure to build agents that send context-aware, data-grounded emails.
LlamaIndex is the leading data framework for connecting LLMs to external data sources. It specializes in indexing, retrieval, and RAG patterns, making it ideal for agents that need to reference documents, knowledge bases, or prior emails when composing responses. MultiMail provides the email infrastructure layer that LlamaIndex agents need to actually send, receive, and manage messages.
By integrating MultiMail with LlamaIndex, your agents can retrieve relevant context from your data sources, compose informed emails, and send them through managed mailboxes — all while respecting human oversight boundaries. The monitored mode is particularly useful for RAG-powered auto-replies where you want visibility into what the agent sends without blocking every message.
Connect LlamaIndex to MultiMail by defining tool specs that wrap the REST API, or use the @multimail/mcp-server for automatic tool discovery. Both approaches integrate cleanly with LlamaIndex's ReAct agent pattern.
LlamaIndex retrieves relevant context from your knowledge base before composing emails. MultiMail handles the delivery infrastructure, ensuring that RAG-powered responses reach recipients reliably.
RAG-powered emails may contain hallucinated or incorrect information. MultiMail's gated_send mode lets humans review AI-generated emails before delivery, catching factual errors that retrieval alone cannot prevent.
Use LlamaIndex to index your email history from MultiMail's API, then query it for context when composing new messages. Past email threads become part of your retrieval corpus for more contextual replies.
LlamaIndex's ReAct agent can use MultiMail tools alongside retrieval tools. The agent reasons about when to search your knowledge base and when to send an email, combining both capabilities in a single workflow.
Combine MultiMail's search_contacts and add_contact tools with LlamaIndex's retrieval to build agents that know who to email and what context is relevant for each contact.
import requests
from llama_index.core.tools import FunctionTool
MULTIMAIL_API = "https://api.multimail.dev/v1"
HEADERS = {"Authorization": "Bearer mm_live_your_api_key"}
def send_email(to: str, subject: str, body: str, mailbox_id: str) -> str:
"""Send an email through MultiMail. In gated_send mode, this queues for human approval."""
resp = requests.post(f"{MULTIMAIL_API}/send", headers=HEADERS, json={
"mailbox_id": mailbox_id, "to": to,
"subject": subject, "body": body
})
return str(resp.json())
def check_inbox(mailbox_id: str, limit: int = 10) -> str:
"""Check the inbox for recent messages."""
resp = requests.get(
f"{MULTIMAIL_API}/mailboxes/{mailbox_id}/inbox",
headers=HEADERS, params={"limit": limit}
)
return str(resp.json())
def reply_email(message_id: str, body: str) -> str:
"""Reply to an email by message ID, maintaining thread context."""
resp = requests.post(f"{MULTIMAIL_API}/reply", headers=HEADERS, json={
"message_id": message_id, "body": body
})
return str(resp.json())
send_tool = FunctionTool.from_defaults(fn=send_email)
inbox_tool = FunctionTool.from_defaults(fn=check_inbox)
reply_tool = FunctionTool.from_defaults(fn=reply_email)Create LlamaIndex FunctionTool wrappers around MultiMail API endpoints.
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.agent import ReActAgent
from llama_index.llms.openai import OpenAI
"cm"># Index your knowledge base
documents = SimpleDirectoryReader("./knowledge_base").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
"cm"># Create a query tool for context retrieval
from llama_index.core.tools import QueryEngineTool
knowledge_tool = QueryEngineTool.from_defaults(
query_engine=query_engine,
name="knowledge_base",
description="Search the knowledge base for information to include in emails."
)
"cm"># Build the agent with both retrieval and email tools
llm = OpenAI(model="gpt-4o")
agent = ReActAgent.from_tools(
tools=[knowledge_tool, send_tool, inbox_tool, reply_tool],
llm=llm,
verbose=True,
system_prompt="You are an email assistant. Search the knowledge base for "
"context before composing emails. The mailbox uses gated_send mode, so "
"sent emails require human approval before delivery."
)
response = agent.chat("Reply to the latest email about pricing with our current rates")Create a LlamaIndex ReAct agent that retrieves context from a knowledge base before composing and sending emails.
from llama_index.core import Document, VectorStoreIndex
def fetch_email_history(mailbox_id: str, limit: int = 100) -> list:
"""Fetch email history from MultiMail and create LlamaIndex documents."""
resp = requests.get(
f"{MULTIMAIL_API}/mailboxes/{mailbox_id}/inbox",
headers=HEADERS, params={"limit": limit}
)
emails = resp.json().get("emails", [])
documents = []
for email in emails:
doc = Document(
text=f"From: {email[&"cm">#039;from']}\nTo: {email['to']}\n"
f"Subject: {email[&"cm">#039;subject']}\n\n{email['body']}",
metadata={
"message_id": email["message_id"],
"date": email["date"],
"from": email["from"]
}
)
documents.append(doc)
return documents
# Index email history
email_docs = fetch_email_history("your_mailbox_id")
email_index = VectorStoreIndex.from_documents(email_docs)
email_query = email_index.as_query_engine()
email_context_tool = QueryEngineTool.from_defaults(
query_engine=email_query,
name="email_history",
description="Search past email conversations for context and prior commitments."
)Fetch email history from MultiMail and index it with LlamaIndex for context-aware future replies.
import subprocess
import json
"cm"># The MCP server can be used alongside LlamaIndex tools
"cm"># Start the MCP server process
process = subprocess.Popen(
["npx", "-y", "@multimail/mcp-server"],
env={"MULTIMAIL_API_KEY": "mm_live_your_api_key"},
stdin=subprocess.PIPE,
stdout=subprocess.PIPE
)
"cm"># For LlamaIndex, the recommended approach is FunctionTool wrappers
"cm"># around the REST API, as shown in the previous examples.
"cm"># The MCP server is ideal for MCP-native clients.
"cm"># Combine both approaches: MCP for discovery, FunctionTool for execution
agent = ReActAgent.from_tools(
tools=[send_tool, inbox_tool, reply_tool, knowledge_tool],
llm=llm,
verbose=True
)
response = agent.chat("Check my inbox and draft replies using knowledge base context")Use the MultiMail MCP server with LlamaIndex for automatic tool discovery.
Sign up at multimail.dev, create a mailbox, and generate an API key from your dashboard. Your key will start with mm_live_.
Install LlamaIndex core, an LLM provider, and requests for calling the MultiMail API.
pip install llama-index llama-index-llms-openai requestsWrap MultiMail API endpoints as LlamaIndex FunctionTool objects for send_email, check_inbox, and reply_email.
Create a ReActAgent with your email tools and optionally a knowledge base query tool for RAG-powered email composition.
agent = ReActAgent.from_tools(
tools=[send_tool, inbox_tool, reply_tool],
llm=OpenAI(model="gpt-4o"), verbose=True
)If your mailbox uses gated_send mode (the default), review and approve pending emails in the MultiMail dashboard before they are delivered.
Email infrastructure built for AI agents. Verifiable identity, graduated oversight, and a 38-tool MCP server. Formally verified in Lean 4.