Skip to content

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

fix(mcp): route and capture the snap follow-up snapshot through one helper

Luigi Agosti42ecb173531e
diff --git a/internal/mcp/handlers_interaction.go b/internal/mcp/handlers_interaction.go
index 855862d7..244754c7 100644
--- a/internal/mcp/handlers_interaction.go
+++ b/internal/mcp/handlers_interaction.go
@@ -3,7 +3,6 @@ package mcp
import (
"context"
"fmt"
- "net/url"
"strings"
"github.com/mark3labs/mcp-go/mcp"
@@ -290,22 +289,9 @@ func handleAction(c *Client, kind string) func(context.Context, mcp.CallToolRequ
return resultFromBytes(body, code)
}
- snapSupportedKinds := map[string]bool{"click": true, "fill": true, "select": true}
if snapSupportedKinds[kind] {
- if snap, ok := optBool(r, "snap"); ok && snap {
- q := url.Values{}
- q.Set("filter", "interactive")
- q.Set("format", "compact")
- if tabID := optString(r, "tabId"); tabID != "" {
- q.Set("tabId", tabID)
- }
- snapBody, _, snapErr := c.GetCapturingVocab(ctx, "/snapshot", q, optString(r, "tabId"))
- if snapErr == nil {
- return mcp.NewToolResultText(string(body) + "\n" + string(snapBody)), nil
- }
- }
+ return withOptionalSnapshot(ctx, c, r, optString(r, "tabId"), body, code)
}
-
return resultFromBytes(body, code)
}
}
@@ -347,3 +333,9 @@ func handleKeyboard(c *Client) func(context.Context, mcp.CallToolRequest) (*mcp.
return resultFromBytes(body, code)
}
}
+
+// snapSupportedKinds are the interaction kinds whose tools declare snap. Only
+// these follow the action with a snapshot; mcp-go does not reject an undeclared
+// argument, so a snap sent to hover or scroll must not silently change what the
+// tool returns.
+var snapSupportedKinds = map[string]bool{"click": true, "fill": true, "select": true}
diff --git a/internal/mcp/handlers_navigation.go b/internal/mcp/handlers_navigation.go
index 2eadc02f..1a91ce97 100644
--- a/internal/mcp/handlers_navigation.go
+++ b/internal/mcp/handlers_navigation.go
@@ -57,7 +57,7 @@ func withOptionalSnapshot(ctx context.Context, c *Client, r mcp.CallToolRequest,
} else if returnedTabID := responseStringField(body, "tabId"); returnedTabID != "" {
q.Set("tabId", returnedTabID)
}
- snapBody, _, snapErr := c.Get(ctx, "/snapshot", routedQuery(r, q))
+ snapBody, _, snapErr := c.GetCapturingVocab(ctx, "/snapshot", routedQuery(r, q), tabID)
if snapErr != nil {
return resultFromBytes(body, code)
}
diff --git a/internal/mcp/vocab_echo_test.go b/internal/mcp/vocab_echo_test.go
index c2d6381e..e8cd2ed6 100644
--- a/internal/mcp/vocab_echo_test.go
+++ b/internal/mcp/vocab_echo_test.go
@@ -6,6 +6,8 @@ import (
"io"
"net/http"
"net/http/httptest"
+ "net/url"
+ "sync"
"testing"
"github.com/mark3labs/mcp-go/mcp"
@@ -109,3 +111,74 @@ func TestTheEchoedTokenIsScopedToItsTab(t *testing.T) {
t.Errorf("an action on t2 echoed t1's vocabulary token %q", got)
}
}
+
+// rotatingVocabServer hands out a fresh token on each /snapshot and records the
+// query every snapshot request carried, so a test can see both which token the
+// client kept and which instance it asked.
+func rotatingVocabServer(tokens ...string) (*httptest.Server, *[]url.Values) {
+ var seen []url.Values
+ var mu sync.Mutex
+ next := 0
+ return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path == "/snapshot" {
+ mu.Lock()
+ seen = append(seen, r.URL.Query())
+ if next < len(tokens) {
+ w.Header().Set(vocabHeader, tokens[next])
+ next++
+ }
+ mu.Unlock()
+ }
+ resp := map[string]any{"path": r.URL.Path}
+ if r.Method == http.MethodPost {
+ body, _ := io.ReadAll(r.Body)
+ var parsed map[string]any
+ if json.Unmarshal(body, &parsed) == nil {
+ resp["body"] = parsed
+ }
+ }
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(200)
+ _ = json.NewEncoder(w).Encode(resp)
+ })), &seen
+}
+
+// snap=true renumbers the tab, so the snapshot it returns must be the one the next
+// action echoes. When the navigation's own snapshot was fetched without capturing
+// its token, the client kept the token an earlier pinchtab_snapshot left behind and
+// the server answered the very refs it had just handed out with 409 vocab_superseded.
+func TestNavigateWithSnapCapturesTheTokenItReturns(t *testing.T) {
+ srv, _ := rotatingVocabServer("vocab-1", "vocab-2")
+ defer srv.Close()
+ c := NewClient(srv.URL, "")
+
+ callSharedTool(t, c, "pinchtab_snapshot", map[string]any{"tabId": "t1"})
+ callSharedTool(t, c, "pinchtab_navigate", map[string]any{"url": "https://example.com", "tabId": "t1", "snap": true})
+ clickResult := callSharedTool(t, c, "pinchtab_click", map[string]any{"ref": "e5", "tabId": "t1"})
+
+ got, ok := actionVocabSent(t, clickResult)
+ if !ok {
+ t.Fatal("the click carried no vocab token")
+ }
+ if got != "vocab-2" {
+ t.Errorf("click echoed vocab %q, want the token the navigation's own snapshot returned; the refs the agent read came from that snapshot", got)
+ }
+}
+
+// The follow-up snapshot decides which instance answers, and only the query is read
+// when routing. Fetching it unrouted described a different browser's page under the
+// action's result — and stashed that page's vocabulary under this tab.
+func TestClickWithSnapRoutesItsFollowUpSnapshotToTheSameBrowser(t *testing.T) {
+ srv, seen := rotatingVocabServer("vocab-1")
+ defer srv.Close()
+ c := NewClient(srv.URL, "")
+
+ callSharedTool(t, c, "pinchtab_click", map[string]any{"ref": "e5", "tabId": "t1", "browser": "cloak", "snap": true})
+
+ if len(*seen) != 1 {
+ t.Fatalf("got %d snapshot requests, want 1", len(*seen))
+ }
+ if browser := (*seen)[0].Get("browser"); browser != "cloak" {
+ t.Errorf("the follow-up snapshot asked browser=%q, want cloak; routing reads the query and nothing else", browser)
+ }
+}