Skip to content

Example: pre-pay check of an x402 endpoint in a Mastra agent - #8

Open
IlhamEl777 wants to merge 1 commit into
agent402-dev:mainfrom
IlhamEl777:example/mastra-prepay-check
Open

IlhamEl777 wants to merge 1 commit into
agent402-dev:mainfrom
IlhamEl777:example/mastra-prepay-check

Conversation

@IlhamEl777

Copy link
Copy Markdown

/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 with createTool that calls runCheck402({ url }) from @agent402-dev/ai-sdk-tools and returns its result (fixes, findings incl. 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 when fixes > 0.
  • broken402.ts — a tiny local server answering a broken 402 (no PAYMENT-REQUIRED header) for the negative case. runCheck402 refuses localhost by default, so the demo opts in explicitly with ALLOW_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 to gpt-4o-mini, overridable via MASTRA_EXAMPLE_MODEL). No payment code, payment rules/limits, keys, wallets or signing are touched.

How to run

cd examples/mastra-prepay-check
npm install
export OPENAI_API_KEY=<redacted>
npm start -- "https://agent402-check402.agent402-ebc.workers.dev/x402/check402?url=https://example.com"
npm run broken402 &                  # negative demo
ALLOW_LOCAL=1 npm start -- http://127.0.0.1:8402/paid

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 --noEmit passes)

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_KEY set, npm start runs 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.ts on http://127.0.0.1:8402/paid, with ALLOW_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=1 is 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.

@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