blob: 7cc343764dd7bd250f0523f150c905ca6ddb8150 [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
38- The `chromedp` package handles launching and controlling the browser
39
40## Tool Input/Output
41
42All tools follow a standard JSON input/output format. For example:
43
44**Navigate Tool Input:**
45```json
46{
47 "url": "https://example.com"
48}
49```
50
51**Navigate Tool Output (success):**
52```json
53{
54 "status": "success"
55}
56```
57
58**Tool Output (error):**
59```json
60{
61 "status": "error",
62 "error": "Error message"
63}
64```
65
66## Example Tool Usage
67
68```go
69// Example of using the navigate tool directly
70navTool := tools[0] // Get browser_navigate tool
71input := map[string]string{"url": "https://example.com"}
72inputJSON, _ := json.Marshal(input)
73
74// Call the tool
75result, err := navTool.Run(ctx, json.RawMessage(inputJSON))
76if err != nil {
77 log.Fatalf("Error: %v", err)
78}
79fmt.Println(result)
80```
81
82## Screenshot Storage
83
84The 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:
85
861. Reducing token usage in LLM responses
872. Avoiding encoding/decoding overhead
883. Allowing for larger screenshots without message size limitations
89
90### How It Works
91
921. When a screenshot is taken, it's saved to `/tmp/sketch-screenshots/` with a unique UUID filename
932. The tool returns the screenshot ID in its response
943. The web UI can fetch the screenshot using the `/screenshot/{id}` endpoint
95
96### Example Usage
97
98Agent calls the screenshot tool:
99```json
100{
101 "id": "tool_call_123",
102 "name": "browser_screenshot",
103 "params": {}
104}
105```
106
107Tool response:
108```json
109{
110 "id": "tool_call_123",
111 "result": {
112 "id": "550e8400-e29b-41d4-a716-446655440000"
113 }
114}
115```
116
117The screenshot is then accessible at: `/screenshot/550e8400-e29b-41d4-a716-446655440000`