Potential settlement gating issue: paid tool callback runs before settlement
Hi, I noticed a possible settlement-ordering issue in the paid MCP tool wrapper.
In packages/agents/src/mcp/x402.ts, the wrapper builds payment requirements for a tool:
146: // Build v2 payment requirements for this tool call
147: const resourceConfig: ResourceConfig = {
148: scheme: "exact",
149: payTo: cfg.recipient,
150: price: priceUSD,
151: network,
152: maxTimeoutSeconds: 300
153: };
168: const resourceInfo = {
169: url: `x402://${name}`,
170: description,
171: mimeType: "application/json"
172: };
It decodes and verifies the submitted payment:
202: // Decode the payment payload (base64-encoded JSON)
203: let paymentPayload: PaymentPayload;
205: paymentPayload = JSON.parse(atob(token));
210: // Find matching requirements for this payment
211: const matchingReq = resourceServer.findMatchingRequirements(
212: requirements,
213: paymentPayload
214: );
219: // Verify payment with facilitator
221: const vr = await resourceServer.verifyPayment(
222: paymentPayload,
223: matchingReq
224: );
225: if (!vr.isValid) {
226: return paymentRequired(vr.invalidReason ?? "INVALID_PAYMENT", {
After verification, the tool callback executes before settlement:
234: // Execute the tool callback
235: let result: CallToolResult;
236: let failed = false;
237: try {
238: result = await cb(args, extra);
...
257: // Settle payment only on success
258: if (!failed) {
259: try {
260: const s = await resourceServer.settlePayment(
261: paymentPayload,
262: matchingReq
263: );
264: if (s.success) {
The effective order is:
payment payload -> matching requirement -> verifyPayment -> cb(args, extra) -> settlePayment
Why this may matter:
- A paid MCP tool callback may perform expensive compute, network calls, or external side effects.
- If settlement fails after the callback returns, the wrapper can return a payment-required response, but the callback work has already happened.
- The wrapper is framework-level, so it cannot assume every callback is cheap or reversible.
A safer design would settle before invoking externally effective callbacks, or explicitly restrict pre-settlement callbacks to side-effect-free work. I am reporting this as a potential issue rather than a confirmed exploit, since impact depends on what tool callbacks are registered.
Good catch, and the read is right. The crux is what verifyPayment actually guarantees: in x402 v2 it is a validity check (signature, amount, expiry, not-already-spent), not a binding hold on the funds. So a valid verify does not guarantee a later settle will succeed, which is exactly why a callback running between the two can do irreversible work that never gets paid for.
Worth noting your suggested fix -- settle before the callback -- does not remove the risk, it moves it: settle-first reintroduces the charged-without-content case (settlement succeeds, the callback then fails, and the caller paid for nothing). The two orderings just choose which side bears a failure: the provider does free work, or the caller pays for nothing.
Since the wrapper is framework-level and, as you note, can't assume callbacks are cheap or reversible, the cleanest shape is a per-tool policy rather than one global order:
settle_after(current): fine for cheap, reversible, or idempotent callbacks.settle_before+ a refund/compensation path: for expensive or irreversible callbacks, handling the charged-without-content tail explicitly.reserve: if the facilitator supports a hold / two-phase capture, verify places the hold and settle captures it, which closes the window on both sides. Most x402 facilitators don't support this yet, so it's aspirational.
Default by the tool's declared cost profile, and let tool authors opt expensive tools into settle-before.
One more from production that is orthogonal to the ordering and bites later: on probabilistic-finality networks (Base, Solana) a successful settle is not final. A reorg can reverse a settled payment after the callback already ran, so for high-value tools settle-success is not the same as paid -- you want a post-settlement finality check or a revocation path on top of whichever ordering you pick.
For reference, AlgoVoi runs verify, then execute, then settle across 8 chains in production with a reorg-aware finality monitor and a signed receipt at admission and post-settlement. It is a gated implementation; the public agent card sets out the chains and the verify/settle flow: https://api.algovoi.co.uk/.well-known/agent.json
AlgoVoi (chopmob-cloud) -- docs.algovoi.co.uk/substrate-2