agents icon indicating copy to clipboard operation
agents copied to clipboard

Propagate upstream HTTP 402 from the openApiMcpServer request callback as an MCP-level payment-required response

Open giter010101 opened this issue 4 months ago • 3 comments

Problem

openApiMcpServer() keeps authentication host-side by design: the request callback attaches credentials, and the sandbox never sees them. This works well for bearer auth, but there is no equivalent story when the upstream OpenAPI service gates some operations with x402 (HTTP 402 + PAYMENT-REQUIRED).

Today the host callback has two options, and both fall short of the caller-pays model:

  1. Host auto-pay (@x402/fetch inside the callback) — works today, but the Worker's wallet pays. That fits an operator-resells-API model, not the case where the MCP caller should pay for the operations it triggers.
  2. Surface the 402 to the sandbox as a structured return value — the model can see the payment requirements, but there is nothing it can do with them: the sandbox has no wallet (correctly so), and there is no mechanism to hand the challenge to the MCP client.

Meanwhile, the Agents SDK already has a clean caller-pays primitive: paidTool / withX402 returns an MCP-level 402, and withX402Client signs against the advertised requirements and replays the tool call with the proof in MCP metadata. But execute is a single generic tool with per-operation upstream pricing, so paidTool's static per-tool price doesn't fit — and today an upstream HTTP 402 inside execute never becomes an MCP-level 402 that an x402-capable client could settle.

To be precise about the gap, caller-pays through execute needs three pieces, and only the first is a simple mapping:

  1. Upstream PAYMENT-REQUIRED requirements propagated up as an MCP-level 402 — a typed error/result from the request callback.
  2. On the client's paid retry, the proof must be attached to the one upstream request that triggered the 402 — the host needs a correlation between the MCP-level payment and a specific request() invocation.
  3. Upstream calls that already succeeded inside the same script must not be re-executed.

Without (2) and (3), propagation alone produces either a full script replay (side effects re-applied) or a client that paid while the host doesn't know where to attach the signature.

Proposed Solution

A design that keeps the runtime untouched — no script suspension, no replay:

  • When the host request callback throws a typed PaymentRequiredError (carrying the parsed upstream requirements), openApiMcpServer() aborts the script at that point and returns an MCP-level 402 in the withX402 shape, extended with a payment intent: an ID bound host-side to the exact pending upstream request (method, path, query, body).
  • After the client pays, it retries referencing the intent ID (proof in MCP _meta, as withX402Client already does). The host executes only that single upstream request with PAYMENT-SIGNATURE attached and returns its result as the tool result.
  • The model, having received the partial script output plus the settled operation's result, continues with a follow-up execute if needed.

This is deliberately narrower than "resume the script where it paused": execute's boundary is the whole script and the request callback is a sandbox-to-host RPC, so suspending mid-script would be a significant runtime change. Scoping settlement to a single pending request keeps this at the level of an error mapper plus an intent store, at the cost of the model explicitly continuing after a paid operation — which seems acceptable for payment-gated writes.

Trade-off worth flagging: script work after the aborted call is lost, and scripts mixing several paid calls settle one at a time. If the team prefers a suspension-based design instead, that is a bigger ask and I'd defer to you on feasibility.

Alternatives Considered

  • Host auto-pay, documented as the official pattern — if caller-pays through execute is out of scope, a docs recipe showing @x402/fetch inside the request callback (mirroring the bearer-auth section of the search/execute guide) would at least make the operator-pays model a supported, copy-pasteable path.
  • Hybrid composition recipe — document running openApiMcpServer() for free reads alongside paidTool for the paid writes on the same server, so publishers don't have to discover this composition themselves.
  • Coarse-grained pricing — wrapping execute itself with withX402 (one flat price per script run). Possible today but a poor fit for per-operation upstream pricing.

Additional Context

giter010101 avatar Jul 23 '26 00:07 giter010101

MACH Base is live x402 infrastructure for Base-chain reads with no signup or API key. We are trying to verify the cleanest caller-pays integration path for your agent/runtime. A live compatibility call is GET https://api.mach.gallery/api/base/block-number ($0.005 USDC on Base); discovery: https://api.mach.gallery/.well-known/x402 ; OpenAPI: https://api.mach.gallery/openapi.json . If this fits your stack, one real paid call is enough to validate it. If not, a short note on the blocking integration/policy is useful. MACH conversation=8e3b94570f42480b2c35110d.

TheAliphant avatar Sep 26 '26 21:09 TheAliphant

Your issue describes the gap between an upstream HTTP 402 and a caller-pays retry: preserving one pending request without replaying prior operations. We prepared a dependency-free no-funds adapter that creates a host-side intent bound to the request and selected PaymentRequirements, rejects mismatches, requires proof for the same intent, and only then marks one request authorized/completed. It does not create payment payloads, sign, settle, or store wallet keys. Five focused tests pass (37/37 for the non-live Varyntiq suite). Would you be open to reviewing this against the openApiMcpServer callback boundary, or pointing us to the preferred error/metadata shape?

mercadosclaro-eng avatar Sep 27 '26 10:09 mercadosclaro-eng

MACH Base is live x402 infrastructure for Base-chain reads with no signup or API key. We are trying to verify the cleanest caller-pays integration path for your agent/runtime. A live compatibility call is GET https://api.mach.gallery/api/base/block-number ($0.005 USDC on Base); discovery: https://api.mach.gallery/.well-known/x402 ; OpenAPI: https://api.mach.gallery/openapi.json . If this fits your stack, one real paid call is enough to validate it. If not, a short note on the blocking integration/policy is useful. MACH conversation=8e3b94570f42480b2c35110d:step:1.

TheAliphant avatar Sep 27 '26 11:09 TheAliphant