An email inbox for a Pydantic AI agent

A Pydantic AI agent gets email by adding MCPToolset for https://api.trotta.email/mcp with an API key. Wrap the toolset in approval_required so sending tools stop for your yes or no, and set output_type to a model of your own to get each handled email back as validated data. Mail is screened before delivery.

Updated

How do I connect Pydantic AI to an inbox?

MCPToolset takes the server URL and your headers, and goes into toolsets like any other. Create an inbox first: one call, no account.

shell
pip install pydantic-ai
curl -X POST https://api.trotta.email/v1/agents/signup
export TROTTA_MAIL_API_KEY=...   # api_key from the answer, shown once
export ANTHROPIC_API_KEY=...     # or switch the model string

Approve sends, return a typed report

Two lines do the work. approval_required picks the tools that must stop. output_type lists your Triage model and DeferredToolRequests, so a run ends with either a finished report or a send waiting for you.

agent.py
import asyncio
import os

from pydantic import BaseModel
from pydantic_ai import Agent, DeferredToolRequests, DeferredToolResults
from pydantic_ai.mcp import MCPToolset

SENDS = {"send_message", "reply_to_message", "answer_invitation"}

trotta = MCPToolset(
    "https://api.trotta.email/mcp",
    headers={"Authorization": f"Bearer {os.environ['TROTTA_MAIL_API_KEY']}"},
    include_instructions=True,  # the server tells the model that mail is data
).approval_required(lambda ctx, tool, args: tool.name in SENDS)

class Triage(BaseModel):
    message_id: str
    sender: str
    asks_for: str
    replied: bool

agent = Agent(
    "anthropic:claude-sonnet-5-5",
    toolsets=[trotta],
    output_type=[Triage, DeferredToolRequests],
)

async def main():
    result = await agent.run(
        "Find my inbox, wait for the next email, read it, reply in one line and report."
    )
    while isinstance(result.output, DeferredToolRequests):
        decisions = DeferredToolResults()
        for call in result.output.approvals:
            print(call.tool_name, call.args_as_dict())
            decisions.approvals[call.tool_call_id] = input("Send? [y/N] ") == "y"
        result = await agent.run(
            message_history=result.all_messages(),
            deferred_tool_results=decisions,
        )
    print(result.output)

asyncio.run(main())

Run it and send a mail to the agent's address. The agent waits, reads and writes a reply, then the script prints the exact arguments and asks. The last line printed is a Triage object. If you answer no, the model is told the call was denied and nothing leaves.

What the typed result is for

Validation checks the shape of an answer, not its truth. A sender field filled by the model is still the model's reading of an email. When code acts on it, take the facts from the message itself: from and verdict come back on every read_message result.

Keep the model narrow. A field such as new_bank_details invites a stranger's email to fill it, and your code to trust it.

Which tools should need approval?

ToolEffectApprove?
send_messageStarts a new emailYes
reply_to_messageAnswers in the threadYes
answer_invitationSends an answer to the organizerYes
create_draftSaves a draft, sends nothingOptional
wait_for_message, read_messageRead onlyNo

This gate runs in your process. Outgoing mail is also screened by Trotta Mail, and until a human claims the agent it replies only to senders whose mail arrived authenticated and writes to its own organization's inboxes, 50 sends a day.

What reaches the model

read_message returns the safe view with a verdict: hidden text removed, instructions aimed at a model marked, risky links defanged. Held mail is withheld from the agent until a human releases it. Words the sender wrote sit inside tags ending in a code that is new on each result.

include_instructions is off by default on MCPToolset. The sample turns it on so the server's short instructions reach the model. No filter catches everything, so keep the approval.

MCPToolset reads for up to five minutes by default, so a 60 second wait_for_message needs no extra setting.

Questions

How do I add an email tool to a Pydantic AI agent?

Pass MCPToolset with the Trotta Mail URL and an Authorization header in toolsets. The agent gets tools to create and list inboxes, wait for mail, read, reply, send and draft.

Can I require approval for one MCP tool and not the others?

Yes. approval_required takes a function of the run context, the tool definition and the arguments. Return True for the tools that send, or inspect the recipients and ask only for new addresses.

Can I test the agent without spending model tokens?

Yes. Pydantic AI ships TestModel and FunctionModel for tests. The email side can run against a second inbox of your own, so no outside mail is involved.

Do I need a Trotta Mail package for Pydantic AI?

No. MCPToolset connects to the remote server. If you prefer function tools of your own, wrap the trottamail Python SDK in agent.tool_plain.

Sources

Give your agent an inbox of its own. One call, or one minute in the console.