blob: 6c03e1a72e7797612495a27950ec31195048334f [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
17
18## Usage
19
20```go
21// Create a context
22ctx := context.Background()
23
24// Register browser tools and get a cleanup function
25tools, cleanup := browse.RegisterBrowserTools(ctx)
26defer cleanup() // Important: always call cleanup to release browser resources
27
28// Add tools to your agent
29for _, tool := range tools {
30 agent.AddTool(tool)
31}
32```
33
34## Requirements
35
36- Chrome or Chromium must be installed on the system
37- The `chromedp` package handles launching and controlling the browser
38
39## Tool Input/Output
40
41All tools follow a standard JSON input/output format. For example:
42
43**Navigate Tool Input:**
44```json
45{
46 "url": "https://example.com"
47}
48```
49
50**Navigate Tool Output (success):**
51```json
52{
53 "status": "success"
54}
55```
56
57**Tool Output (error):**
58```json
59{
60 "status": "error",
61 "error": "Error message"
62}
63```
64
65## Example Tool Usage
66
67```go
68// Example of using the navigate tool directly
69navTool := tools[0] // Get browser_navigate tool
70input := map[string]string{"url": "https://example.com"}
71inputJSON, _ := json.Marshal(input)
72
73// Call the tool
74result, err := navTool.Run(ctx, json.RawMessage(inputJSON))
75if err != nil {
76 log.Fatalf("Error: %v", err)
77}
78fmt.Println(result)
79```
80
81## Screenshot Storage
82
83The 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:
84
851. Reducing token usage in LLM responses
862. Avoiding encoding/decoding overhead
873. Allowing for larger screenshots without message size limitations
88
89### How It Works
90
911. When a screenshot is taken, it's saved to `/tmp/sketch-screenshots/` with a unique UUID filename
922. The tool returns the screenshot ID in its response
933. The web UI can fetch the screenshot using the `/screenshot/{id}` endpoint
94
95### Example Usage
96
97Agent calls the screenshot tool:
98```json
99{
100 "id": "tool_call_123",
101 "name": "browser_screenshot",
102 "params": {}
103}
104```
105
106Tool response:
107```json
108{
109 "id": "tool_call_123",
110 "result": {
111 "id": "550e8400-e29b-41d4-a716-446655440000"
112 }
113}
114```
115
116The screenshot is then accessible at: `/screenshot/550e8400-e29b-41d4-a716-446655440000`