Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

feat: route /capture and pinchtab_capture per-browser

Luigi Agosti0e3ca05274b5
diff --git a/docs/reference/mcp-tools.md b/docs/reference/mcp-tools.md
index 0a6b9915..76ea5e78 100644
--- a/docs/reference/mcp-tools.md
+++ b/docs/reference/mcp-tools.md
@@ -26,8 +26,8 @@ Structured semantic locators are matched by the semantic engine; CSS, XPath, ref
| `pinchtab_navigate` | `url` required, `tabId`, `snap` | Uses `/navigate`; omitting `tabId` opens a new tab. `snap=true` returns an interactive compact snapshot in the same response |
| `pinchtab_snapshot` | `tabId`, `interactive`, `compact`, `format`, `diff`, `selector`, `maxTokens`, `depth`, `noAnimations` | `selector` scopes the snapshot; `format` is limited to `compact` or `text` |
| `pinchtab_frame` | `tabId`, `target` | Get or set the frame scope for selector-based actions on the tab; `target` accepts `main`, a snapshot ref, an iframe selector, or a frame name/URL |
-| `pinchtab_screenshot` | `tabId`, `selector`, `scale`, `format`, `quality`, `annotate`, `beyondViewport` | `selector` captures a specific element in current frame scope; `scale` rescales the output bitmap (e.g. `0.5` = half size); `format` is `jpeg` or `png`; `annotate=true` overlays numbered ref boxes and populates the annotations envelope; `beyondViewport=true` captures the full scrollable document (ignored when `selector` is set) — box coords are document-relative in that mode |
-| `pinchtab_capture` | `tabId`, `selector`, `filter`, `format`, `quality`, `depth`, `scale`, `wait`, `withBounds`, `beyondViewport`, `requirePair`, `noAnimations` | Paired screenshot + accessibility snapshot from the same DOM epoch. Returns an image content block plus a JSON envelope with `epoch`, `pairing.navigated`, per-node `boundingBox`, and `image.coordinateSpace` (`viewport`, `document`, or selector `clip`). Use when the model reads pixels AND acts on refs in the same turn. |
+| `pinchtab_screenshot` | `tabId`, `selector`, `scale`, `format`, `quality`, `annotate`, `beyondViewport`, `browser` | `selector` captures a specific element in current frame scope; `scale` rescales the output bitmap (e.g. `0.5` = half size); `format` is `jpeg` or `png`; `annotate=true` overlays numbered ref boxes and populates the annotations envelope; `beyondViewport=true` captures the full scrollable document (ignored when `selector` is set) — box coords are document-relative in that mode; `browser` selects the browser (e.g. `chrome`, `cloak`) for this request |
+| `pinchtab_capture` | `tabId`, `selector`, `filter`, `format`, `quality`, `depth`, `scale`, `wait`, `withBounds`, `beyondViewport`, `requirePair`, `noAnimations`, `browser` | Paired screenshot + accessibility snapshot from the same DOM epoch. Returns an image content block plus a JSON envelope with `epoch`, `pairing.navigated`, per-node `boundingBox`, and `image.coordinateSpace` (`viewport`, `document`, or selector `clip`). `browser` selects the browser (e.g. `chrome`, `cloak`); the static ghost-chrome runtime cannot paint, so it falls back to chrome. Use when the model reads pixels AND acts on refs in the same turn. |
| `pinchtab_get_text` | `tabId`, `raw`, `format`, `maxChars` | `raw=true` maps to `/text?mode=raw`; `format=text/plain` returns plain text; inherits the current `pinchtab_frame` scope for that tab |
## Interaction
diff --git a/internal/handlers/capture.go b/internal/handlers/capture.go
index 7fe02e2c..06df2d64 100644
--- a/internal/handlers/capture.go
+++ b/internal/handlers/capture.go
@@ -3,17 +3,24 @@ package handlers
import (
"context"
"encoding/base64"
+ "errors"
"fmt"
"log/slog"
"net/http"
"os"
"path/filepath"
"strconv"
+ "strings"
"time"
"github.com/chromedp/cdproto/page"
+ "github.com/pinchtab/pinchtab/internal/activity"
"github.com/pinchtab/pinchtab/internal/bridge"
+ "github.com/pinchtab/pinchtab/internal/browserops"
+ "github.com/pinchtab/pinchtab/internal/browsers"
+ "github.com/pinchtab/pinchtab/internal/config"
"github.com/pinchtab/pinchtab/internal/httpx"
+ "github.com/pinchtab/pinchtab/internal/session"
)
// HandleCapture returns a screenshot and an accessibility snapshot from the
@@ -42,11 +49,70 @@ import (
func (h *Handlers) HandleCapture(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
tabID := q.Get("tabId")
+
+ // Browser resolution: request > session > instance > global default > chrome.
+ requestBrowser := strings.TrimSpace(q.Get("browser"))
+ var sessionBrowser string
+ if sess, ok := session.FromRequest(r); ok && sess != nil {
+ sessionBrowser = sess.Browser
+ }
+ var instanceBrowser string
+ if tabID != "" && h.Orchestrator != nil {
+ if inst, ok := h.Orchestrator.FindInstanceByTab(tabID); ok && inst != nil && inst.Browser != "" {
+ instanceBrowser = inst.Browser
+ }
+ }
+
+ browser := config.ResolveBrowser(requestBrowser, sessionBrowser, instanceBrowser, h.Config.DefaultBrowser, h.Config.BrowsersAvailable)
+ if browser != config.BrowserChrome {
+ if _, err := config.ParseBrowser(browser, h.Config.BrowsersAvailable); err != nil {
+ httpx.Error(w, http.StatusBadRequest, err)
+ return
+ }
+ }
+
+ // /capture pairs a screenshot with an accessibility snapshot from the same
+ // DOM epoch — a visual operation. The static (ghost-chrome) runtime cannot
+ // paint, so it skips ShapeVisual and we fall back to Chrome.
+ handleDecision, err := checkBrowserCanHandle(browser, browsers.RequestIntent{
+ Shape: browsers.ShapeVisual,
+ })
+ if err != nil {
+ httpx.Error(w, http.StatusBadRequest, err)
+ return
+ }
+ if handleDecision.Decision == browsers.DecisionSkip {
+ browser = config.BrowserChrome
+ }
+
+ // Resolve the effective config with target-specific overrides merged in.
+ effectiveCfg, err := h.resolveEffectiveConfig(browser)
+ if err != nil {
+ var ambErr *config.AmbiguousBrowserError
+ if errors.As(err, &ambErr) {
+ httpx.ErrorCode(w, http.StatusBadRequest, "browser_ambiguous", err.Error(), false, map[string]any{
+ "browser": ambErr.Browser,
+ "targets": ambErr.Targets,
+ })
+ } else {
+ httpx.Error(w, http.StatusBadRequest, err)
+ }
+ return
+ }
+
h.recordReadRequest(r, "capture", tabID)
- // /capture is Chrome-only: it pairs a screenshot with an accessibility
- // snapshot from the same DOM epoch, which the static (ghost-chrome) runtime
- // cannot produce. Go straight to chrome.
+ captureRoute := browserops.SingleBrowserRoute("chrome")
+ captureRoute.Attempts = append(captureRoute.Attempts, browserops.RouteAttempt{
+ Browser: browser,
+ Accepted: handleDecision.Decision == browsers.DecisionHandle,
+ Reason: handleDecision.Reason,
+ })
+ if requestBrowser != "" {
+ captureRoute.RequestedBrowser = requestBrowser
+ }
+ h.recordActivity(r, activity.Update{Route: captureRoute})
+
if err := h.ensureChrome(); err != nil {
if h.writeBridgeUnavailable(w, err) {
return
@@ -122,7 +188,7 @@ func (h *Handlers) HandleCapture(w http.ResponseWriter, r *http.Request) {
return
}
defer h.armAutoCloseIfEnabled(resolvedTabID)
- tCtx, tCancel := context.WithTimeout(ctx, h.Config.ActionTimeout)
+ tCtx, tCancel := context.WithTimeout(ctx, effectiveCfg.ActionTimeout)
defer tCancel()
go httpx.CancelOnClientDone(r.Context(), tCancel)
diff --git a/internal/handlers/navigation_browser_test.go b/internal/handlers/navigation_browser_test.go
index 8cb4c7f4..88f501a6 100644
--- a/internal/handlers/navigation_browser_test.go
+++ b/internal/handlers/navigation_browser_test.go
@@ -108,6 +108,23 @@ func TestHandleSnapshot_BrowserParam_Invalid_Returns400(t *testing.T) {
}
}
+func TestHandleCapture_BrowserParam_Invalid_Returns400(t *testing.T) {
+ h := New(&mockBridge{}, &config.RuntimeConfig{
+ BrowsersAvailable: []string{"chrome"},
+ }, nil, nil, nil)
+
+ req := httptest.NewRequest("GET", "/capture?browser=invalid", nil)
+ w := httptest.NewRecorder()
+ h.HandleCapture(w, req)
+
+ if w.Code != http.StatusBadRequest {
+ t.Fatalf("expected 400 for invalid browser, got %d body=%s", w.Code, w.Body.String())
+ }
+ if !strings.Contains(w.Body.String(), "unknown browser") {
+ t.Fatalf("expected 'unknown browser' in error, got %s", w.Body.String())
+ }
+}
+
func TestHandleAction_BrowserParam_Invalid_Returns400(t *testing.T) {
h := New(&mockBridge{}, &config.RuntimeConfig{
BrowsersAvailable: []string{"chrome"},
diff --git a/internal/mcp/handlers_navigation.go b/internal/mcp/handlers_navigation.go
index 2b25a276..6f88969a 100644
--- a/internal/mcp/handlers_navigation.go
+++ b/internal/mcp/handlers_navigation.go
@@ -282,6 +282,9 @@ func handleCapture(c *Client) func(context.Context, mcp.CallToolRequest) (*mcp.C
if v, ok := optBool(r, "noAnimations"); ok && v {
q.Set("noAnimations", "true")
}
+ if browser := optString(r, "browser"); browser != "" {
+ q.Set("browser", browser)
+ }
body, code, err := c.Get(ctx, "/capture", q)
if err != nil {
diff --git a/internal/mcp/handlers_navigation_test.go b/internal/mcp/handlers_navigation_test.go
index 08252dd9..65b45dc9 100644
--- a/internal/mcp/handlers_navigation_test.go
+++ b/internal/mcp/handlers_navigation_test.go
@@ -511,3 +511,24 @@ func TestHandleGetTextMaxCharsZeroIgnored(t *testing.T) {
t.Errorf("maxChars should not be sent when not specified, got %s", text)
}
}
+
+func TestHandleCaptureForwardsBrowser(t *testing.T) {
+ encoded := base64.StdEncoding.EncodeToString([]byte("JPEGBYTES"))
+ var captureQuery url.Values
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ captureQuery = r.URL.Query()
+ w.Header().Set("Content-Type", "application/json")
+ _ = json.NewEncoder(w).Encode(map[string]any{
+ "image": map[string]any{"format": "jpeg", "base64": encoded},
+ })
+ }))
+ defer srv.Close()
+
+ r := callTool(t, "pinchtab_capture", map[string]any{"browser": "cloak"}, srv)
+ if r.IsError {
+ t.Fatalf("unexpected error: %s", resultText(t, r))
+ }
+ if got := captureQuery.Get("browser"); got != "cloak" {
+ t.Fatalf("capture browser = %q, want cloak; query=%v", got, captureQuery)
+ }
+}
diff --git a/internal/mcp/tools.go b/internal/mcp/tools.go
index b5a0792b..3c5b78d8 100644
--- a/internal/mcp/tools.go
+++ b/internal/mcp/tools.go
@@ -61,6 +61,8 @@ func allTools() []mcp.Tool {
mcp.WithBoolean("beyondViewport", mcp.Description("Capture the full scrollable document instead of just the visible viewport. Coordinate space becomes 'document'; bounding boxes use page coords.")),
mcp.WithBoolean("requirePair", mcp.Description("Return 409 if the main frame navigates during the capture window (loaderId changes).")),
mcp.WithBoolean("noAnimations", mcp.Description("Inject prefers-reduced-motion CSS for the capture window")),
+ mcp.WithString("browser",
+ mcp.Description("Browser to use for this request (e.g. chrome, cloak). The static ghost-chrome runtime cannot paint, so it falls back to chrome.")),
),
mcp.NewTool("pinchtab_get_text",
mcp.WithDescription("Extract readable text content from the current page. Inherits the current frame scope for the tab unless no frame is selected."),
diff --git a/skills/pinchtab/references/mcp.md b/skills/pinchtab/references/mcp.md
index 16db7d53..d57fc7a3 100644
--- a/skills/pinchtab/references/mcp.md
+++ b/skills/pinchtab/references/mcp.md
@@ -53,8 +53,8 @@ All tool names are prefixed with `pinchtab_`.
|------|-------------|
| `pinchtab_navigate` | Navigate to a URL. Required param: `url`. Optional: `tabId`. |
| `pinchtab_snapshot` | Accessibility tree. Optional: `interactive`, `compact`, `format` (`compact` or `text`), `diff`, `selector`, `maxTokens`, `depth`, `noAnimations`, `tabId`. |
-| `pinchtab_screenshot` | Capture screenshot. Optional: `format` (`jpeg` default, `png`), `quality`, `selector`, `scale`, `annotate`, `beyondViewport`, `tabId`. Returns an MCP image content block (rendered inline by clients) plus a stable JSON text block `{"format", "annotations": [...]}`; `annotations` is `[]` by default and is populated with `{ref, role, name, tag, box {x,y,w,h}}` entries when `annotate=true` so refs in the picture map back to selectors. `beyondViewport=true` captures the full scrollable page (ignored when `selector` is set) and returns document-relative box coords. |
-| `pinchtab_capture` | Paired screenshot + accessibility snapshot from the same DOM epoch. Optional: `selector`, `filter`, `format`, `quality`, `depth`, `wait` (`stable`/`load`/`none`), `withBounds`, `beyondViewport`, `requirePair`, `noAnimations`, `tabId`. Returns an MCP image content block plus a JSON envelope with `epoch.domEpoch`, `pairing.navigated`, `image.coordinateSpace`, and per-node `boundingBox`. Use this instead of `pinchtab_screenshot` + `pinchtab_snapshot` when the model reads pixels AND acts on refs in the same turn. |
+| `pinchtab_screenshot` | Capture screenshot. Optional: `format` (`jpeg` default, `png`), `quality`, `selector`, `scale`, `annotate`, `beyondViewport`, `browser`, `tabId`. Returns an MCP image content block (rendered inline by clients) plus a stable JSON text block `{"format", "annotations": [...]}`; `annotations` is `[]` by default and is populated with `{ref, role, name, tag, box {x,y,w,h}}` entries when `annotate=true` so refs in the picture map back to selectors. `beyondViewport=true` captures the full scrollable page (ignored when `selector` is set) and returns document-relative box coords. `browser` selects the browser (e.g. `chrome`, `cloak`). |
+| `pinchtab_capture` | Paired screenshot + accessibility snapshot from the same DOM epoch. Optional: `selector`, `filter`, `format`, `quality`, `depth`, `wait` (`stable`/`load`/`none`), `withBounds`, `beyondViewport`, `requirePair`, `noAnimations`, `browser`, `tabId`. Returns an MCP image content block plus a JSON envelope with `epoch.domEpoch`, `pairing.navigated`, `image.coordinateSpace`, and per-node `boundingBox`. `browser` selects the browser (e.g. `chrome`, `cloak`); the static ghost-chrome runtime cannot paint, so it falls back to chrome. Use this instead of `pinchtab_screenshot` + `pinchtab_snapshot` when the model reads pixels AND acts on refs in the same turn. |
| `pinchtab_get_text` | Extract readable page text. Optional: `raw`, `format`, `maxChars`, `tabId`. |
### Interaction