Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions examples/mastra-prepay-check/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# mastra-prepay-check

A small, runnable [Mastra](https://mastra.ai) example that checks an x402 paid endpoint **before** an agent pays for it.

A Mastra tool (`createTool`) wraps `runCheck402({ url })` from [`@agent402-dev/ai-sdk-tools`](https://www.npmjs.com/package/@agent402-dev/ai-sdk-tools) and returns its result to the agent. The agent's instructions say: report the price, and **do not pay when `fixes > 0`**.

The check sends one request with no payment header — nothing is signed or paid, and no wallet, key or RPC is read. The only key involved is the LLM key for Mastra itself.

## Install

Node 20+:

```sh
cd examples/mastra-prepay-check
npm install
```

## Run

With an LLM key, the agent itself answers:

```sh
export OPENAI_API_KEY=<your key>
npm start -- "https://agent402-check402.agent402-ebc.workers.dev/x402/check402?url=https://example.com"
```

The URL above is this toolkit's own hosted paid check402 route (0.01 USDC) — a live x402 endpoint. The agent calls the tool, then prints its answer with the price and `fixes: 0`.

### Negative demo: a broken local 402

`broken402.ts` is a tiny local server that answers HTTP 402 **without** a `PAYMENT-REQUIRED` header:

```sh
npm run broken402 # listens on http://127.0.0.1:8402/paid
```

`runCheck402` refuses localhost unless it is explicitly allowed, so opt in for this demo only:

```sh
ALLOW_LOCAL=1 npm start -- http://127.0.0.1:8402/paid
```

The check reports `fixes: 1` (`header_missing`), and the agent says it will not pay.

### Without an LLM key

If `OPENAI_API_KEY` is not set, `npm start` runs the tool directly — the exact component the agent calls — prints its raw JSON result, and states the verdict the agent's instructions produce from it. With a key set, the same check runs through the agent and the agent's own answer is printed.

## What the tool returns

| Field | Meaning |
| --- | --- |
| `url` | The endpoint URL that was checked. |
| `method` | `GET` or `POST` — the method the paid call would use. |
| `fixes` | Number of `fix` findings; `null` when the request itself failed (see `error`). |
| `findings` | `{ level, code, message }` list, fix first, then note, then ok: payment format, network and asset, price, payTo and resource URL. |
| `usdcAmounts` | USDC amounts (base units) the 402 asks for, one per USDC payment option. |
| `error` | Set only when the URL was refused or the request failed. |
| `signedOrPaid` | Always `false` — the check never signs or pays. |

## Versions used

- `@mastra/core` 1.87.0, model `gpt-4o-mini` via `@ai-sdk/openai` 4.0.130 (default; override with `MASTRA_EXAMPLE_MODEL`)
- `@agent402-dev/ai-sdk-tools` 0.1.0
- Run and verified by the author on Node v24.22.1, TypeScript 5.9.3, tsx 4.23.15
19 changes: 19 additions & 0 deletions examples/mastra-prepay-check/broken402.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// broken402.ts — a tiny local server that answers a *broken* 402:
// HTTP 402 with no PAYMENT-REQUIRED header, so the pre-pay check reports
// fixes: 1 (header_missing) and the agent refuses to pay.
//
// npm run broken402 # listens on http://127.0.0.1:8402/paid
// ALLOW_LOCAL=1 npm start -- http://127.0.0.1:8402/paid

import { createServer } from 'node:http';

const port = Number(process.env.PORT ?? 8402);

const server = createServer((_req, res) => {
res.writeHead(402, { 'content-type': 'application/json' });
res.end(JSON.stringify({ error: 'payment required' }));
});

server.listen(port, '127.0.0.1', () => {
console.log(`Broken 402 server listening on http://127.0.0.1:${port}/paid`);
});
Loading