|
| 1 | +# MCP: plan_and_code Tool |
| 2 | + |
| 3 | +The `plan_and_code` MCP tool lets Claude Code trigger StackMemory’s multi‑agent flow silently and receive a single JSON result. It plans with Claude, implements with Codex or Claude, and critiques the result — with optional retry loops and context recording. |
| 4 | + |
| 5 | +## What it does |
| 6 | +- Planner (Claude): generates a concise plan with acceptance criteria and risks. |
| 7 | +- Implementer (Codex/Claude): applies a focused change per step. |
| 8 | +- Critic (Claude): returns `{ approved, issues[], suggestions[] }` to gate retries. |
| 9 | +- Returns a single JSON payload: `{ plan, implementation, critique, iterations[] }`. |
| 10 | + |
| 11 | +## Tool definition |
| 12 | +- name: `plan_and_code` |
| 13 | +- arguments: |
| 14 | + - `task` (string, required): short task description |
| 15 | + - `implementer` ("codex" | "claude", default: `codex`) |
| 16 | + - `maxIters` (number, default: `2`): retry loop iterations |
| 17 | + - `execute` (boolean, default: `false`): if `false`, implementer is dry‑run |
| 18 | + - `record` (boolean, default: `false`): write plan/critique as simple context rows |
| 19 | + - `recordFrame` (boolean, default: `false`): write a real frame + anchors |
| 20 | + |
| 21 | +## Environment defaults |
| 22 | +If not specified in arguments, the MCP handler reads these env vars: |
| 23 | +- `STACKMEMORY_MM_PLANNER_MODEL` (e.g., `claude-3-5-sonnet-latest`) |
| 24 | +- `STACKMEMORY_MM_REVIEWER_MODEL` (defaults to planner model if unset) |
| 25 | +- `STACKMEMORY_MM_IMPLEMENTER` (`codex` or `claude`) |
| 26 | +- `STACKMEMORY_MM_MAX_ITERS` (e.g., `3`) |
| 27 | + |
| 28 | +## Example (MCP request) |
| 29 | +```json |
| 30 | +{ |
| 31 | + "method": "tools/call", |
| 32 | + "params": { |
| 33 | + "name": "plan_and_code", |
| 34 | + "arguments": { |
| 35 | + "task": "Refactor config loader into provider pattern", |
| 36 | + "implementer": "codex", |
| 37 | + "maxIters": 2, |
| 38 | + "execute": true, |
| 39 | + "recordFrame": true |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +Response content is a single `text` item containing a JSON string: |
| 46 | +```json |
| 47 | +{ |
| 48 | + "ok": true, |
| 49 | + "result": { |
| 50 | + "plan": { "summary": "...", "steps": [ ... ], "risks": [ ... ] }, |
| 51 | + "implementation": { "success": true, "summary": "...", "commands": [ ... ] }, |
| 52 | + "critique": { "approved": true, "issues": [], "suggestions": [] }, |
| 53 | + "iterations": [ |
| 54 | + { "command": "...", "ok": true, "outputPreview": "...", "critique": { ... } } |
| 55 | + ] |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Recording behavior |
| 61 | +- `record: true` writes two entries into `.stackmemory/context.db` (simple `contexts` table): |
| 62 | + - `Plan: <summary>` (importance 0.8) |
| 63 | + - `Critique: approved|needs_changes` (importance 0.6) |
| 64 | +- `recordFrame: true` writes a real frame + anchors using the FrameManager: |
| 65 | + - Frame: `Plan & Code: <task>` |
| 66 | + - Anchors: `DECISION` (plan summary), `FACT` (commands), `RISK` (first few issues), `TODO` (first few suggestions) |
| 67 | + - Closes the frame with `{ approved: true|false }` |
| 68 | +- Both modes are best‑effort. If the DB isn’t ready, handler returns JSON without failing. |
| 69 | + |
| 70 | +## Notes |
| 71 | +- Implementer `codex` calls `codex-sm` (must be on PATH). Use `--execute` in CLI, or `execute: true` in MCP, to actually run it; otherwise it’s a dry‑run. |
| 72 | +- Audit files are saved to `.stackmemory/mm-spike/spike-<timestamp>.json` to support review/debugging. |
| 73 | +- You can compare models: |
| 74 | + - Planner/critic: override with `STACKMEMORY_MM_PLANNER_MODEL` / `STACKMEMORY_MM_REVIEWER_MODEL`. |
| 75 | + - Implementer: set to `claude` to A/B against Codex, or keep `codex` (default). |
| 76 | + |
| 77 | +## CLI equivalents (for quick checks) |
| 78 | +- Quiet JSON output: |
| 79 | + - `stackmemory mm-spike --task "Refactor config loader" --json` |
| 80 | + - `stackmemory skills spike --task "Refactor config loader" --json` |
| 81 | +- Execute implementer and record as frame: |
| 82 | + - `stackmemory skills spike --task "Refactor" --execute --max-iters 3 --json --record-frame` |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## Approval‑Gated Flow (plan_gate → approve_plan) |
| 87 | + |
| 88 | +Use this two‑phase flow when you want the plan reviewed before any code runs. |
| 89 | + |
| 90 | +### Phase 1: plan_gate |
| 91 | + |
| 92 | +Request (tools/call): |
| 93 | + |
| 94 | +```json |
| 95 | +{ |
| 96 | + "method": "tools/call", |
| 97 | + "params": { |
| 98 | + "name": "plan_gate", |
| 99 | + "arguments": { |
| 100 | + "task": "Refactor config loader into provider pattern", |
| 101 | + "plannerModel": "claude-3-5-sonnet-latest" |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +Response (content[0].text is a JSON string): |
| 108 | + |
| 109 | +```json |
| 110 | +{ |
| 111 | + "ok": true, |
| 112 | + "approvalId": "appr_1738612345678_ab12cd", |
| 113 | + "plan": { "summary": "...", "steps": [ ... ], "risks": [ ... ] } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +Render `plan` for review; store `approvalId` for Phase 2. |
| 118 | + |
| 119 | +### Phase 2: approve_plan |
| 120 | + |
| 121 | +Request (tools/call): |
| 122 | + |
| 123 | +```json |
| 124 | +{ |
| 125 | + "method": "tools/call", |
| 126 | + "params": { |
| 127 | + "name": "approve_plan", |
| 128 | + "arguments": { |
| 129 | + "approvalId": "appr_1738612345678_ab12cd", |
| 130 | + "implementer": "codex", |
| 131 | + "maxIters": 2, |
| 132 | + "execute": true, |
| 133 | + "recordFrame": true |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +Response (content[0].text is a JSON string): |
| 140 | + |
| 141 | +```json |
| 142 | +{ |
| 143 | + "ok": true, |
| 144 | + "approvalId": "appr_1738612345678_ab12cd", |
| 145 | + "result": { |
| 146 | + "plan": { ... }, |
| 147 | + "implementation": { "success": true, "commands": [ ... ] }, |
| 148 | + "critique": { "approved": true, "issues": [], "suggestions": [] }, |
| 149 | + "iterations": [ { "command": "...", "ok": true, "critique": { ... } } ] |
| 150 | + } |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +Notes: |
| 155 | +- `recordFrame: true` creates a real StackMemory frame + anchors (plan summary, commands, issues, suggestions). |
| 156 | +- `execute: true` actually invokes the implementer; otherwise it’s a dry‑run. |
| 157 | +- Approval IDs are persisted to `.stackmemory/mm-spike/pending.json` so editor restarts don’t lose pending approvals. |
| 158 | + |
| 159 | +### Optional helper tools |
| 160 | +- `plan_only`: Returns a plan JSON without running code. |
| 161 | +- `call_claude`: Calls Claude directly (prompt/model/system). |
| 162 | +- `call_codex`: Calls Codex via `codex-sm` (prompt/args/execute). |
| 163 | +- `pending_list`: Lists pending approval-gated plans with `approvalId`, `task`, and `createdAt`. Supports optional filters: |
| 164 | + - `{ taskContains: "refactor", sort: "desc", limit: 10 }` |
| 165 | + - `{ olderThanMs: 3600000 }` (older than 1 hour) |
| 166 | + - `{ newerThanMs: 600000 }` (newer than 10 minutes) |
| 167 | +- `pending_clear`: Clears pending approvals. Args: `{ approvalId }`, or `{ all: true }`, or `{ olderThanMs: <ms> }`. |
| 168 | +- `pending_show`: Returns a stored pending plan by `{ approvalId }`. |
0 commit comments