LangChain.js and LangGraph

agent402-langchain · npm · MIT

LangChain.js tool objects for Agent402, ready for any LangChain or LangGraph agent. Four meta tools cover the whole catalog, so the agent's tool budget stays small however many endpoints the catalog holds.

Install

npm install agent402-langchain langchain @langchain/core @langchain/openai zod @x402/fetch @x402/core @x402/evm viem

Example

import { agent402Tools } from "agent402-langchain";
import { ChatOpenAI } from "@langchain/openai";
import { createAgent } from "langchain";

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 array of four tools; agent402_call pays search and answer through this fetch
const tools = await agent402Tools({ fetch: payFetch });
const agent = createAgent({ model: new ChatOpenAI({ model: "gpt-4o-mini" }), tools });

const result = await agent.invoke({
  messages: [{ role: "user", content: "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_call fetches 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/fetch pays in USDC over x402 on whichever chain your wallet is funded for; a stock mppx fetch pays over MPP.
  • Unpaid lookups (find, route, about) use fetchImpl and never spend anything.

A paid call that ends in any status of 400 or above is not charged over x402: settlement runs after the tool and only on a successful response. Background: What x402 is · MPP (Machine Payments Protocol).

Configuration

OptionTypeDescription
fetchtypeof fetchA payment-wrapped fetch (@x402/fetch for x402, mppx for MPP) that agent402_call uses for wallet-only tools.
fetchImpltypeof fetchThe plain fetch used for the unpaid lookups (find, route, about) and proof-of-work calls. Defaults to the global fetch.
baseUrlstringPoint to a self-hosted Agent402 instance.
// A payment-wrapped fetch (@x402/fetch for x402, mppx for MPP) that agent402_call uses for wallet-only tools.
agent402Tools({ fetch: payFetch })

// The plain fetch used for the unpaid lookups (find, route, about) and proof-of-work calls. Defaults to the global fetch.
agent402Tools({ fetchImpl: globalThis.fetch })

// Point to a self-hosted Agent402 instance.
agent402Tools({ baseUrl: "https://my-agent402.example.com" })

works withLangChain JSLangGraphcreateAgentAny tool-calling LangChain model

Tools to try first

Every tool, price and input schema: our tools.

Links