Skip to content

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

fix(mcp): correct snapshot query params and expose maxTokens/depth/maxChars

Mario551859a05bb0
diff --git a/internal/mcp/handlers.go b/internal/mcp/handlers.go
index 16656181..05d1e079 100644
--- a/internal/mcp/handlers.go
+++ b/internal/mcp/handlers.go
@@ -86,6 +86,15 @@ func optBool(r mcp.CallToolRequest, key string) (bool, bool) {
return v, ok
}
+func optNumber(r mcp.CallToolRequest, key string) float64 {
+ v, _ := r.GetArguments()[key].(float64)
+ return v
+}
+
+func formatInt(v float64) string {
+ return fmt.Sprintf("%d", int(v))
+}
+
func resultFromBytes(body []byte, code int) (*mcp.CallToolResult, error) {
if code >= 400 {
return mcp.NewToolResultError(fmt.Sprintf("HTTP %d: %s", code, string(body))), nil
@@ -141,10 +150,10 @@ func handleSnapshot(c *Client) func(context.Context, mcp.CallToolRequest) (*mcp.
q.Set("tabId", tabID)
}
if v, ok := optBool(r, "interactive"); ok && v {
- q.Set("interactive", "true")
+ q.Set("filter", "interactive")
}
if v, ok := optBool(r, "compact"); ok && v {
- q.Set("compact", "true")
+ q.Set("format", "compact")
}
if v, ok := optBool(r, "diff"); ok && v {
q.Set("diff", "true")
@@ -152,6 +161,12 @@ func handleSnapshot(c *Client) func(context.Context, mcp.CallToolRequest) (*mcp.
if sel := optString(r, "selector"); sel != "" {
q.Set("selector", sel)
}
+ if v := optNumber(r, "maxTokens"); v > 0 {
+ q.Set("maxTokens", formatInt(v))
+ }
+ if v := optNumber(r, "depth"); v > 0 {
+ q.Set("depth", formatInt(v))
+ }
body, code, err := c.Get(ctx, "/snapshot", q)
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
@@ -189,6 +204,9 @@ func handleGetText(c *Client) func(context.Context, mcp.CallToolRequest) (*mcp.C
if v, ok := optBool(r, "raw"); ok && v {
q.Set("mode", "raw")
}
+ if v := optNumber(r, "maxChars"); v > 0 {
+ q.Set("maxChars", formatInt(v))
+ }
body, code, err := c.Get(ctx, "/text", q)
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
diff --git a/internal/mcp/handlers_test.go b/internal/mcp/handlers_test.go
index 68bb98fb..94b2728c 100644
--- a/internal/mcp/handlers_test.go
+++ b/internal/mcp/handlers_test.go
@@ -718,3 +718,137 @@ func TestHandleNetworkClearWithTab(t *testing.T) {
t.Errorf("expected /network/clear path, got %s", text)
}
}
+
+// ── Issue #396: snapshot sends wrong query params for interactive/compact ──
+
+func TestHandleSnapshotInteractiveSendsFilter(t *testing.T) {
+ srv := mockPinchTab()
+ defer srv.Close()
+
+ r := callTool(t, "pinchtab_snapshot", map[string]any{
+ "interactive": true,
+ }, srv)
+
+ text := resultText(t, r)
+ if !strings.Contains(text, `"filter"`) {
+ t.Errorf("expected 'filter' query param, got %s", text)
+ }
+ if strings.Contains(text, `"interactive"`) && !strings.Contains(text, `"filter"`) {
+ t.Error("handler sent ?interactive=true instead of ?filter=interactive")
+ }
+}
+
+func TestHandleSnapshotCompactSendsFormat(t *testing.T) {
+ srv := mockPinchTab()
+ defer srv.Close()
+
+ r := callTool(t, "pinchtab_snapshot", map[string]any{
+ "compact": true,
+ }, srv)
+
+ text := resultText(t, r)
+ if !strings.Contains(text, `"format"`) {
+ t.Errorf("expected 'format' query param, got %s", text)
+ }
+ if strings.Contains(text, `"compact"`) && !strings.Contains(text, `"format"`) {
+ t.Error("handler sent ?compact=true instead of ?format=compact")
+ }
+}
+
+func TestHandleSnapshotInteractiveCompactCombined(t *testing.T) {
+ srv := mockPinchTab()
+ defer srv.Close()
+
+ r := callTool(t, "pinchtab_snapshot", map[string]any{
+ "interactive": true,
+ "compact": true,
+ "selector": "#main",
+ }, srv)
+
+ text := resultText(t, r)
+ if !strings.Contains(text, `"filter"`) {
+ t.Errorf("expected 'filter' query param, got %s", text)
+ }
+ if !strings.Contains(text, `"format"`) {
+ t.Errorf("expected 'format' query param, got %s", text)
+ }
+}
+
+// ── Issue #397: maxTokens, depth, maxChars not exposed via MCP ─────────
+
+func TestHandleSnapshotMaxTokens(t *testing.T) {
+ srv := mockPinchTab()
+ defer srv.Close()
+
+ r := callTool(t, "pinchtab_snapshot", map[string]any{
+ "interactive": true,
+ "maxTokens": float64(300),
+ }, srv)
+
+ text := resultText(t, r)
+ if !strings.Contains(text, `"maxTokens"`) {
+ t.Errorf("expected 'maxTokens' query param, got %s", text)
+ }
+ if !strings.Contains(text, "300") {
+ t.Errorf("expected maxTokens=300 in query, got %s", text)
+ }
+}
+
+func TestHandleSnapshotDepth(t *testing.T) {
+ srv := mockPinchTab()
+ defer srv.Close()
+
+ r := callTool(t, "pinchtab_snapshot", map[string]any{
+ "depth": float64(3),
+ }, srv)
+
+ text := resultText(t, r)
+ if !strings.Contains(text, `"depth"`) {
+ t.Errorf("expected 'depth' query param, got %s", text)
+ }
+}
+
+func TestHandleSnapshotMaxTokensZeroIgnored(t *testing.T) {
+ srv := mockPinchTab()
+ defer srv.Close()
+
+ r := callTool(t, "pinchtab_snapshot", map[string]any{
+ "maxTokens": float64(0),
+ }, srv)
+
+ text := resultText(t, r)
+ if strings.Contains(text, `"maxTokens"`) {
+ t.Errorf("maxTokens=0 should not be sent, got %s", text)
+ }
+}
+
+func TestHandleGetTextMaxChars(t *testing.T) {
+ srv := mockPinchTab()
+ defer srv.Close()
+
+ r := callTool(t, "pinchtab_get_text", map[string]any{
+ "maxChars": float64(3000),
+ }, srv)
+
+ text := resultText(t, r)
+ if !strings.Contains(text, `"maxChars"`) {
+ t.Errorf("expected 'maxChars' query param, got %s", text)
+ }
+ if !strings.Contains(text, "3000") {
+ t.Errorf("expected maxChars=3000 in query, got %s", text)
+ }
+}
+
+func TestHandleGetTextMaxCharsZeroIgnored(t *testing.T) {
+ srv := mockPinchTab()
+ defer srv.Close()
+
+ r := callTool(t, "pinchtab_get_text", map[string]any{
+ "raw": true,
+ }, srv)
+
+ text := resultText(t, r)
+ if strings.Contains(text, `"maxChars"`) {
+ t.Errorf("maxChars should not be sent when not specified, got %s", text)
+ }
+}
diff --git a/internal/mcp/tools.go b/internal/mcp/tools.go
index 03babaf3..e9ae61aa 100644
--- a/internal/mcp/tools.go
+++ b/internal/mcp/tools.go
@@ -18,6 +18,8 @@ func allTools() []mcp.Tool {
mcp.WithBoolean("compact", mcp.Description("Compact format (most token-efficient)")),
mcp.WithBoolean("diff", mcp.Description("Only changes since last snapshot")),
mcp.WithString("selector", mcp.Description("Unified selector to scope the snapshot (CSS, XPath, text, or ref)")),
+ mcp.WithNumber("maxTokens", mcp.Description("Maximum estimated tokens in response (e.g. 300)")),
+ mcp.WithNumber("depth", mcp.Description("Maximum tree depth (e.g. 3)")),
),
mcp.NewTool("pinchtab_screenshot",
mcp.WithDescription("Take a screenshot of the current page"),
@@ -29,6 +31,7 @@ func allTools() []mcp.Tool {
mcp.WithDescription("Extract readable text content from the current page"),
mcp.WithString("tabId", mcp.Description("Target tab ID")),
mcp.WithBoolean("raw", mcp.Description("Return raw text without formatting")),
+ mcp.WithNumber("maxChars", mcp.Description("Maximum characters in response (e.g. 3000)")),
),
// ── Interaction ─────────────────────────────────────────────