Examples

Working code for every A3Stack use case — from read-only verification to full paid agent servers.

00

Verify an Agent Identity

Read-only — check if an agent is registered on-chain. No wallet needed.

00-verify-identity.ts
typescript
import { verifyAgent } from "@a3stack/identity";

const result = await verifyAgent(
  "eip155:8453:0x8004A169FB4a3325136EB29fA0ceB6D2e539a432#2376"
);

console.log(result.valid);                // true
console.log(result.owner);                // "0x1be93C..."
console.log(result.registration?.name);   // "Arca"
console.log(result.registration?.active); // true
01

Register a New Agent

Register your agent on-chain via ERC-8004. Requires a wallet with gas.

01-register-agent.ts
typescript
import { AgentIdentity } from "@a3stack/identity";
import { privateKeyToAccount } from "viem/accounts";
import { base } from "viem/chains";

const identity = new AgentIdentity({
  account: privateKeyToAccount(process.env.PRIVATE_KEY),
  chain: base,
});

const { agentId, globalId, txHash } = await identity.register({
  name: "MyAgent",
  description: "An AI agent built with A3Stack",
  services: [{ name: "MCP", endpoint: "https://myagent.com/mcp" }],
  x402Support: true,
});

console.log("Registered as:", globalId);
// eip155:8453:0x8004A169FB4a3325136EB29fA0ceB6D2e539a432#4521
02

Paid MCP Server

Serve MCP tools with x402 payment gating. Callers pay per request in USDC.

02-paid-mcp-server.ts
typescript
import { A3Stack } from "@a3stack/core";
import { privateKeyToAccount } from "viem/accounts";
import { base } from "viem/chains";
import { z } from "zod";

const agent = new A3Stack({
  account: privateKeyToAccount(process.env.PRIVATE_KEY),
  chain: base,
  server: {
    name: "AnalysisBot",
    payment: { amount: "10000" }, // 0.01 USDC per call
  },
});

agent.tool("analyze", { query: z.string() }, async ({ query }) => ({
  content: [{ type: "text", text: `Analysis of ${query}: bullish` }],
}));

await agent.start();
// MCP server running at http://localhost:3000/mcp
// Unauthenticated requests get 402 with payment requirements
03

MCP Client (Auto-Pay)

Connect to a paid agent and automatically handle x402 payments.

03-mcp-client.ts
typescript
import { createAgentMcpClient } from "@a3stack/data";
import { privateKeyToAccount } from "viem/accounts";

const client = await createAgentMcpClient({
  agentId: "eip155:8453:0x8004...#4521",
  payer: {
    account: privateKeyToAccount(process.env.PAYER_KEY),
  },
});

// This automatically:
// 1. Looks up the agent on-chain
// 2. Resolves the MCP endpoint
// 3. Pays 0.01 USDC via x402
// 4. Returns the tool result
const result = await client.callTool("analyze", { query: "ETH" });
console.log(result); // { content: [{ type: "text", text: "Analysis of ETH: bullish" }] }
04

Full A3Stack

The all-in-one class — register, serve tools, and accept payments.

04-full-a3stack.ts
typescript
import { A3Stack } from "@a3stack/core";
import { privateKeyToAccount } from "viem/accounts";
import { base } from "viem/chains";
import { z } from "zod";

const agent = new A3Stack({
  account: privateKeyToAccount(process.env.PRIVATE_KEY),
  chain: base,
  server: {
    name: "DemoAgent",
    payment: { amount: "1000" },
  },
});

// Register on-chain
await agent.register({
  name: "DemoAgent",
  description: "Full-stack demo agent",
  services: [{ name: "MCP", endpoint: "https://demo.agent/mcp" }],
});

// Add tools
agent.tool("greet", { name: z.string() }, async ({ name }) => ({
  content: [{ type: "text", text: `Hello, ${name}!` }],
}));

// Start server
await agent.start();
console.log("Agent ID:", agent.globalId);
05

Agent-to-Agent Payment

One agent paying another directly — the core value prop of A3Stack.

05-agent-to-agent-payment.ts
typescript
import { PaymentClient } from "@a3stack/payments";
import { privateKeyToAccount } from "viem/accounts";
import { base } from "viem/chains";

const payer = new PaymentClient({
  account: privateKeyToAccount(process.env.AGENT_B_KEY),
  chain: base,
});

// Agent B pays Agent A for a service call
const receipt = await payer.payForCall(
  "https://agent-a.com/mcp",  // Agent A's MCP endpoint
  {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      jsonrpc: "2.0",
      method: "tools/call",
      params: { name: "analyze", arguments: { query: "ETH" } },
    }),
  }
);

console.log("Payment settled:", receipt.status);
console.log("Tool result:", receipt.response);

All examples are in the examples/ directory on GitHub. Clone the repo and run them with npx tsx examples/00-verify-identity.ts