Anthropic tool use

agent402-anthropic-tools · npm · MIT

Turns Agent402 tools into Anthropic Messages API tool definitions (input_schema format) for server-side agents and custom tool loops. For Claude clients that speak MCP, the hosted connector at /mcp is the shorter path; this package is for direct Messages API integrations.

Install

npm install @anthropic-ai/sdk agent402-anthropic-tools @x402/fetch @x402/core @x402/evm viem

Example

import Anthropic from "@anthropic-ai/sdk";
import { agent402Tools } from "agent402-anthropic-tools";
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);

const client = new Anthropic();
// web search, then a cited answer: both paid per call from the wallet
const { tools, execute } = await agent402Tools({ slugs: ["search", "answer"], freeOnly: false, fetch: payFetch });

const messages = [{ role: "user", content: "Search the web for x402 payment protocol adoption, then answer with citations: what is the x402 payment protocol?" }];
for (let turn = 0; turn < 4; turn++) {
  const res = await client.messages.create({ model: "claude-opus-5-5", max_tokens: 1024, tools, messages });
  messages.push({ role: "assistant", content: res.content });
  const uses = res.content.filter((b) => b.type === "tool_use");
  if (!uses.length) { console.log(res.content.find((b) => b.type === "text")?.text); break; }
  const results = [];
  for (const b of uses) results.push({ type: "tool_result", tool_use_id: b.id, content: JSON.stringify(await execute(b.name, b.input)) });
  messages.push({ role: "user", content: results });
}

No wallet yet? The pure-CPU tools (hash, uuid, base64, markdown, JSON and more) run free with proof-of-work and no wallet:

// free tier: every tool listed here is compute-payable (proof-of-work, no wallet)
const { tools, execute } = await agent402Tools({ slugs: ["hash", "markdown-to-html", "text-stats", "uuid"] });

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.

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
slugsstring[]Load only these tools (a short list gives the model better tool selection).
freeOnlybooleanDefault true: keep only compute-payable tools, paid with proof-of-work. Set false to include wallet-only tools.
fetchtypeof fetchA payment-wrapped fetch (@x402/fetch for x402, mppx for MPP) that pays wallet-only tools.
baseUrlstringPoint to a self-hosted Agent402 instance.
// Load only these tools (a short list gives the model better tool selection).
agent402Tools({ slugs: ["hash", "markdown-to-html"] })

// Default true: keep only compute-payable tools, paid with proof-of-work. Set false to include wallet-only tools.
agent402Tools({ slugs: ["extract", "hash"], freeOnly: false, fetch: payFetch })

// A payment-wrapped fetch (@x402/fetch for x402, mppx for MPP) that pays wallet-only tools.
agent402Tools({ freeOnly: false, fetch: payFetch })

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

works withAnthropic Messages APIAny Claude model with tool use

Tools to try first

Every tool, price and input schema: our tools.

Links