Home › Docs › Adapters › LangChain
LangChain Adapter
agent402-langchainFour meta tools (find, route, call, about) as LangChain.js tool objects for LangChain agents and LangGraph nodes.
Install
npm install agent402-langchain @langchain/core @langchain/langgraph @langchain/openai zod @x402/fetch @x402/core @x402/evm viem
Quick start
import { agent402Tools } from "agent402-langchain";
import { ChatOpenAI } from "@langchain/openai";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
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 = createReactAgent({ llm: 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();
Configuration
| Option | Type | Description |
|---|---|---|
fetch |
typeof fetch |
A payment-wrapped fetch (@x402/fetch for x402, mppx for MPP) that agent402_call uses for wallet-only tools. |
fetchImpl |
typeof fetch |
The plain fetch used for the unpaid lookups (find, route, about) and proof-of-work calls. Defaults to the global fetch. |
baseUrl |
string |
Point 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 with
LangChain JS
LangGraph
createReactAgent
Any LangChain chat model with tool calling