Browser
sandbox.browser drives headless Chromium in a sandbox from the browser template. It reads a page as an accessibility tree: a
short outline in which every control has a ref (e12) that click and type take. An interactive page costs a few hundred tokens
this way, where a screenshot costs an image’s worth on every step.
import re
from svm import Sandbox
def ref_of(snapshot: str, role: str) -> str: """The ref of the first element with this role, as in '- searchbox "Search" [ref=e12]'.""" return re.search(rf"{role}\b[^\n]*\[ref=(e\d+)\]", snapshot).group(1)
browser_server = [{"name": "browser", "catalog_id": "browser"}]with Sandbox.create(template="browser", mcp_servers=browser_server) as sandbox: browser = sandbox.browser page = browser.navigate("https://www.wikipedia.org") print(page.text[:400]) # the page as an accessibility tree
snapshot = browser.snapshot() browser.type(ref_of(snapshot.text, "searchbox"), "Firecracker microVM", submit=True) browser.click(ref_of(browser.snapshot().text, "link"))
with open("page.png", "wb") as f: f.write(browser.screenshot())import { writeFile } from "node:fs/promises";import { Sandbox } from "@svm/sandbox";
const sandbox = await Sandbox.create({ template: "browser", mcpServers: [{ name: "browser", catalog_id: "browser" }], wait: true,});try { const browser = sandbox.browser; await browser.navigate("https://example.com");
const page = await browser.snapshot(); // accessibility tree; refs such as "e2" come from here console.log(page.text);
await browser.click("e2", { element: "More information link" }); await browser.type("e5", "sandbox", { element: "search box", submit: true });
await writeFile("page.png", await browser.screenshot({ fullPage: true }));} finally { await sandbox.kill();}sandbox, err := client.CreateSandbox(ctx, svm.CreateSandboxOptions{ Template: "browser", MCPServers: []map[string]any{{"name": "browser", "catalog_id": "browser"}},})if err != nil { return err}defer sandbox.Kill(ctx)
browser := sandbox.Browser()call := svm.BrowserCallOptions{}if _, err := browser.Navigate(ctx, "https://en.wikipedia.org", call); err != nil { return err}snapshot, err := browser.Snapshot(ctx, call) // the accessibility tree, a ref such as "e2" on every controlif err != nil { return err}fmt.Println(snapshot.Text)
// Take the refs from the snapshot text.if _, err := browser.Click(ctx, "e12", "Search box", false, call); err != nil { return err}if _, err := browser.Type(ctx, "e12", "Firecracker microVM", "Search box", true, call); err != nil { return err}png, err := browser.Screenshot(ctx, false)if err != nil { return err}require "svm"
box = SVM::Sandbox.create(template: "browser", mcp_servers: [{ name: "browser", catalog_id: "browser" }])begin browser = box.browser browser.navigate("https://example.com")
page = browser.snapshot # the accessibility tree, with a ref such as "e2" on every control puts page.text
# Use refs from the latest snapshot. browser.click("e6", element: "More information link") browser.type("e3", "sandboxes", element: "Search box", submit: true)
File.binwrite("page.png", browser.screenshot) # PNG bytesensure box.killendimport dev.svm.sandbox.Browser;import dev.svm.sandbox.Client;import dev.svm.sandbox.Sandbox;import java.nio.file.Files;import java.nio.file.Path;import java.util.List;import java.util.Map;
public class BrowserExample { public static void main(String[] args) throws Exception { Client client = Client.fromEnvironment(); Sandbox.CreateOptions options = new Sandbox.CreateOptions(); options.template = "browser"; options.mcpServers = List.of(Map.of("name", "browser", "catalog_id", "browser")); options.wait = true; try (Sandbox sandbox = Sandbox.create(client, options)) { Browser browser = sandbox.browser();
browser.navigate("https://duckduckgo.com"); // The snapshot is the accessibility tree; every control carries a ref such as "e2". System.out.println(browser.snapshot().text);
// Use the refs the snapshot printed for the search box and its button. browser.type("e2", "firecracker microvm", "search box", false); browser.click("e3", "Search button", false);
Files.write(Path.of("page.png"), browser.screenshot(false)); } }}| Method | What it does |
|---|---|
navigate(url), back() |
Move, and return the snapshot after the move |
snapshot(max_chars=) |
The accessibility tree, cut at 24,000 characters |
click(ref), type(ref, text, submit=), press(key) |
Act on a ref from the latest snapshot |
wait_for(text=, gone=, seconds=) |
Wait for the page to show or lose some text |
screenshot(full_page=) |
PNG bytes, only when you ask |
call(tool, args), tools() |
Any tool the browser server offers |
The same Chromium serves the DevTools protocol inside the sandbox at browser.cdp_url. The browser-use library is installed in the
template and attaches there.