blob: 69ebf078a64169695bec0d7255f0d4f19816a217 [file] [log] [blame] [view]
Philip Zeyliger33d282f2025-05-03 04:01:54 +00001# Browser Tools for Claude
2
3This package provides a set of tools that allow Claude to control a headless
4Chrome browser from Go. The tools are built using the
5[chromedp](https://github.com/chromedp/chromedp) library.
6
7## Available Tools
8
91. `browser_navigate` - Navigate to a URL and wait for the page to load
102. `browser_click` - Click an element matching a CSS selector
113. `browser_type` - Type text into an input field
124. `browser_wait_for` - Wait for an element to appear in the DOM
135. `browser_get_text` - Get the text content of an element
146. `browser_eval` - Evaluate JavaScript in the browser context
157. `browser_screenshot` - Take a screenshot of the page or a specific element
168. `browser_scroll_into_view` - Scroll an element into view
Philip Zeyliger05224842025-05-10 18:26:08 -0700179. `browser_resize` - Resize the browser window to specific dimensions
Philip Zeyliger33d282f2025-05-03 04:01:54 +000018
19## Usage
20
21```go
22// Create a context
23ctx := context.Background()
24
25// Register browser tools and get a cleanup function
26tools, cleanup := browse.RegisterBrowserTools(ctx)
27defer cleanup() // Important: always call cleanup to release browser resources
28
29// Add tools to your agent
30for _, tool := range tools {
31 agent.AddTool(tool)
32}
33```
34
35## Requirements
36
37- Chrome or Chromium must be installed on the system
Philip Zeyliger95006172025-05-28 20:05:46 -070038- In Docker environments, the multi-stage build automatically provides headless-shell from chromedp/headless-shell
39- For local development, install Chrome/Chromium manually
Philip Zeyliger33d282f2025-05-03 04:01:54 +000040- The `chromedp` package handles launching and controlling the browser
41
42## Tool Input/Output
43
44All tools follow a standard JSON input/output format. For example:
45
46**Navigate Tool Input:**
47```json
48{
49 "url": "https://example.com"
50}
51```
52
53**Navigate Tool Output (success):**
54```json
55{
56 "status": "success"
57}
58```
59
60**Tool Output (error):**
61```json
62{
63 "status": "error",
64 "error": "Error message"
65}
66```
67
68## Example Tool Usage
69
70```go
71// Example of using the navigate tool directly
72navTool := tools[0] // Get browser_navigate tool
73input := map[string]string{"url": "https://example.com"}
74inputJSON, _ := json.Marshal(input)
75
76// Call the tool
77result, err := navTool.Run(ctx, json.RawMessage(inputJSON))
78if err != nil {
79 log.Fatalf("Error: %v", err)
80}
81fmt.Println(result)
82```
83
84## Screenshot Storage
85
86The browser screenshot tool has been modified to save screenshots to a temporary directory and identify them by ID, rather than returning base64-encoded data directly. This improves efficiency by:
87
881. Reducing token usage in LLM responses
892. Avoiding encoding/decoding overhead
903. Allowing for larger screenshots without message size limitations
91
92### How It Works
93
941. When a screenshot is taken, it's saved to `/tmp/sketch-screenshots/` with a unique UUID filename
952. The tool returns the screenshot ID in its response
963. The web UI can fetch the screenshot using the `/screenshot/{id}` endpoint
97
98### Example Usage
99
100Agent calls the screenshot tool:
101```json
102{
103 "id": "tool_call_123",
104 "name": "browser_screenshot",
105 "params": {}
106}
107```
108
109Tool response:
110```json
111{
112 "id": "tool_call_123",
113 "result": {
114 "id": "550e8400-e29b-41d4-a716-446655440000"
115 }
116}
117```
118
119The screenshot is then accessible at: `/screenshot/550e8400-e29b-41d4-a716-446655440000`