Vercel AI SDK
Gives a Vercel AI SDK agent four meta tools that reach the whole catalog: the model describes a task, agent402_find or agent402_route picks the tool, and agent402_call runs and pays for it. Works with generateText and streamText on any provider the SDK supports.
Install
npm install agent402-ai-sdk ai @ai-sdk/openai zod @x402/fetch @x402/core @x402/evm viem
Example
import { agent402Tools } from "agent402-ai-sdk";
import { generateText, stepCountIs } from "ai";
import { openai } from "@ai-sdk/openai";
import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
const payClient = new x402Client();
registerExactEvmScheme(payClient, { signer: privateKeyToAccount(process.env.AGENT_KEY) });
const payFetch = wrapFetchWithPayment(fetch, payClient);
// an object keyed by tool name; agent402_call pays search and answer through this fetch
const tools = await agent402Tools({ fetch: payFetch });
const { text } = await generateText({
model: openai("gpt-4o"),
tools,
stopWhen: stepCountIs(6), // find, search, answer, then reply
prompt: "Search the web for x402 payment protocol adoption, then answer with citations: what is the x402 payment protocol?",
});
No wallet yet? The pure-CPU tools (hash, uuid, base64, markdown, JSON and more) run free with proof-of-work and no wallet:
// the same four tools with no fetch: agent402_call pays pure-CPU tools with proof-of-work
const tools = await agent402Tools();
What it exposes
agent402_find: a plain-language task in, the best-matching tools out (slug, route, price, input schema, a ready example).agent402_route: ranks tools across every indexed x402 seller;include: "external"leaves this server out.agent402_call: calls a tool by slug and pays for it.agent402_about: the service manifest (payment options, capability map, MCP connector).agent402ToolSpecs()returns the same four as framework-agnostic specs (name, description, JSON Schema, execute) with no framework dependency.
How payment works
- Pure-CPU tools:
agent402_callfetches a proof-of-work challenge and solves it in-process, so no wallet is needed. - Wallet-only tools: pass
fetch, a payment-wrapped fetch.@x402/fetchpays in USDC over x402 on whichever chain your wallet is funded for; a stockmppxfetch pays over MPP. - Unpaid lookups (find, route, about) use
fetchImpland never spend anything.
A call that fails is not charged: settlement runs after the tool and only on a successful response. Background: What x402 is · MPP (Machine Payments Protocol).
Tools to try first
Every tool, price and input schema: the catalog.
Links
agent402-ai-sdk on npm · source on GitHub · configuration reference
Guides: One payment, any proven seller: the x402 Smart Order Router · Make your AI agent pay for what it needs: x402 in 5 minutes
All integrations