High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: MCP maps a remedy verb to its tool via an explicit table, not pinchtab_<verb> convention (PIN-388)
Luigi Agosti0b584dc84283
diff --git a/cmd/pinchtab/remedy_mcp_translation_test.go b/cmd/pinchtab/remedy_mcp_translation_test.gonew file mode 100644index 00000000..78eaf351--- /dev/null+++ b/cmd/pinchtab/remedy_mcp_translation_test.go@@ -0,0 +1,66 @@+package main++import (+ "testing"++ "github.com/pinchtab/pinchtab/internal/mcp"+ "github.com/pinchtab/pinchtab/internal/remedy"+)++// remedyVerbsWithoutMCPTool records the pinchtab CLI verbs declared remedies use that have+// no MCP tool counterpart. Each is a decision, not a gap: the MCP surface exposes browser+// control, not server/session/config/profile administration or the download path.+var remedyVerbsWithoutMCPTool = map[string]string{+ "server": "server lifecycle is CLI/HTTP only",+ "session": "agent-session administration is CLI/HTTP only",+ "config": "config editing is CLI/HTTP only",+ "profiles": "profile management is CLI only",+ "download": "no pinchtab_download tool; downloads are a CLI/HTTP path",+}++// A declared remedy is one pinchtab CLI line the CLI renders verbatim; an MCP agent runs+// tools, so the funnel translates the remedy's verb into its MCP tool. This walks every+// remedy declared in the whole binary and pins that each verb either maps to a registered+// tool with the named parameter, or is a recorded no-counterpart verb — so a new remedy+// whose verb has an MCP tool cannot ship untranslated (nav, snap) or mis-translated (dialog).+//+// It reads remedy.Templates() rather than a producer list, so a remedy declared anywhere in+// the binary's import graph is covered; the floor guards against the walk losing its+// producer packages and passing vacuously.+func TestEveryRemedyVerbMapsToItsMCPToolOrIsRecordedCounterpartFree(t *testing.T) {+ declared := remedy.Templates()+ if len(declared) < 8 {+ t.Fatalf("only %d remedies declared (%v); the walk lost its producer packages and would pass vacuously", len(declared), declared)+ }++ seen := map[string]bool{}+ for _, line := range declared {+ for _, words := range remedy.Segments(line) {+ if len(words) < 2 {+ continue+ }+ verb := words[1]+ seen[verb] = true++ name, param, ok := mcp.RemedyToolForVerb(verb)+ if ok {+ if !mcp.ToolInputHasParam(name, param) {+ t.Errorf("remedy verb %q maps to tool %q param %q, which is not a registered tool with that parameter", verb, name, param)+ }+ continue+ }+ if _, recorded := remedyVerbsWithoutMCPTool[verb]; !recorded {+ t.Errorf("remedy %q uses verb %q with no MCP tool mapping and no recorded no-counterpart entry; add it to the MCP verb table or record it here with a reason", line, verb)+ }+ }+ }++ for verb, reason := range remedyVerbsWithoutMCPTool {+ if reason == "" {+ t.Errorf("no-counterpart verb %q is recorded without a reason", verb)+ }+ if !seen[verb] {+ t.Errorf("no-counterpart verb %q is recorded but no declared remedy uses it; drop the stale entry", verb)+ }+ }+}diff --git a/internal/mcp/handlers_helpers.go b/internal/mcp/handlers_helpers.goindex 7cac095e..ffd1002d 100644--- a/internal/mcp/handlers_helpers.go+++ b/internal/mcp/handlers_helpers.go@@ -268,11 +268,31 @@ func resultFromBytes(body []byte, code int) (*mcp.CallToolResult, error) {return mcp.NewToolResultText(string(body)), nil}-// mcpRemedyGuidance turns a refusal's details.remedy — one pinchtab CLI line the CLI-// renders verbatim — into guidance an MCP agent can act on, which runs tools rather than-// a shell. It fires for any remedy whose verb has a matching tool (resume → pinchtab_resume),-// so every future single-command remedy is served, not just handoff; a verb with no matching-// tool, or a multi-command remedy, yields nothing and the raw body stands alone.+// remedyTool is the MCP tool that performs a pinchtab CLI verb, and the tool parameter+// its single positional value fills.+type remedyTool struct {+ name string+ param string+}++// remedyVerbTools maps a pinchtab CLI verb to its MCP tool EXPLICITLY, because neither the+// name nor the argument follows from the verb: nav's tool is pinchtab_navigate, snap's is+// pinchtab_snapshot, and dialog's positional is its action, not a tab id. A verb absent here+// has no MCP counterpart. RemedyToolForVerb and the whole-binary remedy census pin that+// every declared remedy verb is either in this table — naming a registered tool and a real+// parameter — or recorded as counterpart-free, so a new remedy cannot ship untranslated.+var remedyVerbTools = map[string]remedyTool{+ "resume": {"pinchtab_resume", "tabId"},+ "dialog": {"pinchtab_dialog", "action"},+ "nav": {"pinchtab_navigate", "url"},+ "back": {"pinchtab_back", ""},+ "snap": {"pinchtab_snapshot", ""},+}++// mcpRemedyGuidance turns a refusal's details.remedy — one pinchtab CLI line the CLI renders+// verbatim — into guidance an MCP agent can act on, which runs tools rather than a shell.+// A verb without an MCP counterpart, or a multi-command remedy, yields nothing and the raw+// body stands alone.func mcpRemedyGuidance(body []byte) string {segments := remedy.Segments(remedyLine(body))if len(segments) != 1 {@@ -282,14 +302,40 @@ func mcpRemedyGuidance(body []byte) string {if len(words) < 2 {return ""}- tool := "pinchtab_" + words[1]- if !isRegisteredTool(tool) {+ tool, ok := remedyVerbTools[words[1]]+ if !ok {return ""}- if arg := firstPositional(words[2:]); arg != "" {- return fmt.Sprintf("call %s with tabId %s", tool, arg)+ if tool.param != "" {+ if arg := firstPositional(words[2:]); arg != "" {+ return fmt.Sprintf("call %s with %s %s", tool.name, tool.param, arg)+ }+ }+ return "call " + tool.name+}++// RemedyToolForVerb reports the MCP tool a pinchtab CLI verb maps to, and the tool parameter+// its positional value fills (empty when the tool takes none). It is the seam the whole-binary+// remedy census reads to prove the mapping stays complete and correct.+func RemedyToolForVerb(verb string) (name, param string, ok bool) {+ tool, ok := remedyVerbTools[verb]+ return tool.name, tool.param, ok+}++// ToolInputHasParam reports whether name is a registered tool and, when param is non-empty,+// whether that tool declares param in its input schema.+func ToolInputHasParam(name, param string) bool {+ for _, tool := range allTools() {+ if tool.Name != name {+ continue+ }+ if param == "" {+ return true+ }+ _, ok := tool.InputSchema.Properties[param]+ return ok}- return "call " + tool+ return false}func remedyLine(body []byte) string {@@ -313,15 +359,6 @@ func firstPositional(words []string) string {return ""}-func isRegisteredTool(name string) bool {- for _, tool := range allTools() {- if tool.Name == name {- return true- }- }- return false-}-// reportsNoSuccess is the funnel's body-level failure rule: an endpoint that answers 200// while reporting it achieved nothing must reach the agent as an error, or the agent// confirms work the browser never did — the cookie-set path shipped exactly that.diff --git a/internal/mcp/handlers_helpers_test.go b/internal/mcp/handlers_helpers_test.goindex 0f8c05c0..c24c9b68 100644--- a/internal/mcp/handlers_helpers_test.go+++ b/internal/mcp/handlers_helpers_test.go@@ -216,33 +216,51 @@ func TestEveryFailedKeyProducerHasARecordedFunnelDecision(t *testing.T) {// A refusal's details.remedy is one pinchtab CLI line the CLI renders verbatim, but an// MCP agent runs tools, not a shell. The funnel must translate the remedy's verb into the-// matching tool so the agent can recover; the handoff refusal is the case that shipped the-// unrunnable "POST /tabs/{id}/resume" instruction to MCP.+// matching tool and name the parameter its value fills — not by the pinchtab_<verb>+// convention, which mis-named the tool (nav → pinchtab_navigate) and mis-labelled the+// argument (dialog's positional is its action, not a tab id).func TestMCPRefusalTranslatesTheRemedyIntoAToolCall(t *testing.T) {- body := []byte(`{"error":"tab tab1 is paused for human handoff (manual_handoff)",` +- `"code":"tab_paused_handoff",` +- `"details":{"hint":"...then call POST /tabs/{id}/resume to continue","remedy":"pinchtab resume tab1"}}`)-- result, err := resultFromBytes(body, http.StatusConflict)- if err != nil {- t.Fatalf("resultFromBytes: %v", err)- }- if !result.IsError {- t.Fatal("a 409 refusal must reach the agent as an error")- }- text := resultText(t, result)- if !strings.Contains(text, "call pinchtab_resume with tabId tab1") {- t.Fatalf("MCP refusal does not name the resume tool: %q", text)+ for _, tc := range []struct {+ name, remedy, want string+ }{+ {"resume", "pinchtab resume tab1", "call pinchtab_resume with tabId tab1"},+ {"dialog action, not a tab", "pinchtab dialog accept", "call pinchtab_dialog with action accept"},+ {"nav renames the tool and fills url", "pinchtab nav https://example.test", "call pinchtab_navigate with url https://example.test"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ body := []byte(`{"code":"x","details":{"remedy":"` + tc.remedy + `"}}`)+ result, err := resultFromBytes(body, http.StatusConflict)+ if err != nil {+ t.Fatalf("resultFromBytes: %v", err)+ }+ if !result.IsError {+ t.Fatal("a 409 refusal must reach the agent as an error")+ }+ if text := resultText(t, result); !strings.Contains(text, tc.want) {+ t.Fatalf("MCP guidance = %q, want it to contain %q", text, tc.want)+ }+ })}- // The mapping is real, not a bare "pinchtab_"+verb: a remedy whose verb has no- // matching tool (nav → pinchtab_navigate, not pinchtab_nav) adds no guidance.- noTool := []byte(`{"code":"x","details":{"remedy":"pinchtab nav https://example.test"}}`)+ // A verb with no MCP counterpart (config, server, session, …) adds no guidance;+ // the raw body stands alone rather than inventing a pinchtab_config tool.+ noTool := []byte(`{"code":"x","details":{"remedy":"pinchtab config set security.allowCookies true"}}`)noToolResult, err := resultFromBytes(noTool, http.StatusConflict)if err != nil {t.Fatalf("resultFromBytes: %v", err)}if quiet := resultText(t, noToolResult); strings.Contains(quiet, "call pinchtab_") {- t.Fatalf("a verb with no matching tool produced tool guidance: %q", quiet)+ t.Fatalf("a verb with no MCP counterpart produced tool guidance: %q", quiet)+ }+}++// Every mapping entry must name a registered tool and a real parameter of it, or the guidance+// cites a tool or argument the agent's client does not expose. The whole-binary census+// (cmd/pinchtab) proves the other direction — that every declared remedy verb is covered.+func TestRemedyVerbToolsNameRealToolParameters(t *testing.T) {+ for verb, tool := range remedyVerbTools {+ if !ToolInputHasParam(tool.name, tool.param) {+ t.Errorf("verb %q maps to tool %q param %q, which is not a registered tool with that parameter", verb, tool.name, tool.param)+ }}}