|
| 1 | +# The Claude Code adapter |
| 2 | + |
| 3 | +Claude Code is the second harness. It proves the central claim of this PoC: that swapping |
| 4 | +the agent is one config value. Where the [Pi adapter](pi.md) does much of its work inside Pi |
| 5 | +through an extension, Claude does its work through standard ACP. That makes Claude the |
| 6 | +template for any MCP-capable harness rivet can drive. |
| 7 | + |
| 8 | +Read the [architecture](../architecture.md) and [ports and adapters](../ports-and-adapters.md) |
| 9 | +pages first. |
| 10 | + |
| 11 | +## Running Claude |
| 12 | + |
| 13 | +The daemon resolves the harness id `claude` to the `claude-agent-acp` adapter, which starts |
| 14 | +the `claude` CLI. One operational detail is worth calling out, because it caused a real bug. |
| 15 | +The daemon does not ship the `claude` CLI. It downloads it over HTTPS the first time a run |
| 16 | +asks for Claude. The sidecar image is a slim Node image with no root certificates, so that |
| 17 | +HTTPS download failed until we added `ca-certificates` to the image. With the certs in |
| 18 | +place, the download verifies and Claude runs. |
| 19 | + |
| 20 | +Auth is config, like everything else. Claude authenticates with `ANTHROPIC_API_KEY` from the |
| 21 | +project vault when present, or with an OAuth token (`CLAUDE_CODE_OAUTH_TOKEN`) otherwise. The |
| 22 | +runner turns the common failures into one clear line, so a user sees "add the project's |
| 23 | +Anthropic key" rather than a stack trace. |
| 24 | + |
| 25 | +## Tools over MCP |
| 26 | + |
| 27 | +Claude advertises the `mcpTools` capability, so the runner delivers tools to Claude the |
| 28 | +standard ACP way, over MCP. This is the branch that the [capability probe](../ports-and-adapters.md) |
| 29 | +chooses: deliver over MCP when the harness reports `mcpTools`, not when the harness name is |
| 30 | +something in particular. |
| 31 | + |
| 32 | +The mechanism is a small stdio MCP server (`tools/mcp-server.ts`) that the daemon launches |
| 33 | +and attaches to the session. Its tool bodies POST back to Agenta's `/tools/call` with the |
| 34 | +same callback-tool envelope the Pi path uses. The resolved specs and the callback endpoint reach the |
| 35 | +MCP server through its environment, so nothing tool-specific is written to a file the agent |
| 36 | +can read. The safety property is identical to Pi's: the provider key and the connection auth |
| 37 | +stay server-side, and the agent only ever asks Agenta to run a named tool. |
| 38 | + |
| 39 | +## Permissions |
| 40 | + |
| 41 | +Claude gates tool use behind a permission prompt. In an Agenta run there is no human at the |
| 42 | +keyboard to answer it, so the runner answers for it. By default it auto-approves, because the |
| 43 | +tools are backend-resolved and trusted. The per-run permission policy (or an env override) |
| 44 | +can flip this to deny, which rejects tool use instead. This is handled on |
| 45 | +`session.onPermissionRequest`, a hook Pi does not need because Pi does not gate tools this |
| 46 | +way. |
| 47 | + |
| 48 | +## Tracing from the event stream |
| 49 | + |
| 50 | +Claude does not self-instrument the way Pi does, because we do not load an Agenta extension |
| 51 | +into Claude. So the runner builds the trace itself, from the ACP event stream. It subscribes |
| 52 | +to the session's `session/update` notifications and turns them into the same span tree Pi |
| 53 | +produces: |
| 54 | + |
| 55 | +``` |
| 56 | +invoke_agent (AGENT) |
| 57 | + turn 0 (CHAIN) |
| 58 | + chat <model> (LLM) |
| 59 | + execute_tool <name> (TOOL) one per ACP tool_call |
| 60 | +``` |
| 61 | + |
| 62 | +This is the general path. Any harness rivet drives that does not bring its own |
| 63 | +instrumentation gets traced this way. Pi is the exception that traces itself; Claude is the |
| 64 | +rule. |
| 65 | + |
| 66 | +## Usage and output |
| 67 | + |
| 68 | +Claude reports usage in two places, so the runner reads both. The per-call input and output |
| 69 | +token split rides on the ACP `PromptResponse`, and the cost rides on the `usage_update` |
| 70 | +event. The runner combines them into the run total, which then rolls onto the workflow span |
| 71 | +the same way Pi's writeback total does. |
| 72 | + |
| 73 | +Output needs one small piece of care. Claude streams text deltas and also periodically |
| 74 | +streams a full cumulative snapshot of the message so far. If the runner naively appended |
| 75 | +everything, the answer would double. The runner detects a snapshot (a chunk that is a |
| 76 | +superset of what it already has) and replaces rather than appends, so the final text is |
| 77 | +correct whether a chunk is a delta or a snapshot. |
| 78 | + |
| 79 | +## Models |
| 80 | + |
| 81 | +Claude ignores a model id meant for another provider. Ask it for `gpt-5.5` and it keeps its |
| 82 | +own default. The runner handles this honestly: when the harness does not accept the requested |
| 83 | +model, the chat span is labelled `chat` rather than falsely claiming a model the run did not |
| 84 | +use. |
| 85 | + |
| 86 | +## What Claude demonstrates |
| 87 | + |
| 88 | +Claude is the proof that the seam works. Adding it took a `ClaudeHarness` (which holds its |
| 89 | +Pi-versus-Claude config mapping) and no change to the workflow handler above the ports; the |
| 90 | +same `RivetBackend` drives it. It also exercises the capability-driven branches the design is |
| 91 | +built on: tools over MCP because it reports `mcpTools`, a permission answer because it gates |
| 92 | +tools, and event-stream tracing because it does not self-instrument. A future harness that |
| 93 | +rivet can drive would reuse this exact path. A future harness that rivet cannot drive would |
| 94 | +instead get its own backend beside `RivetBackend` and `InProcessPiBackend`, behind the same |
| 95 | +`/run` contract. |
0 commit comments