Skip to content

examples: Mastra agent checks an x402 endpoint before paying - #13

Open
riccardocens-code wants to merge 2 commits into
agent402-dev:mainfrom
riccardocens-code:examples/mastra-prepay-check-3
Open

riccardocens-code wants to merge 2 commits into
agent402-dev:mainfrom
riccardocens-code:examples/mastra-prepay-check-3

Conversation

@riccardocens-code

Copy link
Copy Markdown

/claim #3

Adds examples/mastra-prepay-check/, a runnable Mastra example that checks an x402 endpoint before anything pays it.

What it does

  • src/tool.ts: a createTool tool (check_x402_endpoint) that calls runCheck402({ url }) from @agent402-dev/ai-sdk-tools and returns its result (fixes, findings, usdcAmounts, signedOrPaid). One request without a payment header; nothing signed or paid, no key read.
  • src/agent.ts: the agent. Its instructions say: do not pay when fixes > 0 (and not when fixes is null, which runCheck402 returns when the URL was refused or the request failed).
  • The example passes allowLocal: true so the check can reach http://127.0.0.1 for the local server; other URLs must still be public https. The README says how to drop it.
  • src/broken402Server.ts: a small local server whose 402 has a PAYMENT-REQUIRED header that is not base64 JSON.
  • src/tool.test.ts: npm test, no LLM key: a public http URL is refused with no request, and a local broken 402 gives fixes > 0.
  • package.json pins @mastra/core 1.87.0, @agent402-dev/ai-sdk-tools 0.1.0, zod 4.6.7, tsx 4.23.15. Node 22.13+ (Mastra's requirement).

Run by me (@mastra/core 1.87.0, model google/gemini-3.5-flash-lite via Mastra's model router with GOOGLE_GENERATIVE_AI_API_KEY, ai-sdk-tools 0.1.0 / pay-server 0.15.0, Node 24.22.1, npm 11.21.1). npm install && npm start -- <url>:

Live endpoint, npm start -- "https://agent402-check402.agent402-ebc.workers.dev/x402/check402?url=https://example.com":

price: Solana mainnet or Base, 0.01 USDC (10000 base units).
fixes: 0
decision: will pay because there are no fixes required (`fixes: 0`) and all checks passed successfully.

Local broken 402, npm run broken-402 & then npm start -- http://127.0.0.1:4021/paid:

price: none (no USDC amounts returned)
fixes: 1 (header_undecodable: PAYMENT-REQUIRED is not base64 JSON that @x402/core can decode)
decision: will not pay because fixes > 0 (the endpoint has decoding issues with its payment requirement header).

npm start -- http://example.com/paid (not https): refused without a request:

price: none
fixes: null (check failed: Only https URLs are checked)
decision: will not pay because the check failed and fixes is null/refused.

npm test: 2 tests pass.

No payment code, rules, keys, wallets or signing are touched; the change is only the new example folder.

@opirebot

opirebot Bot commented Dec 5, 2026

Copy link
Copy Markdown

👀 We've notified the reward creators here.

This branch has not been deployed

No deployments
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