BrowserViewer
The BrowserViewer class provides browser automation for CLI-based tools. It launches Chrome through Playwright and exposes a Chrome DevTools Protocol (CDP) URL. It automatically injects that URL into CLI commands run through workspace tools.
Use BrowserViewer when your agent drives a browser through a CLI tool like browser-use, agent-browser, or browse. For SDK-based browser automation, use AgentBrowser or StagehandBrowser.
Usage exampleUsage example的直接链接
import { Workspace, LocalSandbox } from '@mastra/core/workspace'
import { Memory } from '@mastra/memory'
import { BrowserViewer } from '@mastra/browser-viewer'
import { Agent } from '@mastra/core/agent'
const workspace = new Workspace({
sandbox: new LocalSandbox({
workingDirectory: './workspace',
}),
browser: new BrowserViewer({
cli: 'browser-use',
headless: false,
}),
})
const browserAgent = new Agent({
id: 'browser-agent',
model: 'openai/gpt-5.6-sol',
workspace,
instructions: 'You are a web automation assistant.',
memory: new Memory(),
})
Connecting to an existing browserConnecting to an existing browser的直接链接
const viewer = new BrowserViewer({
cli: 'browser-use',
cdpUrl: 'ws://127.0.0.1:9222/devtools/browser/abc123',
})
When cdpUrl is provided, BrowserViewer connects to the existing browser instead of launching a new one. Scope defaults to 'shared'.
Constructor parametersConstructor parameters的直接链接
cli:
headless?:
cdpUrl?:
cdpPort?:
scope?:
viewport?:
executablePath?:
onLaunch?:
onClose?:
screencast?:
PropertiesProperties的直接链接
id:
name:
provider:
providerType:
cli:
status:
MethodsMethods的直接链接
LifecycleLifecycle的直接链接
launch(threadId?)launchthreadid的直接链接
Launches Chrome with either a single shared browser for 'shared' scope or a browser for the specified thread when using 'thread' scope.
await viewer.launch()
await viewer.launch('thread-123')
ensureReady()ensureready的直接链接
Ensures the browser is launched and ready. For 'thread' scope, creates a new browser for the current thread if needed.
await viewer.ensureReady()
isBrowserRunning(threadId?)isbrowserrunningthreadid的直接链接
Checks if a browser is running. For 'thread' scope, checks the specified thread.
const running = viewer.isBrowserRunning()
const threadRunning = viewer.isBrowserRunning('thread-123')
Returns: boolean
close()close的直接链接
Closes all browser instances and cleans up resources.
await viewer.close()
CDP accessCDP access的直接链接
getCdpUrl(threadId?)getcdpurlthreadid的直接链接
Returns the CDP WebSocket URL for the current or specified thread. CLI tools use this URL to connect to the managed browser.
const cdpUrl = viewer.getCdpUrl()
// => 'ws://127.0.0.1:52481/devtools/browser/abc...'
Returns: string | null
connectToExternalCdp(cdpUrl, threadId?)connecttoexternalcdpcdpurl-threadid的直接链接
Connects to an external browser via CDP URL for screencast. Use this when the agent provides its own browser endpoint (for example, a cloud browser service). BrowserViewer connects for screencast without managing the browser lifecycle.
await viewer.connectToExternalCdp('wss://cloud.example.com/session', 'thread-123')
ScreencastScreencast的直接链接
startScreencast(options?)startscreencastoptions的直接链接
Starts streaming browser frames. Returns a ScreencastStream that emits frame events. Handles tab switching automatically by creating fresh CDP sessions when tabs change.
const stream = await viewer.startScreencast({
format: 'jpeg',
quality: 80,
})
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 injectionInput injection的直接链接
injectMouseEvent(params, threadId?)injectmouseeventparams-threadid的直接链接
Injects a mouse event into the browser via CDP. Used by Studio for live interaction.
await viewer.injectMouseEvent({
type: 'mousePressed',
x: 100,
y: 200,
button: 'left',
clickCount: 1,
})
injectKeyboardEvent(params, threadId?)injectkeyboardeventparams-threadid的直接链接
Injects a keyboard event into the browser via CDP. Used by Studio for live interaction.
await viewer.injectKeyboardEvent({
type: 'keyDown',
key: 'Enter',
code: 'Enter',
})
Supported CLIsSupported CLIs的直接链接
Each CLI must be installed separately. Each also publishes a skill that teaches the agent its commands and workflows. When a CLI command runs through workspace_execute_command, Mastra detects it and injects the CDP URL automatically using the correct flag.
Unlike SDK providers, BrowserViewer doesn't provide agent tools. The agent uses CLI commands through workspace_execute_command instead.
agent-browseragent-browser的直接链接
Config value: 'agent-browser' · CDP flag: --cdp
npm install -g agent-browser
npx skills add vercel-labs/agent-browser
browser-usebrowser-use的直接链接
Config value: 'browser-use' · CDP flag: --cdp-url
pip install browser-use
npx skills add browser-use/browser-use --skill browser-use
browse (command: browse)browse-command-browse的直接链接
Config value: 'browse' · CDP flag: --ws
npm install -g browse
browse skills install
RelatedRelated的直接链接
- BrowserViewer guide: Setup and usage walkthrough
- MastraBrowser: Base class API reference
- Sandbox: Workspace configuration