Skip to main content
Glama
README.md
# get-paid-per-call

A tiny MCP server with one tool that AI agents **pay for per call**: one cent, in USDC, over the
[x402](https://www.x402.org) protocol. It's the reference repo for Savantly's guide to getting paid
by AI agents. Copy the pattern into your own server.

> **Adding payments to your own server?** Point your coding agent at [AGENT-GUIDE.md](./AGENT-GUIDE.md):
> "Follow AGENT-GUIDE.md to make my `search` tool cost $0.01 per call."

> **Status: v0.1, testnet only.** It runs on Base Sepolia with test money. Read "Going live" before
> taking real payments.

## The whole idea in one diff

A free tool and a paid tool differ by one wrapper (`src/paid-server.ts`):

```ts
server.tool('ping', 'Free health check.', {}, async () => ({content: [{type: 'text', text: 'pong'}]}));

server.tool('sun_times', 'Sunrise and sunset… Costs $0.01 per call.', schema,
  paid(async ({lat, lon, date}) => ({content: [{type: 'text', text: JSON.stringify(sunTimes(lat, lon, date))}]})),
);
```

`paid` comes from `@x402/mcp`'s `createPaymentWrapper`, configured once with a facilitator, a
network, a price and the address you're paid at.

## How a paid call works

1. The agent calls `sun_times` with no payment.
2. The server answers with an x402 **PaymentRequired** result: amount, asset (USDC), network, `payTo`.
3. The agent's wallet signs a USDC transfer authorization and retries the call with it attached.
4. The server asks the **facilitator** to verify the payment, runs the tool, then settles it on-chain.
5. The agent gets the answer plus the transaction hash. You get a cent, and a signed record of the call.

The server never holds a private key. It only needs the address you're paid at.

## Quick start (testnet, about 5 minutes)

```bash
npm install
cp .env.example .env              # set PAY_TO to an address you control
npm test                          # free tool works; paid tool returns PaymentRequired
                                  # (npm scripts load .env via Node's --env-file-if-exists; Node >= 22.9)
npm start                         # MCP server at http://localhost:4022/mcp
```

Pay for a call yourself, playing the agent:

```bash
npm run new-wallet                # throwaway TESTNET buyer wallet
# fund it with Base Sepolia USDC from a faucet, then:
BUYER_PRIVATE_KEY=0x... npm run pay
```

With `BUYER_PRIVATE_KEY` set, `npm test` also makes a real testnet payment end to end.

Check any server's paid tool without paying (exit code 0 = it asks for payment correctly):

```bash
MCP_URL=http://localhost:3000/mcp TOOL=my_tool ARGS='{"q":"x"}' EXPECT_PRICE='$0.01' npm run check
```

## Who can pay you today (November 2026)

- **Can pay:** agents developers build with an x402 client (`@x402/mcp`, `@x402/fetch`, the Python
  SDK), Cloudflare's Agents SDK, AWS Bedrock AgentCore payments.
- **Can't yet:** ChatGPT, Claude and Cursor don't pay MCP tools per call natively.
- **So keep a free tier or an API-key path** next to the paid one. In this repo, `ping` is free; in
  yours, it might be a rate-limited free plan.

## Going live (real money)

- Set `X402_NETWORK=eip155:8453` (Base mainnet) and use a mainnet facilitator. The public x402.org
  facilitator serves testnet; for mainnet, check the current x402 docs (Coinbase's CDP facilitator
  needs API keys). *To verify before the guide ships.*
- Receive at an address whose keys you keep offline, or at a split contract if you share revenue.
- Keep records: each settlement hook (`onAfterSettlement`) gives you the payer and transaction.
- Watch for wash traffic: lots of tiny payments from wallets you control or that pay each other.

## CI

`.github/workflows/weekly-paid-call.yml` runs the tests on every push and weekly. With the
`PAY_TO` and `BUYER_PRIVATE_KEY` secrets set (a testnet wallet with faucet USDC), it makes a real paid
call, so we hear about SDK or facilitator changes before readers do.

## Roadmap

- [x] Agent guide ([AGENT-GUIDE.md](./AGENT-GUIDE.md)); eval 1 (2026-11-22): a fresh agent made a sample server's `search` tool paid and passed the check, and its findings are folded in
- [ ] Stripe MPP variant
- [ ] Python variant
- [ ] Hosted demo endpoint that readers' agents can pay
- [ ] Mainnet notes verified

Pinned to `@x402/*` 2.32.0 and `@modelcontextprotocol/sdk` 1.32.2 (checked 2026-11-22).