Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
agentmail integration
Gregor Žunič0d670fa39caa
diff --git a/examples/integrations/agentmail/2fa.py b/examples/integrations/agentmail/2fa.pynew file mode 100644index 000000000..ac84e80f5--- /dev/null+++ b/examples/integrations/agentmail/2fa.py@@ -0,0 +1,25 @@+import asyncio++from browser_use import Agent+from examples.integrations.agentmail.controller import EmailController++TASK = """+Go to reddit.com, create a new account (please don't make email, use the get_email_address and use that email address), make up password and all other information, confirm the 2fa, and like latest post on r/elon subreddit.+"""+++async def main():+ email_controller = EmailController()++ actions = email_controller.registry.get_prompt_description()++ agent = Agent(+ task=TASK,+ controller=email_controller,+ )++ await agent.run()+++if __name__ == '__main__':+ asyncio.run(main())diff --git a/examples/integrations/agentmail/controller.py b/examples/integrations/agentmail/controller.pynew file mode 100644index 000000000..1a598a775--- /dev/null+++ b/examples/integrations/agentmail/controller.py@@ -0,0 +1,97 @@+"""+Email management to enable 2fa.+"""++import asyncio++from agentmail import AsyncAgentMail, Message, MessageReceived, Subscribe+from agentmail.inboxes.types.inbox import Inbox+from agentmail.inboxes.types.inbox_id import InboxId++from browser_use.controller.service import Controller+++class EmailController(Controller):+ def __init__(self, email_client: AsyncAgentMail | None = None, email_timeout: int = 30):+ super().__init__()+ self.email_client = email_client or AsyncAgentMail()++ self.email_timeout = email_timeout++ self.register_email_tools()++ def _serialize_message_for_llm(self, message: Message) -> str:+ """+ Serialize a message for the LLM+ """+ return f'From: {message.from_}\nTo: {message.to}\nTimestamp: {message.timestamp.isoformat()}\nSubject: {message.subject}\nBody: {message.text}'++ async def get_or_create_inbox_client(self) -> Inbox:+ """+ Create a default inbox profile for this API key (assume that agent is on free tier)++ If you are not on free tier it is recommended to create 1 inbox per agent.+ """+ inboxes = await self.email_client.inboxes.list()++ if not inboxes.inboxes:+ inbox = await self.email_client.inboxes.create()+ return inbox++ return inboxes.inboxes[0]++ async def wait_for_message(self, inbox_id: InboxId) -> Message:+ """+ Wait for a message to be received in the inbox+ """+ async with self.email_client.websockets.connect() as ws:+ await ws.send_subscribe(message=Subscribe(inbox_ids=[inbox_id]))++ try:+ while True:+ data = await asyncio.wait_for(ws.recv(), timeout=self.email_timeout)+ if isinstance(data, MessageReceived):+ await self.email_client.inboxes.messages.update(+ inbox_id=inbox_id, message_id=data.message.message_id, remove_labels=['unread']+ )+ return data.message+ # If not MessageReceived, continue waiting for the next event+ except TimeoutError:+ raise TimeoutError(f'No email received in the inbox in {self.email_timeout}s')++ def register_email_tools(self):+ """Register all email-related controller actions"""++ @self.action('Get email address for login. You can use this email to login to any service with email and password')+ async def get_email_address() -> str:+ """+ Get the email address of the inbox+ """+ inbox = await self.get_or_create_inbox_client()+ return inbox.inbox_id++ @self.action(+ 'Get the latest email from the inbox. You can use this to get the codes for 2fa for example. This function automatically waits for the email to be received.'+ )+ async def get_latest_email() -> str:+ """+ 1. check whether there is an unread email in the inbox; if multiple return all emails as string+ 2. if no email; connect via websocket to agentmail and wait until `message_received`+ """++ inbox = await self.get_or_create_inbox_client()++ emails = await self.email_client.inboxes.messages.list(inbox_id=inbox.inbox_id, labels=['unread'])++ if not emails.messages:+ latest_message = await self.wait_for_message(inbox_id=inbox.inbox_id)+ return self._serialize_message_for_llm(latest_message)++ last_email_id = emails.messages[-1].message_id++ last_email = await self.email_client.inboxes.messages.get(inbox_id=inbox.inbox_id, message_id=last_email_id)+ await self.email_client.inboxes.messages.update(+ inbox_id=inbox.inbox_id, message_id=last_email_id, remove_labels=['unread']+ )++ return self._serialize_message_for_llm(last_email)diff --git a/pyproject.toml b/pyproject.tomlindex 3069190e1..6b1f6ad67 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -62,6 +62,7 @@ aws = ["boto3>=1.38.45"]examples = [+ "agentmail>=0.0.53",# botocore: only needed for Bedrock Claude boto3 examples/models/bedrock_claude.py"botocore>=1.37.23","imgcat>=0.6.0",