MastraBrowser class
The MastraBrowser class is the abstract base class for browser automation providers. It defines the common interface for launching browsers, managing thread isolation, streaming screencasts, and handling input events.
You don't instantiate MastraBrowser directly. Instead, use a provider implementation:
AgentBrowser: Deterministic browser automation using refsStagehandBrowser: AI-powered browser automation using natural language
Usage exampleDirect link to Usage example
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 to find information.',
model: 'openai/gpt-5.4',
browser,
})
Constructor parametersDirect link to Constructor parameters
headless?:
viewport?:
timeout?:
cdpUrl?:
scope?:
onLaunch?:
onClose?:
screencast?:
format?:
quality?:
maxWidth?:
maxHeight?:
everyNthFrame?:
PropertiesDirect link to Properties
The following properties (id, name, provider) are abstract and must be defined by concrete provider implementations:
id:
name:
provider:
headless:
status:
MethodsDirect link to Methods
LifecycleDirect link to Lifecycle
ensureReady()Direct link to ensureready
Ensures the browser is launched and ready for use. Automatically called before tool execution. Implemented in the base class.
await browser.ensureReady()
close()Direct link to close
Closes the browser and cleans up all resources. Implemented in the base class with race-condition-safe handling.
await browser.close()
isBrowserRunning()Direct link to isbrowserrunning
Checks if the browser is currently running.
const isRunning = browser.isBrowserRunning()
Returns: boolean
Thread managementDirect link to Thread management
setCurrentThread(threadId)Direct link to setcurrentthreadthreadid
Sets the current thread ID for browser operations. Used internally by the agent runtime.
browser.setCurrentThread('thread-123')
getCurrentThread()Direct link to getcurrentthread
Gets the current thread ID.
const threadId = browser.getCurrentThread()
Returns: string
hasThreadSession(threadId)Direct link to hasthreadsessionthreadid
Checks if a thread has an active browser session.
const hasSession = browser.hasThreadSession('thread-123')
Returns: boolean
closeThreadSession(threadId)Direct link to closethreadsessionthreadid
Closes a specific thread's browser session. For 'thread' scope, this closes that thread's browser instance. For 'shared' scope, this clears the thread's state.
await browser.closeThreadSession('thread-123')
ToolsDirect link to Tools
getTools()Direct link to gettools
Returns the browser tools for use with agents. Each provider returns different tools based on its paradigm.
const tools = browser.getTools()
Returns: Record<string, Tool>
ScreencastDirect link to Screencast
startScreencast(options?, threadId?)Direct link to startscreencastoptions-threadid
Starts streaming browser frames. Returns a ScreencastStream that emits frame events.
const stream = await browser.startScreencast({ format: 'jpeg', quality: 80 }, 'thread-123')
stream.on('frame', frame => {
console.log('Frame received:', frame.data.length, 'bytes')
})
stream.on('stop', reason => {
console.log('Screencast stopped:', reason)
})
Returns: Promise<ScreencastStream>
Input injectionDirect link to Input injection
injectMouseEvent(params, threadId?)Direct link to injectmouseeventparams-threadid
Injects a mouse event into the browser. Used by Studio for live interaction.
await browser.injectMouseEvent({
type: 'mousePressed',
x: 100,
y: 200,
button: 'left',
clickCount: 1,
})
injectKeyboardEvent(params, threadId?)Direct link to injectkeyboardeventparams-threadid
Injects a keyboard event into the browser. Used by Studio for live interaction.
await browser.injectKeyboardEvent({
type: 'keyDown',
key: 'Enter',
code: 'Enter',
})
StateDirect link to State
getState(threadId?)Direct link to getstatethreadid
Gets the current browser state including URL and tabs.
const state = await browser.getState('thread-123')
console.log('Current URL:', state.currentUrl)
console.log('Tabs:', state.tabs)
Returns: Promise<BrowserState>
interface BrowserState {
currentUrl: string | null
tabs: BrowserTabState[]
activeTabIndex: number
}
interface BrowserTabState {
id: string
url: string
title: string
}
getCurrentUrl(threadId?)Direct link to getcurrenturlthreadid
Gets the current page URL.
const url = await browser.getCurrentUrl()
Returns: Promise<string | null>
Browser scopeDirect link to Browser scope
The scope option controls how browser instances are shared across conversation threads:
| Scope | Description | Use case |
|---|---|---|
'shared' | All threads share a single browser instance | Cost-efficient for non-conflicting tasks |
'thread' | Each thread gets its own browser instance | Full isolation for concurrent users |
// Shared browser for all threads
const sharedBrowser = new AgentBrowser({
scope: 'shared',
})
// Isolated browser per thread
const isolatedBrowser = new AgentBrowser({
scope: 'thread',
})
When using cdpUrl to connect to an external browser, the scope automatically falls back to 'shared' since you can't spawn new browser instances.
Cloud browser providersDirect link to Cloud browser providers
Connect to cloud browser services using the cdpUrl option:
// Static CDP URL
const browser = new AgentBrowser({
cdpUrl: 'wss://browser.example.com/ws',
})
// Dynamic CDP URL (e.g., session-based)
const browser = new AgentBrowser({
cdpUrl: async () => {
const session = await createBrowserSession()
return session.wsUrl
},
})
RelatedDirect link to Related
- AgentBrowser: Deterministic browser automation
- StagehandBrowser: AI-powered browser automation
- Browser overview: Conceptual guide to browser automation