OpenAI function calling

agent402-openai-tools · npm · MIT

Turns Agent402 tools into OpenAI function-calling definitions. The returned tools array is the same JSON used by chat.completions, Assistants v2 and the Responses API, and execute runs whatever tool call the model returns.

Install

npm install openai agent402-openai-tools

Example

import OpenAI from "openai";
import { agent402Tools } from "agent402-openai-tools";

const openai = new OpenAI();
const { tools, execute } = await agent402Tools({ slugs: ["extract", "hash", "render", "screenshot"] });

const res = await openai.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [{ role: "user", content: "Get the title of https://example.com/article" }],
  tools,
});

const call = res.choices[0].message.tool_calls?.[0];
if (call) {
  const result = await execute(call.function.name, JSON.parse(call.function.arguments));
  console.log(result);
}

What it exposes

  • One native tool per catalog slug you pass in slugs (a short list gives the model better tool selection).
  • With no slugs, the free-tier catalog: every compute-payable tool (freeOnly: true is the default).
  • An execute(name, args) function that runs a tool call and pays for it underneath.
  • baseUrl points the adapter at a self-hosted Agent402 instance.

How payment works

  • Free tier: compute-payable tools settle with a sha256 proof-of-work solved in-process. No wallet, no API key.
  • Wallet-only tools (browser, network, memory, live data): set freeOnly: false and pass a payment-wrapped fetch. An @x402/fetch fetch pays in USDC over x402; a stock mppx fetch pays over MPP. The same 402 carries both offers.
  • Prices come from the live catalog at /api/pricing and are quoted in every 402 before anything is signed.
import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";

const client = new x402Client();
registerExactEvmScheme(client, { signer: privateKeyToAccount(process.env.AGENT_KEY) });
const payFetch = wrapFetchWithPayment(fetch, client);

const { tools, execute } = await agent402Tools({ freeOnly: false, fetch: payFetch });

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

Extract article$0.010
extract
Hash$0.001
hash
Browser render$0.02
render
Screenshot$0.004
screenshot
Web search$0.02
search
Web answer$0.08
answer

Every tool, price and input schema: the catalog.

Links

agent402-openai-tools on npm · source on GitHub · configuration reference
Guides: Make your AI agent pay for what it needs: x402 in 5 minutes · Use Agent402 from Claude Code, Cursor, VS Code, Windsurf, Cline, Roo Code, Codex CLI, Gemini CLI, Muse Code, Continue, ElizaOS, AgentCore and any OpenAI SDK
All integrations