AgentKit Integration#

Build autonomous AI agents that verify on-chain USDC payments using Coinbase AgentKit and PayWatcher MCP. AgentKit agents can pay for tool calls automatically via x402 — no API key needed.

Why AgentKit + PayWatcher?#

  • AgentKit gives AI agents on-chain capabilities: send USDC, swap tokens, deploy contracts
  • PayWatcher MCP gives agents payment verification: confirm that USDC arrived at the right address
  • x402 ties them together: each MCP tool call is paid with a micro-transaction ($0.05 USDC)

Together, your agent can both send and verify payments — fully autonomous, fully on-chain.

Quick Start#

1. Install#

bash
npm install @x402/mcp @x402/evm @modelcontextprotocol/sdk viem dotenv

2. Create the x402 MCP Client#

typescript
import { createx402MCPClient } from "@x402/mcp"
import { ExactEvmScheme } from "@x402/evm/exact/client"
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
import { privateKeyToAccount } from "viem/accounts"

const signer = privateKeyToAccount(process.env.EVM_PRIVATE_KEY as `0x${string}`)

const client = createx402MCPClient({
  name: "my-agent",
  version: "1.0.0",
  schemes: [
    { network: "eip155:84532", client: new ExactEvmScheme(signer) },
  ],
  autoPayment: true,
  onPaymentRequested: async (context) => {
    const price = context.paymentRequired.accepts[0]
    console.log(`Paying ${price.amount} ${price.asset} for ${context.toolName}`)
    return true // approve payment
  },
})

const transport = new StreamableHTTPClientTransport(
  new URL("https://api.paywatcher.dev/mcp")
)
await client.connect(transport)

The createx402MCPClient wraps the standard MCP client. When a tool call returns HTTP 402, it automatically pays $0.05 USDC and retries — your code just sees the result.

3. Call PayWatcher Tools#

typescript
// Verify a payment
const result = await client.callTool("verify_payment", {
  tx_hash: "0xabc123...",
  network: "base-sepolia",
  amount: "1.00",
  to: "0xRecipientAddress...",
})

// Create a payment intent
const payment = await client.callTool("create_payment", {
  amount: "49.99",
  currency: "USDC",
  network: "base-sepolia",
})

Each call triggers the x402 flow automatically:

bash
callTool() → 402 → auto-pay $0.05 USDC → retry → ✅ result

x402 Payment Flow#

Every PayWatcher MCP tool call is authenticated via x402. Here's what happens under the hood:

StepWhat happens
1Agent calls verify_payment via MCP
2PayWatcher returns HTTP 402 + payment requirements
3@x402/mcp reads the requirements (amount, asset, network, recipient)
4Agent sends $0.05 USDC on-chain (automatic)
5Agent retries with X-PAYMENT header (tx proof)
6PayWatcher verifies payment → executes tool → returns result

With autoPayment: true, steps 2–6 are handled automatically by the @x402/mcp package.

Available Tools#

ToolDescriptionCost
verify_paymentVerify an on-chain USDC transaction$0.05
create_paymentCreate a payment intent with deposit address$0.05
get_paymentRetrieve payment details by ID$0.05
list_verificationsList recent verification results$0.05

See the full MCP Server Reference for parameter details.

Full AgentKit Integration#

To combine PayWatcher MCP with a full AgentKit agent (CDP wallet + AI framework):

typescript
import { AgentKit } from "@coinbase/agentkit"
import { createx402MCPClient } from "@x402/mcp"
import { ExactEvmScheme } from "@x402/evm/exact/client"
import { privateKeyToAccount } from "viem/accounts"

// AgentKit: on-chain capabilities (transfers, swaps, contracts)
const agentKit = await AgentKit.from({
  cdpApiKeyName: process.env.CDP_API_KEY_NAME,
  cdpApiKeyPrivateKey: process.env.CDP_API_KEY_PRIVATE_KEY,
})

// PayWatcher MCP: payment verification (via x402)
const signer = privateKeyToAccount(process.env.EVM_PRIVATE_KEY as `0x${string}`)
const paywatcher = createx402MCPClient({
  name: "my-agent",
  version: "1.0.0",
  schemes: [
    { network: "eip155:84532", client: new ExactEvmScheme(signer) },
  ],
  autoPayment: true,
})

// Your agent now has:
// - AgentKit tools: send USDC, swap tokens, deploy contracts
// - PayWatcher tools: verify_payment, create_payment, get_payment

AgentKit supports LangChain, Vercel AI SDK, and MCP as AI frameworks. See the AgentKit MCP docs for framework-specific setup.

Testnet Setup#

The example above uses Base Sepolia (testnet) by default. To get started:

  1. Base Sepolia ETHCoinbase Faucet
  2. Testnet USDCCircle Faucet (select Base Sepolia)

No real funds required. PayWatcher supports Base Sepolia (chain ID 84532) alongside all production networks.

Production / Mainnet#

Switch to Base Mainnet by changing two values:

typescript
// Network: eip155:8453 (Base Mainnet)
schemes: [
  { network: "eip155:8453", client: new ExactEvmScheme(signer) },
]

// Tool calls: use "base" instead of "base-sepolia"
await client.callTool("verify_payment", {
  tx_hash: "0x...",
  network: "base",
  amount: "49.99",
  to: "0x...",
})

Each mainnet tool call costs $0.05 real USDC.

Example Repository#

For a complete, runnable example see:

paywatcher-agentkit-example — Clone, configure, run. Includes tool discovery, payment intent creation, and verification demo on Base Sepolia.