A runnable end-to-end demonstration of the full Tomopay payment loop: server-side gateway (@tomopay/gateway) + buyer client (@tomopay/client) communicating over an in-memory MCP transport.
- Free tool passthrough — tools without a price execute immediately, no payment flow triggered
- Transparent auto-pay — when a paid tool returns
PAYMENT_REQUIRED, the client auto-detects the challenge, signs a payment proof, and retries without any changes to your agent code - Multi-tool payments — two paid tools at different prices (
translateat $0.01,summarizeat $0.05) - Budget tracking —
client.getSpendingStats()returns per-session and daily spend totals - Receipt flow — every successful paid call returns a
_receiptblock alongside the tool result
npm install
npm run demoRequires Node.js 18+.
=== Tomopay End-to-End Demo ===
Server and client connected
--- Test 1: Free tool (echo) ---
Result: Echo: Hello world!
Free tool works — no payment needed
--- Test 2: Paid tool (translate, $0.01) ---
Auto-paying $0.01 for "translate" via mock
Result: [Translated to Spanish]: Hello world
Paid tool works — payment handled transparently
--- Test 3: Paid tool (summarize, $0.05) ---
Auto-paying $0.05 for "summarize" via mock
Result: Summary of "The quick brown fox jumps over the lazy dog. This ...": This is a key point.
Second paid tool works
--- Test 4: Spending stats ---
Total spent: $0.06
Today: $0.06
Calls: 2
Budget tracking works
=== All tests passed! ===
The full payment loop works:
Agent calls tool -> Gateway returns PAYMENT_REQUIRED
-> Client auto-detects -> Checks budget -> Signs payment
-> Retries with proof -> Gateway verifies -> Tool executes
-> Receipt returned -> Spending tracked
Agent calls tool
│
▼
Gateway checks pricing table
│
├─ No price set ──────────────────► Execute immediately (passthrough)
│
└─ Price set, no _payment arg ───► Return PAYMENT_REQUIRED + challenge
│
▼
Client receives challenge
│
Check budget limits
│
Sign payment proof
│
Retry call with _payment arg
│
▼
Gateway verifies proof
│
Execute tool, return result + receipt
- @tomopay/gateway — server-side payment gateway package
- @tomopay/client — buyer client SDK package
- Tutorial: Monetise your MCP tool in 5 minutes