| Philip Zeyliger | 33d282f | 2025-05-03 04:01:54 +0000 | [diff] [blame] | 1 | # Browser Tools for Claude |
| 2 | |
| 3 | This package provides a set of tools that allow Claude to control a headless |
| 4 | Chrome browser from Go. The tools are built using the |
| 5 | [chromedp](https://github.com/chromedp/chromedp) library. |
| 6 | |
| 7 | ## Available Tools |
| 8 | |
| 9 | 1. `browser_navigate` - Navigate to a URL and wait for the page to load |
| 10 | 2. `browser_click` - Click an element matching a CSS selector |
| 11 | 3. `browser_type` - Type text into an input field |
| 12 | 4. `browser_wait_for` - Wait for an element to appear in the DOM |
| 13 | 5. `browser_get_text` - Get the text content of an element |
| 14 | 6. `browser_eval` - Evaluate JavaScript in the browser context |
| 15 | 7. `browser_screenshot` - Take a screenshot of the page or a specific element |
| 16 | 8. `browser_scroll_into_view` - Scroll an element into view |
| Philip Zeyliger | 0522484 | 2025-05-10 18:26:08 -0700 | [diff] [blame] | 17 | 9. `browser_resize` - Resize the browser window to specific dimensions |
| Philip Zeyliger | 33d282f | 2025-05-03 04:01:54 +0000 | [diff] [blame] | 18 | |
| 19 | ## Usage |
| 20 | |
| 21 | ```go |
| 22 | // Create a context |
| 23 | ctx := context.Background() |
| 24 | |
| 25 | // Register browser tools and get a cleanup function |
| 26 | tools, cleanup := browse.RegisterBrowserTools(ctx) |
| 27 | defer cleanup() // Important: always call cleanup to release browser resources |
| 28 | |
| 29 | // Add tools to your agent |
| 30 | for _, 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 | |
| 42 | All 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 |
| 70 | navTool := tools[0] // Get browser_navigate tool |
| 71 | input := map[string]string{"url": "https://example.com"} |
| 72 | inputJSON, _ := json.Marshal(input) |
| 73 | |
| 74 | // Call the tool |
| 75 | result, err := navTool.Run(ctx, json.RawMessage(inputJSON)) |
| 76 | if err != nil { |
| 77 | log.Fatalf("Error: %v", err) |
| 78 | } |
| 79 | fmt.Println(result) |
| 80 | ``` |
| 81 | |
| 82 | ## Screenshot Storage |
| 83 | |
| 84 | The 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 | |
| 86 | 1. Reducing token usage in LLM responses |
| 87 | 2. Avoiding encoding/decoding overhead |
| 88 | 3. Allowing for larger screenshots without message size limitations |
| 89 | |
| 90 | ### How It Works |
| 91 | |
| 92 | 1. When a screenshot is taken, it's saved to `/tmp/sketch-screenshots/` with a unique UUID filename |
| 93 | 2. The tool returns the screenshot ID in its response |
| 94 | 3. The web UI can fetch the screenshot using the `/screenshot/{id}` endpoint |
| 95 | |
| 96 | ### Example Usage |
| 97 | |
| 98 | Agent calls the screenshot tool: |
| 99 | ```json |
| 100 | { |
| 101 | "id": "tool_call_123", |
| 102 | "name": "browser_screenshot", |
| 103 | "params": {} |
| 104 | } |
| 105 | ``` |
| 106 | |
| 107 | Tool response: |
| 108 | ```json |
| 109 | { |
| 110 | "id": "tool_call_123", |
| 111 | "result": { |
| 112 | "id": "550e8400-e29b-41d4-a716-446655440000" |
| 113 | } |
| 114 | } |
| 115 | ``` |
| 116 | |
| 117 | The screenshot is then accessible at: `/screenshot/550e8400-e29b-41d4-a716-446655440000` |