Propagate upstream HTTP 402 from the openApiMcpServer request callback as an MCP-level payment-required response
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:
- Host auto-pay (
@x402/fetchinside 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. - 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:
- Upstream
PAYMENT-REQUIREDrequirements propagated up as an MCP-level 402 — a typed error/result from therequestcallback. - 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. - 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
requestcallback throws a typedPaymentRequiredError(carrying the parsed upstream requirements),openApiMcpServer()aborts the script at that point and returns an MCP-level 402 in thewithX402shape, 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, aswithX402Clientalready does). The host executes only that single upstream request withPAYMENT-SIGNATUREattached 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
executeif 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
executeis out of scope, a docs recipe showing@x402/fetchinside therequestcallback (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 alongsidepaidToolfor the paid writes on the same server, so publishers don't have to discover this composition themselves. - Coarse-grained pricing — wrapping
executeitself withwithX402(one flat price per script run). Possible today but a poor fit for per-operation upstream pricing.
Additional Context
- x402 in the Agents SDK: overview, Charge for MCP tools
- Code Mode search/execute guide (host-side auth pattern): Build a search and execute MCP server
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.
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?
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.