Introducing a new way to review your PRs.

Read the announcement

solana-foundation / pay

CLI for Agentic payments (x402, MPP, AP2).

View on GitHub

30 documents · Updated Nov 11, 2026

docs / acp-payment-middleware

ACP payment middleware

ACP payment middleware connects ACP-compatible agent harnesses to Pay's paid inference route without taking ownership of the ACP conversation. The pay-acp crate supports both transparent stream observation and direct agent driving, while the CLI starts the payer route that handles payment challenges and launches the ACP harness. See Agent protocol integrations for the wider MCP and ACP model, MCP payment tools and managed MCP service providers for adjacent agent integrations, and Payment CLI for the full command surface.

ACP boundary

ACP means Agent Client Protocol. An ACP client and harness exchange newline-delimited JSON-RPC messages through standard input and output. Pay supports two boundaries around this channel: middleware can observe and relay an existing conversation, or an ACP client can own the channel and drive an agent through prompt turns.

Protocol transparency

The reusable middleware in rust/…/lib.rs observes complete ACP frames but does not replace their bytes. It ignores malformed, unknown, and vendor-specific messages. This separation lets the middleware learn enough about a turn to support Buzz without becoming an ACP implementation or changing the harness contract.

References: rust/…/lib.rs• L1-13 rust/…/lib.rs• L113-131

Turn tracking

The tracker links a client session/prompt request to its session. It then gathers text from session/update messages and waits for the matching response. A completed response can produce a delivery only when the prompt identifies a Buzz destination, the turn has assistant text, and the response has no error.

References: rust/…/lib.rs• L32-38 rust/…/lib.rs• L148-205 rust/…/lib.rs• L208-231

ACP client

The ACP client in rust/…/client.rs gives a caller direct control of an ACP agent process or stdio connection. It keeps the protocol exchange asynchronous while presenting each prompt as a stream of text, thought, tool-call, completion, or failure events.

Agent driving

The client initializes the agent, creates a session rooted at a working directory, and sends text or structured prompt blocks. It can cancel an active session and can send other ACP requests with an optional timeout. The caller chooses whether permission requests use the broadest allow option or a reject option; unsupported agent requests are refused because Pay declares no file-system or terminal capabilities.

References: rust/…/client.rs• L45-63 rust/…/client.rs• L178-182 rust/…/client.rs• L226-309 rust/…/client.rs• L311-359 rust/…/client.rs• L476-512

Turn events

Each prompt returns a turn that ends with a stop reason or an error. A caller can consume events as they arrive or collect only the assistant text; a disconnected agent fails the active turn and prevents later requests from succeeding.

References: rust/…/client.rs• L79-100 rust/…/client.rs• L374-429 rust/…/client.rs• L557-588

Payment-aware CLI path

The pay acp command is the operational entry point for paid ACP use. It chooses a provider and model that fit the selected harness, starts a local payer route, and launches the external ACP adapter with the route in the adapter's expected format. The ACP crate does not sign payments; the CLI payer route performs that work when the inference service returns a payment challenge.

Provider and model routing

Provider discovery uses the same compatibility rules as the other Pay agent launchers. The route can point to a hosted provider, a local payment gateway, or a direct local provider. The payer route keeps the selected account and network context, handles a 402 Payment Required response, and can retain a delegated MPP session for later completions in the same agent process.

sequenceDiagram
    participant User as User or ACP client
    participant CLI as pay acp
    participant Proxy as Local payer route
    participant Harness as ACP harness
    participant Provider as Inference provider

    User->>CLI: Select harness, provider, and model
    CLI->>Proxy: Start route with account and network context
    CLI->>Harness: Launch with provider route
    User->>Harness: ACP prompt
    Harness->>Proxy: Inference request
    Proxy->>Provider: Forward request
    Provider-->>Proxy: Response or 402 challenge
    Proxy->>Provider: Approved paid retry
    Provider-->>Proxy: Completion stream
    Proxy-->>Harness: Provider response
    Harness-->>User: ACP response

References: rust/…/acp.rs• L82-105 rust/…/mod.rs• L137-257 rust/…/payer_proxy.rs• L1-24 rust/…/payer_proxy.rs• L203-267

Harness launch contracts

Each supported harness keeps its own process contract even though all harnesses use the same Pay route. Pay maps the chosen model and route into the configuration shape that each adapter understands.

HarnessPay-facing role
GooseStarts the native ACP subcommand with an OpenAI-compatible provider setting.
ClaudeUses its normal adapter process and receives the selected route and model through its provider settings.
CodexReceives a Pay provider entry and a temporary model catalog for its ACP process.

The launcher forwards arguments after -- to the adapter. It also carries the selected Pay account into the child process so the child and payer route use the same identity context. A missing model stops the launch before inference begins.

References: rust/…/acp.rs• L23-80 rust/…/acp.rs• L122-180 rust/…/acp.rs• L183-207

Buzz delivery

Buzz delivery is an optional managed-agent behavior layered on top of the ACP stream. When the prompt context names a valid Buzz channel, the tracker collects the assistant's final text and creates a fallback delivery if the agent did not publish that text itself.

Destination and duplicate prevention

The prompt context supplies the Buzz channel and may supply a reply event. The tracker accepts a channel only when it contains a valid UUID. It recognizes a successful buzz messages send tool call in the turn and suppresses the fallback, which prevents one assistant answer from appearing twice.

A failed publish does not suppress the fallback. An ordinary ACP prompt without Buzz context, an empty answer, and an ACP error response also produce no delivery. A relay can also ask whether a valid channel has a turn in progress before publishing on the agent's behalf.

References: rust/…/lib.rs• L134-146 rust/…/lib.rs• L184-206 rust/…/lib.rs• L208-251 rust/…/lib.rs• L258-330

Stream relay

The CLI middleware in rust/…/acp_middleware.rs runs only when both Buzz relay credentials are present. It reads client frames in one thread and forwards them to the child. It reads child frames in the main relay path, asks the tracker whether a fallback is ready, publishes that fallback when needed, and forwards the original frame to stdout.

The relay keeps stderr for the child and reports fallback problems through logs. A missing Buzz command or a failed Buzz send does not rewrite or stop the ACP response stream. Without Buzz credentials, pay acp uses direct standard-input, standard-output, and standard-error inheritance instead of the observing relay.

References: rust/…/acp_middleware.rs• L10-47 rust/…/acp_middleware.rs• L49-97 rust/…/acp_middleware.rs• L105-151

Integration boundaries

The ACP path has four separate responsibilities. The launcher selects the harness and route. The payer route handles inference payment and response streaming. The ACP middleware observes protocol frames and provides optional Buzz delivery. The ACP client provides a direct, event-based way for another caller to drive an agent. Keeping these boundaries separate means payment failure or Buzz delivery failure does not change the basic ACP transport contract.

Responsibility mapping

BoundaryResponsibilityOutput
ACP launcherChoose a compatible provider and model, then start the harnessA configured ACP child process
Local payer routeForward inference requests and complete supported payment handshakesA paid provider response
ACP trackerCorrelate prompts with responses and detect duplicate Buzz publishingAn optional BuzzDelivery decision
CLI relayPreserve ACP bytes and invoke the Buzz fallback when enabledThe unchanged ACP stream plus an optional Buzz message
ACP clientDrive an agent session and expose its turn as eventsA stream of TurnEvent values

This design also explains why pay-acp has no payment signer or HTTP client of its own. It provides protocol-neutral observation and agent-driving primitives. The CLI composes the observer with the existing payer route and the external buzz messages send command, while other callers can compose the client with their own relay or presentation layer.

References: rust/…/Cargo.toml• L1-16 rust/…/lib.rs• L1-13 rust/…/client.rs• L170-206 rust/…/acp.rs• L183-207 rust/…/acp_middleware.rs• L23-47 rust/…/payer_proxy.rs• L75-110

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep ACP frame observation aligned with the stable session/prompt, session/update, and prompt-response shapes while continuing to forward unknown frames unchanged.
  • Keep the prompt-to-session correlation and turn cleanup aligned with concurrent or repeated prompts so text cannot reach the wrong Buzz channel.
  • Keep the explicit buzz messages send detection aligned with the agent tool shape so successful sends suppress fallback delivery and failed sends do not.
  • Keep the CLI's Buzz credential gate aligned with the relay path. A managed launch must not silently lose fallback delivery while the ACP stream still appears healthy.
  • Keep provider compatibility, model selection, payer-route dialects, and each harness's configuration contract aligned when adding or changing an ACP harness.
  • Keep account and network context aligned between pay acp startup and the payer route. Sandbox and mainnet intent must not cross the local boundary.
  • Keep the ACP client event and error contract aligned with agent protocol changes so callers do not miss terminal events or treat a disconnected agent as a completed turn.
  • Keep permission policy behavior aligned with the capabilities declared during initialization. The client must not imply file-system or terminal support that it does not provide.

Important failure modes

TriggerConsequenceGuard
No Pay account is configured outside sandbox modepay acp exits before the harness starts because the payer route cannot sign a paymentRun pay setup in a terminal before starting an ACP client
No compatible provider or model is selectedProvider preparation or harness construction fails before inference startsChoose a provider and model that match the harness dialect
The provider returns a payment challenge that the account or network cannot satisfyThe payer route passes the original challenge through and the harness receives no paid completionUse the correct account, network, and available funds
ACP input or output is malformed or vendor-specificThe tracker makes no delivery decision, but the original frame remains available to the ACP peerTreat the tracker as an observer rather than a protocol rewrite layer
Buzz credentials are missingThe ACP process runs with direct stdio and no automatic fallback messageProvide non-empty BUZZ_RELAY_URL and BUZZ_PRIVATE_KEY when the launch is Buzz-managed
buzz messages send cannot start or returns an errorThe fallback message is not published, but the ACP response still reaches the clientKeep the agent's explicit Buzz send path available or fix the Buzz command and credentials
A turn contains both a successful explicit Buzz send and assistant textA fallback is not sent, so the answer is not duplicatedPreserve completed publish-call tracking in the turn state
The directly driven agent disconnects or fails a requestThe active turn ends with Disconnected or Failed, and later requests cannot proceedSurface terminal TurnEvent values and create a new client for a new agent process
A caller sends a second prompt for a session with an active turnThe client rejects the prompt instead of mixing events between turnsWait for the current turn to end before prompting the same session

Key files

FileWhy to read it
rust/…/lib.rsDefines the reusable ACP frame observer, turn state, destination parsing, and fallback decision.
rust/…/client.rsDefines the direct ACP client, session lifecycle, permission policy, turn events, and disconnect behavior.
rust/…/Cargo.tomlShows the crate's small protocol-observation boundary and its lack of payment transport dependencies.
rust/…/acp.rsDefines the pay acp command, supported harnesses, provider/model handoff, and child launch.
rust/…/acp_middleware.rsRelays ACP stdio and publishes an optional Buzz fallback without changing protocol bytes.
rust/…/mod.rsSelects compatible providers and connects the ACP command to a payer route.
rust/…/payer_proxy.rsHandles inference forwarding, payment challenge retries, session authorization, and response streaming.
rust/…/main.rsShows first-run account handling and the account requirement for pay acp.