Crate browser_use

Crate browser_use 

Source
Expand description

§browser-use

A Rust library for browser automation via Chrome DevTools Protocol (CDP), designed for AI agent integration.

§Features

  • MCP Server: Model Context Protocol server for AI-driven browser automation
  • Browser Session Management: Launch or connect to Chrome/Chromium instances
  • Tool System: High-level browser operations (navigate, click, input, extract, screenshot, etc.)
  • DOM Extraction: Extract page structure with indexed interactive elements for AI-friendly targeting

§MCP Server

The recommended way to use this library is via the Model Context Protocol (MCP) server, which exposes browser automation tools to AI agents like Claude:

§Running the MCP Server

# Run headless browser
cargo run --bin mcp-server

# Run with visible browser (useful for debugging)
cargo run --bin mcp-server -- --headed

§Library Usage (Advanced)

For direct integration in Rust applications:

§Basic Browser Automation

use browser_use::{BrowserSession, LaunchOptions};

// Launch a browser
let session = BrowserSession::launch(LaunchOptions::default())?;

// Navigate to a page
session.navigate("https://example.com")?;

// Extract DOM with indexed elements
let dom = session.extract_dom()?;
println!("Found {} interactive elements", dom.count_interactive());

§Using the Tool System

use browser_use::{BrowserSession, LaunchOptions};
use browser_use::tools::{ToolRegistry, ToolContext};
use serde_json::json;

let session = BrowserSession::launch(LaunchOptions::default())?;
let registry = ToolRegistry::with_defaults();
let mut context = ToolContext::new(&session);

// Navigate using the tool system
registry.execute("navigate", json!({"url": "https://example.com"}), &mut context)?;

// Click an element by index
registry.execute("click", json!({"index": 5}), &mut context)?;

§DOM Indexing for AI Agents

The library automatically indexes interactive elements (buttons, links, inputs) with numeric IDs, making it easier for AI agents to target elements without complex CSS selectors:

let dom = session.extract_dom()?;

// Interactive elements are indexed and can be accessed via tools
println!("Found {} interactive elements", dom.count_interactive());

§Module Overview

  • browser: Browser session management and configuration
  • dom: DOM extraction, element indexing, and tree representation
  • tools: Browser automation tools (navigate, click, input, extract, etc.)
  • error: Error types and result aliases
  • mcp: Model Context Protocol server (requires mcp-server feature) - Start here for AI integration

Re-exports§

pub use browser::BrowserSession;
pub use browser::ConnectionOptions;
pub use browser::LaunchOptions;
pub use dom::BoundingBox;
pub use dom::DomTree;
pub use dom::ElementNode;
pub use dom::ElementSelector;
pub use dom::SelectorMap;
pub use error::BrowserError;
pub use error::Result;
pub use tools::Tool;
pub use tools::ToolContext;
pub use tools::ToolRegistry;
pub use tools::ToolResult;
pub use mcp::BrowserServer;

Modules§

browser
Browser management module
dom
DOM extraction and manipulation module
error
mcp
MCP (Model Context Protocol) server implementation for browser automation
tools
Browser automation tools module