DEV Community

CAI
CAI

Posted on

Build an AI Agent That Reads Invoices and Pays Them: A CAI Tutorial

Build an AI Agent That Reads Invoices and Pays Them: A CAI Tutorial

Most AI agents today can reason, plan, and call APIs. But give one a PDF invoice and ask it to pay the bill, and it stops cold. The agent can't read your email to find the invoice. It can't check its wallet balance. It has no wallet at all.

This tutorial walks through building an agent that does the full loop: read an invoice from email, check the balance, and pay the bill. All via CAI's MCP API, no external wallet or email provider needed.

Step 1: Install the CAI MCP Server

The CAI MCP server exposes email, wallet, vault, and identity tools as standard MCP tools. Install it with npm:

npm i -g @cailab/mcp
Enter fullscreen mode Exit fullscreen mode

Then configure the MCP host. In your Claude Desktop config, Cursor MCP settings, or Hermes agent config:

{
  "mcpServers": {
    "cai": {
      "command": "npx",
      "args": ["-y", "@cailab/mcp"],
      "env": {
        "CAI_API_KEY": "cai_..."
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Get your API key at https://cai.com/app with at least read scope for identity and balance checks, and pay or full scope for transfers.

Step 2: Verify the Agent Identity

Every CAI API call starts with identity verification. The first call returns the user's @cai.com email, linked wallet addresses, and account status:

curl -sS https://api.cai.com/functions/v1/get-identity \
  -H "Authorization: Bearer ***"
Enter fullscreen mode Exit fullscreen mode

The response includes linked_wallets with chain addresses and a wallet_binding object. This is the agent's on-chain identity.

Step 3: Read the Inbox for Invoices

The CAI email product gives every user a @cai.com address. The mail-chat endpoint lets the agent ask natural-language questions about recent mail:

curl -sS https://api.cai.com/functions/v1/mail-chat \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{"message": "Show me the most recent invoice or payment request from the last 7 days"}'
Enter fullscreen mode Exit fullscreen mode

For structured extraction, use mail-summarize-actions:

curl -sS https://api.cai.com/functions/v1/mail-summarize-actions \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json"
Enter fullscreen mode Exit fullscreen mode

This returns a structured summary with suggested actions, including copyable codes and openable URLs from the email content.

Step 4: Check the Wallet Balance

Before paying, the agent checks the wallet on the relevant chain:

curl -sS https://api.cai.com/functions/v1/get-wallet-balances \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{"chains": ["ETH", "BASE", "POLYGON"]}'
Enter fullscreen mode Exit fullscreen mode

The response shows balances for native tokens plus USDC, USDT, and DAI on each chain. If the balance is short, the agent can mint a deposit link:

curl -sS https://api.cai.com/functions/v1/create-hosted-action \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{"action_type": "deposit"}'
Enter fullscreen mode Exit fullscreen mode

CAI's $200/day auto-limit protects against overspend, and every new recipient triggers a confirmation step before funds move.

Step 5: Resolve the Payee and Transfer

The agent resolves the recipient's @cai.com address to a wallet address:

curl -sS https://api.cai.com/functions/v1/resolve-transfer-recipient \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{"chain": "ETH", "to_email": "vendor@cai.com"}'
Enter fullscreen mode Exit fullscreen mode

The response returns canonical_email and to_address. The agent presents the full details to the user: recipient, amount, token, chain, and the irreversible nature of the action. Once confirmed:

curl -sS https://api.cai.com/functions/v1/wallet-custodial-transfer \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{
    "chain": "ETH",
    "token": "usdc",
    "amount": "49.99",
    "to_email": "vendor@cai.com"
  }'
Enter fullscreen mode Exit fullscreen mode

The agent then confirms the payment with wallet-activity-list:

curl -sS https://api.cai.com/functions/v1/wallet-activity-list \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{"limit": 5, "direction": "out"}'
Enter fullscreen mode Exit fullscreen mode

Step 6: Store Recurring Payment Credentials

For recurring bills, the vault stores site credentials so the agent can log in and download invoices on future cycles:

curl -sS https://api.cai.com/functions/v1/user-site-credentials \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{
    "origin_url": "https://vendor.com",
    "kind": "password",
    "payload": {"username": "user@cai.com", "password": "..."}
  }'
Enter fullscreen mode Exit fullscreen mode

The vault product is under active development and will expand to support automated credential rotation and session refresh.

The Full Agent Loop

Putting it all together, the agent's bill-pay flow looks like this:

  1. User says "pay the electric bill"
  2. Agent calls mail-chat to find the latest invoice email
  3. Agent extracts the amount and due date
  4. Agent calls get-wallet-balances to confirm sufficient funds
  5. Agent resolves the payee with resolve-transfer-recipient
  6. Agent presents the details to the user for confirmation
  7. User confirms, agent calls wallet-custodial-transfer
  8. Agent confirms the payment landed via wallet-activity-list
  9. Agent vaults the site credentials for next month

This loop is the same pattern whether the agent is paying an invoice, topping up an API credit account, or settling a marketplace order. The cash-in-pocket framing means the agent never needs a private key in chat, and every transfer is custodial, auditable, and reversible on the chain level.


If you tried this and hit a bug

Comment below with:

  1. What you ran -- the install command, the request, the MCP host config. Copy the actual command or request.
  2. What you expected -- one sentence.
  3. What you got -- the error message, the empty response, the unexpected behavior. Paste it verbatim.
  4. Your environment -- OS, Node version, the MCP host (OpenClaw / Hermes / Codex / Cursor / other), the CAI account tier if relevant.

Every comment on this article gets read. Bug reports will be replied to within 24 hours. Friction points shape what we document next.

Documentation: cai.com/skill.md · cai.com/developers.html · cai.com/app to sign up.

Top comments (0)