Files
pad/cmd
xarmian 9607139340 feat(mcp): add pad_playbook tool (list/get/run) (TASK-1381) (#521)
* feat(mcp): add pad_playbook tool (list/get/run) (TASK-1381)

PLAN-1377 T4 — exposes the playbook surface from TASK-1382 via MCP.
Three passThrough actions match the CLI:

- pad_playbook.list → pad playbook list (metadata catalog)
- pad_playbook.get  → pad playbook show <ref> (full body + fields)
- pad_playbook.run  → pad playbook run <ref> (parse + bind args,
                        return body. Side-effect-free; the agent
                        executes the steps, not the server)

Params advertised in the tool schema: ref (required for get/run),
args (pre-parsed map — MCP / programmatic callers), and raw_args
(CLI-style tokens — strict parsing rules applied server-side via
ParsePlaybookCLIArgs from TASK-1382).

HTTP dispatcher route entries (dispatch_http_routes.go) wire the same
three actions into pad-cloud's in-process path:
  GET  /workspaces/{ws}/playbooks
  GET  /workspaces/{ws}/playbooks/{ref}
  POST /workspaces/{ws}/playbooks/{ref}/run

The run mapper (mapPlaybookRun) JSON-encodes args + raw_args into the
POST body so the server-side parser fires with the same shape it gets
from the CLI.

ToolSurfaceVersion already 0.3 (TASK-1380). This adds a tool but
existing actions are unchanged, so no further bump is needed.

Tests: catalog_readonly_test.go's bijection and dispatch tests
extended with pad_playbook entries (both `expected` maps + the
liveCmdhelpDoc stub). The actions-match-cmdhelp + dispatch-cmdpath
checks pass.

Parent: PLAN-1377.

* fix(mcp): align pad_playbook MCP shape with CLI cmdhelp (TASK-1381)

Codex round 1:

P1 — Renamed CLI Use strings from `show <slug|ref>` / `run <slug|ref> ...`
to plain `show <ref>` / `run <ref> [args...]`. The pipe-alternation
form makes cmdhelp synthesize the arg name as "value"; local stdio MCP
calls were failing with missing "value" because the tool param is
"ref". The Long descriptions still explain the resolver accepts
invocation_slug / item slug / issue ref.

P1 — pad_playbook.action=run is now a custom action handler that
flattens the structured `args` map + `raw_args` slice into the CLI's
positional/flag/kv token sequence before dispatching. Without this the
passThrough path dropped args/raw_args (they aren't cmdhelp args/flags),
making the local-stdio invocation a no-op from the agent's POV. Sort
order is deterministic for test replay stability.

P2 — raw_args type changed from "array" to "array<string>" so the
catalog builder's paramDefToToolOption recognizes it as a string array
instead of falling through to the WithString default. Without this MCP
advertised a string but the mapper expected a slice.

Test cmdhelp stub updated: playbook run now declares "ref" + variadic
"args" positionals, matching the new Use string.

Parent: PLAN-1377.

* fix(mcp): mapPlaybookRun accepts both flattened + map args (TASK-1381)

Codex round 2 HIGH: actionPlaybookRun's flattened input shape
({args: []string}) confused mapPlaybookRun, which expected args as a
map. Cloud/HTTP MCP calls were posting {"args":["PLAN-7"]} and the
server tried to decode that as map[string]any.

mapPlaybookRun now coerces all the shapes both dispatch paths produce:
- args as map → forwarded verbatim as the pre-parsed dictionary.
- args as []string / []any → treated as raw_args (CLI tokens).
- raw_args (any case form) → appended to the raw_args list.

This keeps env.Dispatch the single dispatch entry point on the action
side while letting the HTTP mapper translate freely.

Parent: PLAN-1377.

* fix(cli): use [args]... ellipsis-outside-brackets so cmdhelp parses arg name (TASK-1381)

Codex round 3 HIGH: cmdhelp's argRE bakes the ellipsis into the arg
NAME when it appears inside the brackets — `[args...]` parses as
Arg{Name: "args...", Repeatable: false}, not Arg{Name: "args",
Repeatable: true}. That made BuildCLIArgs (used by ExecDispatcher
for local stdio MCP) drop the playbook argument tokens because the
input map key "args" didn't match the cmdhelp positional name
"args...".

The fix is to move the ellipsis OUTSIDE the brackets per the cmdhelp
spec: `[args]...`. Code comment cross-references cmdhelp/json.go::argRE
so future Use-string editors don't regress.

Parent: PLAN-1377.

* fix(mcp): preserve structured args on HTTP dispatch path (TASK-1381)

Codex round 4 MEDIUM: actionPlaybookRun's flatten step dropped explicit
`false` values on flag-typed args, so an MCP call like
{args: {stop-after-each: false}} couldn't override a flag default of
true.

Fix: dispatcher-type-aware branching.

- HTTPHandlerDispatcher path: forward input as-is. mapPlaybookRun
  preserves the structured args map (including explicit false values),
  and the server's bindPlaybookArgs sees the override correctly.
- ExecDispatcher path: flatten args + raw_args into CLI tokens as
  before. The CLI's strict parser only supports bareword flag
  PRESENCE, so the flag=false override is a documented local-stdio
  limitation; route through HTTP/in-process MCP for that rare case.

Function docstring now spells out the two paths and the CLI
limitation so the next reader doesn't have to reverse-engineer it.

Parent: PLAN-1377.

* fix(mcp): bypass BuildCLIArgs on HTTP playbook-run dispatch (TASK-1381)

Codex round 5: even with the dispatcher-type branch from round 4,
env.Dispatch still ran BuildCLIArgs FIRST and only forwarded to the
chosen dispatcher AFTER. BuildCLIArgs choked on args:map (the cmdhelp
positional 'args' wants strings) and returned a validation_failed
result before mapPlaybookRun ever saw the structured input.

Fix: when dispatching to HTTPHandlerDispatcher, attach the input map
to context manually via WithDispatchInput and call the dispatcher
directly, skipping BuildCLIArgs. ExecDispatcher path unchanged — it
still uses env.Dispatch with the flattened CLI tokens because the
local CLI needs them.

Parent: PLAN-1377.

* fix(mcp): use structured validation envelope for missing ref (TASK-1381)

Codex round 6 P3: actionPlaybookRun's missing-ref error returned a
plain text result, breaking the structured-envelope contract that
every other validation error in the catalog follows. Switch to
NewErrorResult/ErrorPayload so agents can branch on error.code.

Codex's round-6 P2 (read-scope tokens blocked from POST /playbooks/{ref}/run
because the middleware requires GET/HEAD/OPTIONS) is real but
out-of-scope for this PR — it touches the auth scope model and
deserves a dedicated HT item rather than a snap fix here. Filed as
follow-up. The action is still functional for any token with 'write'
scope, which is the default for local-stdio MCP and pad-cloud
deployments.

Parent: PLAN-1377.
2026-05-12 19:11:10 -04:00
..