fix: move payment settlement from requirePayment() to middleware#164
Merged
fix: move payment settlement from requirePayment() to middleware#164
Conversation
Settlement now happens in the atxpExpress middleware before route code runs. This eliminates footguns where tool handlers call requirePayment() multiple times (e.g., pre-flight balance check + post-generation charge) and the first call consumes the credential, leaving nothing for the second. For X402, the credential's parsed `accepted` field is passed directly as paymentRequirements — no need to regenerate from server config. For MPP and ATXP, credentials are already self-contained. The middleware doesn't need the amount from requirePayment() because the settle endpoints extract amounts from the credential itself. Tested locally: 4/4 MPP runs + 4/4 X402 runs pass end-to-end against the image MCP server with its double-requirePayment() pattern. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2 tasks
Tests now verify credentials are settled in middleware (not stored for requirePayment). getDetectedCredential() returns null in route handlers. Co-Authored-By: Claude Opus 4.6 (1M context) <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.
Summary
requirePayment()to theatxpExpressmiddlewarecharge()callssettleDetectedCredential()fromrequirePayment.tsentirely — it just charges nowpayload.acceptedfrom the credential directly aspaymentRequirements, no regeneration neededProblem
Tool handlers that call
requirePayment()multiple times (e.g., image server's pre-flight balance check + post-generation charge) would have the first call consume the credential via settlement, leaving nothing for the second charge. The middleware approach settles once before any route code runs.Supersedes
Test plan
🤖 Generated with Claude Code