Skip to content

fix: clear detected credential after settlement to prevent double-settle#163

Closed
badjer wants to merge 1 commit intomainfrom
fix/clear-credential-after-settlement
Closed

fix: clear detected credential after settlement to prevent double-settle#163
badjer wants to merge 1 commit intomainfrom
fix/clear-credential-after-settlement

Conversation

@badjer
Copy link
Copy Markdown
Contributor

@badjer badjer commented Apr 15, 2026

Summary

  • After settleDetectedCredential() succeeds, clears the credential from AsyncLocalStorage so a second requirePayment() call in the same request doesn't try to re-settle the same already-consumed credential
  • Fixes settlement failures when tool handlers call requirePayment() multiple times (e.g., image server's pre-flight balance check + post-generation charge)
  • Errors seen: "Transaction hash has already been used" (MPP/Tempo), "AlreadyProcessed" (Solana X402)

Root cause

The image MCP server (circuitandchisel/image/src/tools.ts) calls requirePayment() twice per tool invocation:

  1. Line 203: pre-flight balance check before expensive API call
  2. Line 269: actual charge after image generation

On a retry request (with payment credential in headers), both calls read the same credential from ALS. The first settles it successfully, but the second tries to settle the same consumed credential — the on-chain transaction was already processed.

Fix

Add clearDetectedCredential() and call it immediately after successful settlement in requirePayment(). The second call sees no credential and proceeds directly to charge().

Test plan

  • 184 server tests pass
  • End-to-end test with image MCP server after SDK publish + deploy

🤖 Generated with Claude Code

When a tool handler calls requirePayment() multiple times in the same
request (e.g., image server: pre-flight balance check + post-generation
charge), the second call would try to re-settle the same credential
that was already consumed by the first call. This caused settlement
failures: "Transaction hash has already been used" (MPP) or
"AlreadyProcessed" (Solana X402).

Fix: clear the credential from ALS after successful settlement so
subsequent requirePayment() calls in the same request see no credential
and proceed directly to charge.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@badjer
Copy link
Copy Markdown
Contributor Author

badjer commented Apr 15, 2026

Superseded by #164 — settlement moved to middleware instead of clearing credentials after settlement.

@badjer badjer closed this Apr 15, 2026
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.

1 participant