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
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_..."
}
}
}
}
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 ***"
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"}'
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"
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"]}'
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"}'
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"}'
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"
}'
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"}'
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": "..."}
}'
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:
- User says "pay the electric bill"
- Agent calls
mail-chatto find the latest invoice email - Agent extracts the amount and due date
- Agent calls
get-wallet-balancesto confirm sufficient funds - Agent resolves the payee with
resolve-transfer-recipient - Agent presents the details to the user for confirmation
- User confirms, agent calls
wallet-custodial-transfer - Agent confirms the payment landed via
wallet-activity-list - 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:
- What you ran -- the install command, the request, the MCP host config. Copy the actual command or request.
- What you expected -- one sentence.
- What you got -- the error message, the empty response, the unexpected behavior. Paste it verbatim.
- 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)