Repository navigation
Example: pre-pay check of an x402 endpoint in a Mastra agent - #8
Open
IlhamEl777 wants to merge 1 commit into
Open
IlhamEl777 wants to merge 1 commit into
IlhamEl777 wants to merge 1 commit into
Conversation
|
👀 We've notified the reward creators here. |
This branch has not been deployed
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.
/claim #3
What this adds
examples/mastra-prepay-check/— a runnable Mastra example that checks an x402 paid endpoint before anything pays for it, as asked in #3:src/index.ts— a Mastra tool built withcreateToolthat callsrunCheck402({ url })from@agent402-dev/ai-sdk-toolsand returns its result (fixes,findingsincl. the price,usdcAmounts,signedOrPaid). One request, no payment header, nothing signed or paid, no key read. One agent uses the tool; its instructions say explicitly: report the price, and do not pay whenfixes > 0.broken402.ts— a tiny local server answering a broken 402 (noPAYMENT-REQUIREDheader) for the negative case.runCheck402refuses localhost by default, so the demo opts in explicitly withALLOW_LOCAL=1.package.json— all versions pinned.README.md— install, run, the result fields, and the line that nothing is paid.No wallet, key or RPC is needed (the only key is the LLM key for Mastra itself,
OPENAI_API_KEY; the model defaults togpt-4o-mini, overridable viaMASTRA_EXAMPLE_MODEL). No payment code, payment rules/limits, keys, wallets or signing are touched.How to run
What I ran (versions: Node v24.22.1, @mastra/core 1.87.0, @ai-sdk/openai 4.0.130, @agent402-dev/ai-sdk-tools 0.1.0, TypeScript 5.9.3, tsx 4.23.15 —
tsc --noEmitpasses)Honest note on method: my sandbox has no LLM API key, so I could not do a full agent run. The example handles that case openly: with no
OPENAI_API_KEYset,npm startruns the tool itself — the exact component the agent calls — prints its raw JSON result, and states the verdict the agent's instructions produce from it. I verified the agent/tool wiring builds and typechecks, and ran the tool against both required cases:(a) Live x402 endpoint — the toolkit's own hosted paid check402 route (0.01 USDC), through the tool:
{ "url": "https://agent402-check402.agent402-ebc.workers.dev/x402/check402?url=https://example.com", "method": "GET", "fixes": 0, "findings": [ { "level": "note", "code": "tx_version_1", "message": "accepts[0]: extra.transactionVersions includes 1, so buyers may settle this option as a version-1 Solana transaction. Code that reads your settlements with getTransaction must pass maxSupportedTransactionVersion: 1 (with 0 the RPC answers error -32015)." }, { "level": "ok", "code": "header", "message": "PAYMENT-REQUIRED header decodes." }, { "level": "ok", "code": "price", "message": "accepts[0]: Solana mainnet, 0.01 USDC (amount \"10000\")." }, { "level": "ok", "code": "price", "message": "accepts[1]: Base, 0.01 USDC (amount \"10000\")." }, { "level": "ok", "code": "discovery", "message": "Discovery metadata present (input GET)." } ], "usdcAmounts": ["10000", "10000"], "signedOrPaid": false }Price shown,
fixes: 0→ the agent's instructions say this is the payable case.(b) Broken local 402 —
broken402.tsonhttp://127.0.0.1:8402/paid, withALLOW_LOCAL=1, through the tool:{ "url": "http://127.0.0.1:8402/paid", "method": "GET", "fixes": 1, "findings": [ { "level": "fix", "code": "header_missing", "message": "HTTP 402 without a PAYMENT-REQUIRED header." } ], "usdcAmounts": [], "signedOrPaid": false }fixes: 1→ the agent's instructions make it state it will not pay, listing this finding.(c) Localhost refused by default — the same local URL without
ALLOW_LOCAL=1is refused before any request (fixes: null,error: "Only https URLs are checked."), verified in the same test run.AI tools were used to write this, as the issue allows; the checks above were run by me on the versions listed.