Email 2FA in browser agents: Playwright MCP and Browser Use
Published · Updated
A browser agent stalls at "enter the code we emailed you" because it has no inbox to read. The fix is to give it one it controls, plus a tool that blocks until the email arrives and returns the code as a field. With Mail for Bots that is create_inbox, then wait_for_message, then reading extracted.codes.
The same pattern works with any browser driver, including Playwright MCP, because the inbox is just another set of tools the agent can call. Over MCP the agent finds these tools with search_tools and runs them via call_tool. The browser types; the mail server waits.
Why does a browser agent get stuck on email codes?
Clicking and typing are easy for an agent. Receiving is not: the code goes to an address the agent cannot open. Common workarounds are worse than the problem. A human forwards the code from a personal inbox, or the agent polls a shared mailbox and parses raw HTML. Both are slow, and the second one lets stray mail steer the run.
A dedicated inbox removes the human from the loop and keeps the code out of your personal mail.
What is the flow for a sign-up with an emailed code?
- The agent calls
create_inbox(optionally withttl_minutesso it deletes itself) and gets back an address. - It types that address into the site's sign-up form in the browser.
- It calls
wait_for_message, filtering withfrom_containsorsubject_containsso only the right sender counts. The call returns as soon as the email arrives, or returns null on timeout. It returns the first unread matching message and marks it read; useget_messageto read it again. - It reads
extracted.codes(or a verify link fromextracted.links) from the returned message. - It types the code back into the browser and continues.
If you drive the same flow from your own script instead of MCP, the REST equivalents are POST /inboxes and GET /inboxes/:inbox/wait:
# 1. create a throwaway inbox (returns the new inbox, including its address)
curl -X POST "$BASE/api/v1/inboxes" \
-H "Authorization: Bearer $BOTMAIL_KEY" -H "Content-Type: application/json" \
-d '{ "ttl_minutes": 30 }'
# 2. type the returned address into the site, then block for the email
curl "$BASE/api/v1/inboxes/INBOX_ID/wait?from_contains=example.com&timeout_seconds=90" \
-H "Authorization: Bearer $BOTMAIL_KEY"
# 200: the full message, including extracted.codes and extracted.links
# 408: nothing arrived in time, retryHow do I pair it with Playwright MCP?
Put both servers in one MCP client config. Playwright MCP owns the page: navigate, fill, click. Mail for Bots owns the mailbox: create, wait, read. The model decides when to switch between them, and neither server needs to know about the other.
{
"mcpServers": {
"playwright": { "command": "npx", "args": ["@playwright/mcp@latest"] },
"mailforbots": {
"url": "<MCP URL from the setup page>",
"headers": { "Authorization": "Bearer YOUR_API_KEY" }
}
}
}Exact config syntax differs by client; the MCP email server page lists it for each one. Browser Use can also load MCP servers or call REST; check its docs for the config syntax.
| Step | Server | Tool |
|---|---|---|
| Make an address | Mail for Bots | create_inbox |
| Fill the sign-up form | Browser (e.g. Playwright MCP) | browser_navigate, browser_type, browser_click |
| Wait for the email | Mail for Bots | wait_for_message |
| Enter the code | Browser (e.g. Playwright MCP) | browser_type, browser_click |
| Clean up | Mail for Bots | delete_inbox |
What does the agent get back?
wait_for_message and get_message return the full message (with an attachments list and an extracted object). wait_for_message only matches unread inbound messages and marks the one it returns as read. extracted.codes holds one-time codes (4 to 8 digits, split codes like 482-913, and 6 to 8 character uppercase alphanumeric codes containing a digit). extracted.links holds action links labelled by kind: verify, reset, login, unsubscribe or other.
So the model reads a field instead of guessing which number in the body is the code. Plus-addressing lets one inbox serve many sites: filter to_contains on the tag, such as +github. See plus-addressing.
What are the limits?
- CAPTCHAs are not solved. Mail for Bots handles the email step only; a CAPTCHA before or after it still needs a human or another approach.
- Some sites reject disposable or shared domains. On Pro you can create inboxes on your own custom domain, which many sites accept, though none is guaranteed.
- Code extraction is pattern-based. If a site sends a code in an unusual format, fall back to reading the message body.
wait_for_messagewaits up to 300 seconds (default 60). Slow senders need a retry.- Respect each site's terms. This is for real workflows, not mass account creation.
Plans: Free includes 3 inboxes; Pro is $20 a month with 100 inboxes and up to 10 custom domains. See pricing. If you prefer event-driven delivery, Mail for Bots also supports webhooks. For other options see best email for AI agents.
How do I keep a browser agent safe with inbound email?
Treat every inbound email as untrusted text. A message can contain instructions aimed at your model, and a browser agent can act on them. Prefer extracted.codes over free-text interpretation, and only accept mail from the expected sender by filtering on from_contains.
Never give the signing-up agent mail:send, and have any agent that writes mail use drafts, which wait for a human to send. There are two key setups:
- The agent creates its own inbox. Use a key scoped to
mail:read,inboxes:manageanddrafts:write, with nomail:sendand noinbox_idsrestriction. - You pre-create the inbox. Give the agent a key restricted with
inbox_ids. Inbox-restricted keys cannot create inboxes, so the agent only waits and reads.
Details are in AI agents and email verification codes and human approval for agent email.
Checklist for agent builders
- Both servers (browser and mail) are in the same client config.
- One inbox per run, with
ttl_minutesset. wait_for_messagefilters on sender or subject, with a timeout you chose.- The agent uses
extracted.codesor a verify link, not the whole body. - The key has no
mail:sendscope. If the agent creates inboxes it needsinboxes:manage; otherwise pre-create the inbox and restrict the key withinbox_ids. - A timeout path exists: retry once, then report to a human.
- A CAPTCHA path exists: stop and ask, do not loop.
- On sites that block shared domains, switch to a Pro custom domain.
Ready to try it? Start free, connect via the quickstart, and have your browser agent sign up somewhere in minutes. For the broader picture see email for AI agents.
Frequently asked questions
How does an AI agent get an emailed 2FA code in a browser?
It creates an inbox with create_inbox, types the address into the site, calls wait_for_message to block until the email arrives, reads extracted.codes from the returned message, and types the code back into the browser. Over MCP these tools are found with search_tools and run via call_tool.
Does this solve CAPTCHAs or sites that block disposable email?
No. CAPTCHAs are not solved. If a site rejects shared-domain addresses, Pro lets you create inboxes on your own custom domain, which many sites accept, though none is guaranteed.
Is it safe to let a browser agent read inbound email?
Treat inbound email as untrusted text. Prefer extracted codes over free text, filter by sender with from_contains, use a key without mail:send scope, and have outbound replies saved as drafts for human approval.