Skip to content

Mastra

Mastra agents can pay for x402-gated APIs and resources with PipRail — the first x402 payment integration for Mastra, which ships no payment rail of its own. The agent gets a budget-bound wallet that settles straight from your own wallet, verified on your own RPC: no facilitator, no fee.

Like OpenClaw and Hermes (and unlike the native elizaOS / n8n packages), Mastra wires in the published @piprail/mcp server through Mastra’s first-class MCPClient. The agent gets all 8 PipRail tools (piprail_*), capped by a spend policy it cannot exceed.

Point Mastra’s MCPClient at @piprail/mcp, then hand its tools to an agent:

src/mastra/mcp.ts
import { MCPClient } from '@mastra/mcp'
export const piprailMcp = new MCPClient({
id: 'piprail',
servers: {
piprail: {
command: 'npx',
args: ['-y', '@piprail/mcp'],
env: {
PIPRAIL_PRIVATE_KEY: process.env.PIPRAIL_PRIVATE_KEY ?? '', // omit for read-only
PIPRAIL_CHAIN: 'base',
PIPRAIL_MAX_AMOUNT: '0.10', // max per payment (token units)
PIPRAIL_MAX_TOTAL: '5.00', // lifetime budget per token
PIPRAIL_TOKENS: 'USDC',
},
},
},
})
src/mastra/agents/payment-agent.ts
import { Agent } from '@mastra/core/agent'
import { openai } from '@ai-sdk/openai'
import { piprailMcp } from '../mcp'
export const paymentAgent = new Agent({
id: 'payment-agent',
name: 'PipRail Payment Agent',
instructions: 'You can pay x402 URLs for the user, within a hard spend policy. Quote and plan before you pay.',
model: openai('gpt-5'), // any Vercel AI SDK provider, or Mastra's 'openai/…' model router
tools: await piprailMcp.listTools(), // all 8 PipRail tools, registered at agent level
})

For per-request (dynamic) tools instead of agent-level, pass await piprailMcp.listToolsets() to agent.generate(prompt, { toolsets }).

Read-only first run: omit PIPRAIL_PRIVATE_KEY and PipRail boots key-less — discover / quote / register / budget / guide all work; only piprail_pay_request and piprail_plan_payment need the wallet (planning checks your balance, gas, and recipient-readiness, so it needs a key too).

VariableRequiredDefaultPurpose
PIPRAIL_PRIVATE_KEYonly to paySelf-custodial wallet key/seed (EVM 0x…, Solana base58, or a mnemonic). Omit for read-only. Not an API key — never commit it.
PIPRAIL_CHAINbaseChain to pay on (any EVM, or solana/ton/tron/near/sui/aptos/algorand/stellar/xrpl).
PIPRAIL_CHAINSMulti-chain — comma-separated; each takes its own PIPRAIL_<CHAIN>_KEY. Pays whichever chain a 402 asks for.
PIPRAIL_MAX_AMOUNT0.10Max per payment, in the token’s units (≈ $ for USDC/USDT).
PIPRAIL_MAX_TOTAL10.00Lifetime budget per token (server default; this example sets 5.00).
PIPRAIL_TOKENSchain stablesAllowed tokens, comma-separated.
PIPRAIL_SCHEMESonchain-proofAdd exact to also pay standard x402 servers.
PIPRAIL_RPC_URLchain defaultCustom RPC (recommended in production).

piprail_discover · piprail_quote_payment · piprail_plan_payment · piprail_pay_request · piprail_register · piprail_budget · piprail_guide · piprail_verify_receipt. Only piprail_pay_request moves money; the rest are read-only. Full reference: MCP tools. (As noted above, Mastra surfaces these prefixed with the server key — e.g. piprail_piprail_pay_request.)

The runnable example lives at integrations/mastra/piprail/npm install, then:

Terminal window
node verify.mjs --live --mastra # drives the REAL @mastra/mcp MCPClient + the live demo

It proves MCPClient.listTools() surfaces all 8 PipRail tools (each callable, ready for new Agent({ tools })), quotes the live demo (0.01 USDC on Base), and confirms a below-price cap refuses the payment — no funds move. For the checklist, see integrations/TESTING.md.