Use 402.report with Mastra
Give a Mastra agent its own x402 spend and forensics as MCP tools, and route its HTTP through 402.report — with one API key.
Mastra agents connect to remote MCP servers through @mastra/mcp's
MCPClient. 402.report ships a helper that returns the exact server entry, so your agent can
query its own x402 spend and debug failed payments in-loop.
1. Install
npm install @402report/sdk @mastra/mcp
Create an API key in the 402.report dashboard and set it as
REPORT402_API_KEY.
2. Add the MCP tools to your agent
import { MCPClient } from "@mastra/mcp";
import { mastraMcpServer } from "@402report/sdk";
import { Agent } from "@mastra/core/agent";
import { openai } from "@ai-sdk/openai";
const mcp = new MCPClient({
servers: {
report402: mastraMcpServer({ apiKey: process.env.REPORT402_API_KEY! }),
},
});
export const agent = new Agent({
name: "spend-aware-agent",
instructions:
"You can inspect your own x402 payment activity with the 402.report tools — check spend " +
"and failed payments before and after paying for resources.",
model: openai("gpt-4o"),
tools: await mcp.getTools(),
});
Your agent now has tools like get_spend_summary, get_spend_by_domain, and
list_failed_payments — all read-only and scoped to your API key.
3. (Optional) Route the agent's HTTP through 402.report
To observe the x402 payments your agent makes (not just query them), route its outbound HTTP
through the proxy with a drop-in fetch:
import { createProxyFetch } from "@402report/sdk";
const fetch402 = createProxyFetch({ apiKey: process.env.REPORT402_API_KEY! });
// Pass `fetch402` to whatever your tools use to call paid APIs.
That's it — no SDK lock-in, no change to how your agent pays. 402.report is non-custodial: it observes, it never holds or settles funds.