Skip to content

Fix x402 retries for tools without proactive metadata - #365

Merged
jancurn merged 1 commit into
apify:mainfrom
epistemedeus:fix/x402-challenge-retry
Aug 11, 2026
Merged

Fix x402 retries for tools without proactive metadata#365
jancurn merged 1 commit into
apify:mainfrom
epistemedeus:fix/x402-challenge-retry

Conversation

@epistemedeus

Copy link
Copy Markdown
Contributor

Problem

After a payment-required CallToolResult, the bridge signs a fresh payment and stores it in paymentCache before retrying the tool. getOrSignPayment currently checks the tool cache and _meta.x402 before it checks that signed cache entry. Challenge-first servers that omit proactive x402 tool metadata therefore retry unpaid even though a fresh signature exists.

Fix

Reuse a challenge-signed cache entry immediately after confirming that the outgoing request is a tools/call. Proactive signing still requires _meta.x402, and all non-tool requests remain unchanged.

The regression test covers a missing tool-metadata record and proves that the cached signature is attached to both PAYMENT-SIGNATURE and _meta["x402/payment"] without another signing call.

Verification

  • pnpm run build
  • pnpm run test:unit (1007 tests passed)
  • targeted x402 middleware test (10 tests passed)
  • ESLint produced no errors; Prettier check passed

@jancurn
jancurn merged commit 66c9423 into apify:main Aug 11, 2026
7 checks passed
jancurn added a commit that referenced this pull request Aug 12, 2026
…or (#368)

Follow-up to #365 (thanks @epistemedeus — the diagnosis there was right
and the fix is correct). Checking the cache before the tool-metadata
gate also means the session's payment signature is now attached to
*every* `tools/call`, including free tools, handing a live authorization
to calls that never asked for one. The cache now records which tools the
server actually charged for, and reuses the signature only for those.

- `X402PaymentCache.paymentRequiredTools` records a tool on a
payment-required tool result (bridge) or an HTTP 402 on a `tools/call`
(middleware)
- Reuse requires either advertised `_meta.x402` or a previous charge;
proactive signing still requires `_meta.x402`
- Session-level reuse across paid calls is unchanged — mcp.apify.com
treats the signature as a prepaid token (#247)
- Unit tests for the free-tool and unknown-tool cases, and for reuse
after an HTTP 402 challenge
- Adds the CHANGELOG entry #365 was missing

Refs #365, #343, #247

https://claude.ai/code/session_01YFNAoYc88jVSqaj86VdTUJ

---
_Generated by [Claude
Code](https://claude.ai/code/session_01YFNAoYc88jVSqaj86VdTUJ)_

Co-authored-by: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants