Fix x402 retries for tools without proactive metadata - #365
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
After a payment-required
CallToolResult, the bridge signs a fresh payment and stores it inpaymentCachebefore retrying the tool.getOrSignPaymentcurrently checks the tool cache and_meta.x402before 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-SIGNATUREand_meta["x402/payment"]without another signing call.Verification
pnpm run buildpnpm run test:unit(1007 tests passed)