Extend OpenAI's official agent framework with MultiMail's email infrastructure — complete with guardrails, handoffs, and human oversight for every message.
The OpenAI Agents SDK is OpenAI's official framework for building agentic applications. It features a minimal, opinionated API with built-in support for handoffs between agents, guardrails for input/output validation, and tracing for observability. MultiMail provides the email infrastructure layer that these agents need to interact with the real world through email.
By integrating MultiMail with the OpenAI Agents SDK, your agents can send emails, check inboxes, manage contacts, and handle threads — all while benefiting from both OpenAI's guardrails and MultiMail's email-specific oversight. The SDK's guardrails validate agent outputs structurally, while MultiMail's gated_send mode adds human judgment before delivery.
Connect your agents to MultiMail by defining function tools that call the REST API. The SDK's tool-calling pattern makes it straightforward to register MultiMail operations as agent capabilities.
The SDK's guardrails validate agent outputs but cannot catch email-specific risks like wrong recipients or inappropriate tone. MultiMail's oversight modes add a human review layer specifically for email, complementing OpenAI's structural validation.
Use the SDK's handoff feature to route between specialized email agents. A triage agent can hand off to a customer support agent or an escalation agent, each with their own email handling logic and MultiMail mailbox.
The SDK's tracing system records every tool call, including MultiMail email operations. Combined with MultiMail's audit log, you get end-to-end observability from agent reasoning through email delivery.
The OpenAI Agents SDK is designed for simplicity. Registering MultiMail tools requires just a few function definitions with type hints — the SDK handles tool schema generation, invocation, and response parsing automatically.
Start agents in gated_send mode where every email requires human approval. Progress to monitored or autonomous as the agent proves reliable. MultiMail's five oversight levels let you calibrate trust precisely.
import requests
from agents import function_tool
MULTIMAIL_API = "https://api.multimail.dev/v1"
HEADERS = {"Authorization": "Bearer mm_live_your_api_key"}
@function_tool
def send_email(to: str, subject: str, body: str, mailbox_id: str) -> str:
"""Send an email through MultiMail. In gated_send mode, 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())
@function_tool
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())
@function_tool
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())Create function tools that the OpenAI Agents SDK can invoke to interact with MultiMail.
from agents import Agent, Runner, GuardrailFunctionOutput, InputGuardrail
from pydantic import BaseModel
class EmailSafetyCheck(BaseModel):
is_safe: bool
reasoning: str
guardrail_agent = Agent(
name="Email Safety",
instructions="Check if the user&"cm">#039;s email request is appropriate. "
"Flag requests to send spam, phishing, or abusive content.",
output_type=EmailSafetyCheck
)
async def email_guardrail(ctx, agent, input_text):
result = await Runner.run(guardrail_agent, input_text, context=ctx.context)
return GuardrailFunctionOutput(
output_info=result.final_output,
tripwire_triggered=not result.final_output.is_safe
)
email_agent = Agent(
name="Email Assistant",
instructions="You are an email assistant using MultiMail. The mailbox "
"uses gated_send mode, so sent emails queue for human approval. "
"Use the provided tools to check inbox, send, and reply to emails.",
tools=[send_email, check_inbox, reply_email],
input_guardrails=[InputGuardrail(guardrail_function=email_guardrail)]
)
result = await Runner.run(email_agent, "Check my inbox and summarize new messages")
print(result.final_output)Create an agent with email tools and a guardrail that validates email content before sending.
from agents import Agent, Runner
triage_agent = Agent(
name="Triage",
instructions="You triage inbound emails. Check the inbox and classify "
"each email. Hand off urgent emails to the Urgent Responder and "
"routine emails to the Auto Responder.",
tools=[check_inbox],
handoffs=["urgent_responder", "auto_responder"]
)
urgent_responder = Agent(
name="Urgent Responder",
instructions="You handle urgent emails. Draft thoughtful replies "
"using reply_email. These will go through gated_send approval.",
tools=[reply_email, send_email]
)
auto_responder = Agent(
name="Auto Responder",
instructions="You handle routine emails with standard replies. "
"Use reply_email to respond with appropriate templates.",
tools=[reply_email]
)
"cm"># The SDK handles handoffs automatically based on agent decisions
result = await Runner.run(triage_agent, "Process today&"cm">#039;s inbox")
print(result.final_output)Use the SDK's handoff feature to route between specialized email agents.
Sign up at multimail.dev, create a mailbox, and generate an API key. Your key will start with mm_live_.
Install the OpenAI Agents SDK and requests for calling the MultiMail API.
pip install openai-agents requestsCreate functions decorated with @function_tool that wrap MultiMail API endpoints. Define at minimum send_email and check_inbox.
Build an Agent with your email tools, a system prompt explaining the email context, and optionally input guardrails for content validation.
agent = Agent(
name="Email Assistant",
instructions="You manage email via MultiMail in gated_send mode.",
tools=[send_email, check_inbox, reply_email]
)Execute the agent with Runner.run(). Review and approve pending emails in the MultiMail dashboard when using gated_send mode.
result = await Runner.run(agent, "Check my inbox")Email infrastructure built for AI agents. Verifiable identity, graduated oversight, and a 38-tool MCP server. Formally verified in Lean 4.