Skip to main content

AgentBrowser class

The AgentBrowser class provides deterministic browser automation using the agent-browser library. It uses accessibility tree snapshots and element refs (e.g., @e5) for precise, reproducible interactions.

Use AgentBrowser when you need reliable, deterministic browser automation. For AI-powered interactions using natural language, see StagehandBrowser.

Usage example
Direct link to Usage example

src/mastra/agents/index.ts
import { Agent } from '@mastra/core/agent'
import { AgentBrowser } from '@mastra/agent-browser'

const browser = new AgentBrowser({
headless: true,
viewport: { width: 1280, height: 720 },
scope: 'thread',
})

export const browserAgent = new Agent({
name: 'browser-agent',
instructions: `You can browse the web. Use browser_snapshot to see the page structure,
then interact with elements using their refs (e.g., @e5).`,
model: 'openai/gpt-5.4',
browser,
})

Constructor parameters
Direct link to Constructor parameters

headless?:

boolean
= true
Whether to run the browser in headless mode (no visible UI).

viewport?:

{ width: number; height: number }
= { width: 1280, height: 720 }
Browser viewport dimensions.

timeout?:

number
= 30000
Default timeout in milliseconds for browser operations.

cdpUrl?:

string | (() => string | Promise<string>)
CDP WebSocket URL for connecting to an existing browser. Useful for cloud browser providers.

scope?:

'shared' | 'thread'
= 'thread' (or 'shared' when cdpUrl is provided)
Browser instance scope. 'shared' shares one browser across all threads. 'thread' gives each thread its own browser.

onLaunch?:

(args: { browser: MastraBrowser }) => void | Promise<void>
Callback invoked after the browser is ready.

onClose?:

(args: { browser: MastraBrowser }) => void | Promise<void>
Callback invoked before the browser closes.

screencast?:

ScreencastOptions
Configuration for streaming browser frames to Studio.

Tools
Direct link to Tools

AgentBrowser provides 15 deterministic tools for browser automation. All tools that interact with elements use refs from the accessibility tree snapshot.

Core tools
Direct link to Core tools

ToolDescription
browser_gotoNavigate to a URL
browser_snapshotGet accessibility tree snapshot with element refs
browser_clickClick an element by ref
browser_typeType text into an element
browser_pressPress keyboard keys
browser_selectSelect option from dropdown
browser_scrollScroll the page or element
browser_closeClose the browser

Extended tools
Direct link to Extended tools

ToolDescription
browser_hoverHover over an element
browser_backGo back in browser history
browser_dialogHandle browser dialogs (alert, confirm, prompt)
browser_waitWait for element state changes
browser_tabsManage browser tabs (list, new, switch, close)
browser_dragDrag and drop elements
browser_evaluateExecute JavaScript in the page (escape hatch)

Tool reference
Direct link to Tool reference

browser_goto
Direct link to browser_goto

Navigate to a URL.

// Tool input
{
"url": "https://example.com",
"waitUntil": "domcontentloaded",
"timeout": 30000
}
ParameterTypeDescription
urlstringURL to navigate to
waitUntil"load" | "domcontentloaded" | "networkidle"When to consider navigation complete (optional)
timeoutnumberNavigation timeout in ms (optional)

browser_snapshot
Direct link to browser_snapshot

Get an accessibility tree snapshot of the page. Returns element refs like @e5 that you use with other tools.

// Tool input
{
"interactiveOnly": true,
"maxDepth": 10
}
ParameterTypeDescription
interactiveOnlybooleanOnly include interactive elements (optional)
maxDepthnumberMaximum tree depth (optional)

Example output:

[document] Example Page
[banner]
[link @e1] Home
[link @e2] About
[main]
[heading @e3] Welcome
[textbox @e4] Search...
[button @e5] Submit

browser_click
Direct link to browser_click

Click an element using its ref from the snapshot.

{
"ref": "@e5",
"button": "left",
"clickCount": 1,
"modifiers": ["Control", "Shift"]
}
ParameterTypeDescription
refstringElement ref from snapshot (required)
button"left" | "right" | "middle"Mouse button (optional)
clickCountnumberNumber of activations, 2 for double (optional)
modifiersstring[]Modifier keys (optional)

browser_type
Direct link to browser_type

Type text into an input element.

// Tool input
{
"ref": "@e4",
"text": "search query",
"clear": true,
"delay": 50
}
ParameterTypeDescription
refstringElement ref from snapshot (required)
textstringText to type (required)
clearbooleanClear existing content first (optional)
delaynumberDelay between keystrokes in ms (optional)

browser_press
Direct link to browser_press

Press keyboard keys.

// Tool input
{
"key": "Enter",
"modifiers": ["Control"]
}

// Key combinations
{ "key": "Control+a" }
{ "key": "Control+c" }
ParameterTypeDescription
keystringKey name (e.g., "Enter", "Tab", "Escape", "Control+a") (required)
modifiersstring[]Modifier keys (optional)

browser_select
Direct link to browser_select

Select an option from a dropdown. Provide one of value, label, or index.

// Tool input - by value
{
"ref": "@e10",
"value": "option-value"
}

// Tool input - by label
{
"ref": "@e10",
"label": "Option Text"
}

// Tool input - by index
{
"ref": "@e10",
"index": 0
}

browser_scroll
Direct link to browser_scroll

Scroll the page or a specific element.

// Tool input
{
"direction": "down",
"amount": 300,
"ref": "@e15"
}
ParameterTypeDescription
direction"up" | "down" | "left" | "right"Scroll direction (required)
amountnumberPixels to scroll, default 300 (optional)
refstringElement to scroll, scrolls page if omitted (optional)

browser_hover
Direct link to browser_hover

Hover over an element to trigger hover effects.

// Tool input
{
"ref": "@e7"
}

browser_back
Direct link to browser_back

Go back in browser history.

// Tool input (no parameters required)
{}

browser_dialog
Direct link to browser_dialog

Handle browser dialogs (alert, confirm, prompt). Click an element that triggers a dialog and handle it.

// Tool input
{
"triggerRef": "@e5",
"action": "accept",
"text": "response"
}
ParameterTypeDescription
triggerRefstringElement that triggers the dialog (required)
action"accept" | "dismiss"How to handle the dialog (required)
textstringText for prompt dialogs (optional)

browser_wait
Direct link to browser_wait

Wait for an element to reach a specific state.

// Tool input
{
"ref": "@e20",
"state": "visible",
"timeout": 30000
}
ParameterTypeDescription
refstringElement ref to wait for (optional)
state"visible" | "hidden" | "attached" | "detached"State to wait for (optional)
timeoutnumberMax wait time in ms (optional)

browser_tabs
Direct link to browser_tabs

Manage browser tabs.

// List all tabs
{ "action": "list" }

// Open new tab
{ "action": "new", "url": "https://example.com" }

// Switch to tab by index
{ "action": "switch", "index": 0 }

// Close tab by index
{ "action": "close", "index": 1 }

browser_drag
Direct link to browser_drag

Drag an element to a target location.

// Tool input
{
"sourceRef": "@e10",
"targetRef": "@e20"
}
ParameterTypeDescription
sourceRefstringElement to drag (required)
targetRefstringDrop target element (required)

browser_evaluate
Direct link to browser_evaluate

Execute JavaScript in the page context. Use as an escape hatch when other tools don't cover your use case.

// Tool input
{
"script": "document.title",
"returnValue": true
}
ParameterTypeDescription
scriptstringJavaScript to execute (required)
returnValuebooleanWhether to return the result (optional)

browser_close
Direct link to browser_close

Close the browser and clean up resources.

// Tool input (no parameters required)
{}

How refs work
Direct link to How refs work

The browser_snapshot tool returns an accessibility tree with element refs like @e1, @e2, etc. These refs are stable identifiers you use with other tools:

  1. Call browser_snapshot to see the page structure
  2. Find the element you want to interact with
  3. Use its ref with interaction tools like browser_type or browser_scroll.
// 1. Get snapshot
// Returns: [textbox @e4] Search... [link @e5] Home

// 2. Type in the search box
{ "tool": "browser_type", "input": { "ref": "@e4", "text": "mastra" } }

// 3. Navigate to home
{ "tool": "browser_goto", "input": { "url": "https://example.com" } }