| 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 |
| Josh Bleecher Snyder | a271a21 | 2025-07-30 23:08:00 +0000 | [diff] [blame] | 10 | 2. `browser_eval` - Evaluate JavaScript in the browser context |
| 11 | 3. `browser_screenshot` - Take a screenshot of the page or a specific element |
| Philip Zeyliger | 33d282f | 2025-05-03 04:01:54 +0000 | [diff] [blame] | 12 | |
| 13 | ## Usage |
| 14 | |
| 15 | ```go |
| 16 | // Create a context |
| 17 | ctx := context.Background() |
| 18 | |
| 19 | // Register browser tools and get a cleanup function |
| 20 | tools, cleanup := browse.RegisterBrowserTools(ctx) |
| 21 | defer cleanup() // Important: always call cleanup to release browser resources |
| 22 | |
| 23 | // Add tools to your agent |
| 24 | for _, tool := range tools { |
| 25 | agent.AddTool(tool) |
| 26 | } |
| 27 | ``` |
| 28 | |
| 29 | ## Requirements |
| 30 | |
| 31 | - Chrome or Chromium must be installed on the system |
| Philip Zeyliger | 9500617 | 2025-05-28 20:05:46 -0700 | [diff] [blame] | 32 | - In Docker environments, the multi-stage build automatically provides headless-shell from chromedp/headless-shell |
| 33 | - For local development, install Chrome/Chromium manually |
| Philip Zeyliger | 33d282f | 2025-05-03 04:01:54 +0000 | [diff] [blame] | 34 | - The `chromedp` package handles launching and controlling the browser |
| 35 | |
| 36 | ## Tool Input/Output |
| 37 | |
| 38 | All tools follow a standard JSON input/output format. For example: |
| 39 | |
| 40 | **Navigate Tool Input:** |
| 41 | ```json |
| 42 | { |
| 43 | "url": "https://example.com" |
| 44 | } |
| 45 | ``` |
| 46 | |
| 47 | **Navigate Tool Output (success):** |
| 48 | ```json |
| 49 | { |
| 50 | "status": "success" |
| 51 | } |
| 52 | ``` |
| 53 | |
| 54 | **Tool Output (error):** |
| 55 | ```json |
| 56 | { |
| 57 | "status": "error", |
| 58 | "error": "Error message" |
| 59 | } |
| 60 | ``` |
| 61 | |
| 62 | ## Example Tool Usage |
| 63 | |
| 64 | ```go |
| 65 | // Example of using the navigate tool directly |
| 66 | navTool := tools[0] // Get browser_navigate tool |
| 67 | input := map[string]string{"url": "https://example.com"} |
| 68 | inputJSON, _ := json.Marshal(input) |
| 69 | |
| 70 | // Call the tool |
| 71 | result, err := navTool.Run(ctx, json.RawMessage(inputJSON)) |
| 72 | if err != nil { |
| 73 | log.Fatalf("Error: %v", err) |
| 74 | } |
| 75 | fmt.Println(result) |
| 76 | ``` |
| 77 | |
| 78 | ## Screenshot Storage |
| 79 | |
| 80 | 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: |
| 81 | |
| 82 | 1. Reducing token usage in LLM responses |
| 83 | 2. Avoiding encoding/decoding overhead |
| 84 | 3. Allowing for larger screenshots without message size limitations |
| 85 | |
| 86 | ### How It Works |
| 87 | |
| 88 | 1. When a screenshot is taken, it's saved to `/tmp/sketch-screenshots/` with a unique UUID filename |
| 89 | 2. The tool returns the screenshot ID in its response |
| 90 | 3. The web UI can fetch the screenshot using the `/screenshot/{id}` endpoint |
| 91 | |
| 92 | ### Example Usage |
| 93 | |
| 94 | Agent calls the screenshot tool: |
| 95 | ```json |
| 96 | { |
| 97 | "id": "tool_call_123", |
| 98 | "name": "browser_screenshot", |
| 99 | "params": {} |
| 100 | } |
| 101 | ``` |
| 102 | |
| 103 | Tool response: |
| 104 | ```json |
| 105 | { |
| 106 | "id": "tool_call_123", |
| 107 | "result": { |
| 108 | "id": "550e8400-e29b-41d4-a716-446655440000" |
| 109 | } |
| 110 | } |
| 111 | ``` |
| 112 | |
| 113 | The screenshot is then accessible at: `/screenshot/550e8400-e29b-41d4-a716-446655440000` |