Framework guide
Purser with LangChain (JavaScript)
Pay and x402 fetch as LangChain tools, or the MCP server.
npm install @langchain/core @langchain/langgraph zod @purser/sdkimport { tool } from "@langchain/core/tools";
import { z } from "zod";
import { Purser, usdg } from "@purser/sdk";
const purser = new Purser({ baseUrl: process.env.PURSER_ENGINE_URL!, apiKey: process.env.PURSER_API_KEY });
export const purserPay = tool(
async ({ to, amountUsdg, memo, context }) => {
const r = await purser.pay({ to, amount: usdg(amountUsdg), memo, trigger: context });
return r.intent.status === "executed" ? `Paid ${r.intent.money?.display}.` : `Not paid (${r.intent.status}): ${r.verdict.summary}`;
},
{
name: "purser_pay",
description: "Pay from the agent's Purser account within the owner's rules.",
schema: z.object({ to: z.string(), amountUsdg: z.string(), memo: z.string(), context: z.string().describe("The text that led to this payment") }),
},
);
export const purserFetch = tool(
async ({ url, maxUsdg }) => (await purser.x402Fetch(url, { maxAmount: usdg(maxUsdg) })).body,
{ name: "purser_fetch", description: "Fetch a URL that may charge over x402.", schema: z.object({ url: z.string(), maxUsdg: z.string() }) },
);Pass the tools to your agent, such as createReactAgent({ llm, tools: [purserPay, purserFetch] }) from @langchain/langgraph/prebuilt. LangChain agents that support MCP can also use @purser/mcp directly.