|
| 1 | +# Claude Session Lifecycle And Integration |
| 2 | + |
| 3 | +## Motivation |
| 4 | + |
| 5 | +`mcp-repl` should behave predictably in Claude Code even though Claude does not |
| 6 | +currently expose all client lifecycle events or agent identities through MCP. |
| 7 | +The important workflows are session reset on `/clear`, reliable install/tool |
| 8 | +visibility, and clear documentation of shared-session behavior for subagents. |
| 9 | + |
| 10 | +## Target Scenarios |
| 11 | + |
| 12 | +### `/clear` Resets The Runtime |
| 13 | + |
| 14 | +Minimal task: |
| 15 | + |
| 16 | +1. A user starts Claude Code with `mcp-repl` installed. |
| 17 | +2. The agent creates runtime state through the `repl` tool. |
| 18 | +3. The user runs `/clear` in Claude Code. |
| 19 | +4. The next `repl` call uses a fresh worker session. |
| 20 | + |
| 21 | +MCP does not define a `/clear` notification, and Claude does not currently send |
| 22 | +one to MCP servers. A Claude-specific implementation would need to use Claude |
| 23 | +hooks: |
| 24 | + |
| 25 | +- a `SessionStart` hook injects the Claude session ID into the environment, |
| 26 | +- `mcp-repl` records the active REPL control endpoint for that session ID, |
| 27 | +- a `SessionEnd` hook looks up that endpoint and asks `mcp-repl` to restart the |
| 28 | + worker externally. |
| 29 | + |
| 30 | +Codex already closes and restarts MCP connections on `/clear`, so this is a |
| 31 | +Claude-specific lifecycle bridge rather than a general MCP requirement. |
| 32 | + |
| 33 | +### Claude Subagents Share A REPL Session |
| 34 | + |
| 35 | +Current Claude subagents share the same MCP connection as the main agent. Since |
| 36 | +`mcp-repl` owns one long-lived runtime per MCP server connection, those subagents |
| 37 | +also share the same REPL session. |
| 38 | + |
| 39 | +There is no clean server-side fix under the current Claude MCP shape. Tool calls |
| 40 | +do not include a stable agent or subagent ID. `toolUseId` might be correlated by |
| 41 | +polling Claude transcript files, but that would be brittle and should not be |
| 42 | +implemented as the happy path. |
| 43 | + |
| 44 | +If Claude later sends a stable agent identity on MCP tool calls, revisit |
| 45 | +per-agent worker routing. Until then, document the shared session in installer |
| 46 | +output, the plugin skill, or Claude-specific guidance. |
| 47 | + |
| 48 | +### Install And Protocol Drift Stay Covered |
| 49 | + |
| 50 | +Minimal task: |
| 51 | + |
| 52 | +1. A user runs `mcp-repl install --client claude`. |
| 53 | +2. A fresh Claude Code session shows the installed R and Python tools. |
| 54 | +3. A one-call smoke test succeeds for each installed interpreter. |
| 55 | +4. A raw MCP `initialize` request with normal JSON-RPC shape succeeds. |
| 56 | + |
| 57 | +The March 2026 install regression and initialize-handshake bug were both caused |
| 58 | +by client/protocol drift not being covered by the same integration surface as |
| 59 | +Codex. Future changes to install code, server initialization, and tool |
| 60 | +description registration should include Claude coverage when practical. |
| 61 | + |
| 62 | +### Claude Permission Snippets Stay Current |
| 63 | + |
| 64 | +Claude Code permission syntax can change independently of `mcp-repl`. Generated |
| 65 | +or documented Claude permission snippets should avoid known-deprecated patterns |
| 66 | +such as the old `:*` suffix and should be checked against the current Claude |
| 67 | +syntax when touched. |
| 68 | + |
| 69 | +## Current Public Reset Surface |
| 70 | + |
| 71 | +- `repl_reset` explicitly restarts the runtime. |
| 72 | +- `\u0003` interrupts the current runtime request. |
| 73 | +- `\u0004` resets the runtime and runs any remaining input in the fresh |
| 74 | + session. |
| 75 | +- `q()` or EOF exits the runtime; the next request starts a fresh worker. |
| 76 | +- Claude's `/mcp reconnect` exists as a user command, but there is no known |
| 77 | + programmatic hook for an MCP server to trigger it. |
| 78 | + |
| 79 | +## Constraints |
| 80 | + |
| 81 | +- Do not depend on MCP behavior that is not in the spec unless the feature is |
| 82 | + clearly Claude-specific and tested as such. |
| 83 | +- Do not implement transcript polling to infer subagent identity. |
| 84 | +- Do not broaden sandbox or network policy as part of lifecycle handling. |
| 85 | +- Keep the runtime reset action server-owned. Hook scripts should only signal |
| 86 | + the already-running `mcp-repl` instance. |
| 87 | + |
| 88 | +## Acceptance Shape |
| 89 | + |
| 90 | +- Add install tests or smoke coverage showing Claude config generation still |
| 91 | + exposes the expected tools. |
| 92 | +- Add a protocol test for the raw `initialize` request shape that previously |
| 93 | + failed. |
| 94 | +- If `/clear` support is implemented, add hook fixture tests and a manual smoke |
| 95 | + scenario for Claude Code. |
| 96 | +- Add skill or installer text that states Claude subagents share one REPL |
| 97 | + session under the current client behavior. |
| 98 | + |
| 99 | +## Non-Goals |
| 100 | + |
| 101 | +- Per-subagent REPL sessions for Claude before Claude exposes stable agent IDs. |
| 102 | +- A generic MCP `/clear` protocol extension. |
| 103 | +- Programmatically driving Claude's `/mcp reconnect` command from `mcp-repl`. |
0 commit comments