What may this key do?
Before an agent holds a key, ask the chain what that key can do, not a config file. A Clear to Pay agent key is a function-call key: one contract, two methods (submit_invoice, execute_payment), a gas allowance, and no deposit. It can't transfer tokens, add keys or touch any other contract.
Paste any testnet account. Every key on it is listed with its fence. A full-access key is marked red: never give one to an agent.
near account list-keys buyer.cleartopay.testnet network-config testnet now
Add the MCP server
reader/tools/mcp_server.py speaks MCP over stdio with the Python standard library only. Clone the repo, point it at a folder of invoices, and ask your agent things like “inspect INV-2208.pdf”, “why was receipt 1 held, and what do I tell the supplier?” or “is Qingmar payable yet?”
CTP_MCP_DOCS is the only folder the agent may name files in. Links and .. are refused. Hidden text in a PDF is reported by page and reason only; its words never reach the agent.
git clone https://github.com/joezzy-web3/clear-to-pay mkdir -p ~/invoices
{
"mcpServers": {
"clear-to-pay": {
"command": "env",
"args": ["CTP_MCP_DOCS=/Users/YOU/invoices",
"python3", "/Users/YOU/clear-to-pay/reader/tools/mcp_server.py"]
}
}
}claude mcp add clear-to-pay -e CTP_MCP_DOCS=$HOME/invoices -- python3 $HOME/clear-to-pay/reader/tools/mcp_server.py
{
"mcpServers": {
"clear-to-pay": {
"command": "python3",
"args": ["/Users/YOU/clear-to-pay/reader/tools/mcp_server.py"],
"env": { "CTP_MCP_DOCS": "/Users/YOU/invoices" }
}
}
}{
"mcpServers": {
"clear-to-pay": {
"command": "wsl",
"args": ["env", "CTP_MCP_DOCS=/home/YOU/invoices",
"python3", "/home/YOU/clear-to-pay/reader/tools/mcp_server.py"]
}
}
}| Tool | What it does | Sends? |
|---|---|---|
inspect_invoice | Scan a PDF. With read_with_ai, the attested model reads it inside a TEE. | no |
get_receipt list_receipts | Verdicts, reasons, payments | no |
supplier_status pool | The registered address, the 72-hour clock, the pool and limits | no |
next_step | The facts, who acts, and a checked draft for a person | no |
verify_reading | Re-check a saved AI reading offline | no |
submit_invoice retry_payment | Send with the agent's fenced key | only if enabled |
Sending is off until the operator turns it on: add CTP_MCP_ALLOW_SUBMIT=1, CTP_AGENT_ACCOUNT (the buyer) and CTP_AGENT_KEY_FILE (the function-call key's file, needs near-cli-rs) to the server's environment. Before every send the server asks NEAR what the key may do, and refuses anything wider than the fence above or a gas allowance over CTP_MCP_MAX_ALLOWANCE (1 NEAR by default). Reads that matter must agree across every configured RPC server.
One function call
Any agent framework that can run JavaScript can submit. Check the fence first, then send the five signed objects: the invoice, the supplier's signature, the delivery note, the delivery signatures and the document's fingerprint. Your code never chooses where money goes: the contract pays only the supplier's registered address.
The full script is tools/integrate/submit.mjs. Out of the box it uses Judge mode's public key on the demo contract, so you can run it with no account.
import { connect, keyStores, KeyPair, providers } from "near-api-js"; const ACCOUNT = "buyer.example.testnet", CONTRACT = "pay.example.testnet"; const key = KeyPair.fromString(process.env.CTP_KEY); // a function-call key, never full access const keyStore = new keyStores.InMemoryKeyStore(); await keyStore.setKey("testnet", ACCOUNT, key); const near = await connect({ networkId: "testnet", nodeUrl: "https://test.rpc.fastnear.com", keyStore }); // 1. Ask NEAR what this key may do. Refuse anything wider. const ak = await near.connection.provider.query({ request_type: "view_access_key", finality: "final", account_id: ACCOUNT, public_key: key.getPublicKey().toString() }); const fc = ak.permission?.FunctionCall; if (!fc || fc.receiver_id !== CONTRACT || [...fc.method_names].sort().join() !== "execute_payment,submit_invoice") throw new Error("this key is not fenced to Clear to Pay"); // 2. Submit the signed objects. The contract decides. const account = await near.account(ACCOUNT); const outcome = await account.functionCall({ contractId: CONTRACT, methodName: "submit_invoice", args: { invoice, supplier_signature, delivery, delivery_signatures, document_hash }, gas: 300_000_000_000_000n, attachedDeposit: 0n }); const { receipt_id, verdict, reasons, payment } = providers.getTransactionLastResult(outcome);
cd clear-to-pay/tools/integrate && npm install node submit.mjs # dry run: reads the key's fence, prints the call node submit.mjs --send --attack # a fooled agent swaps the payee
key fence (read from NEAR): judge.cleartopay.testnet · execute_payment, submit_invoice · gas allowance 4.988 NEAR · no deposit possible
submit_invoice INV-2208-J30 · payee qingmar-payments.testnet
receipt #16: SEIZED · invalid_supplier_signature, payee_mismatch
View the transaction on NearBlocks
Three answers, one receipt
Every call writes a receipt on chain and returns receipt_id, verdict, machine-readable reasons and, when cleared, where the payment stands. Teach your agent one rule per verdict.
Every reason code is listed in the README and llms.txt. The MCP tool next_step turns any receipt into who acts next and a checked draft for a person.
Paid from the pool to the registered address, within your limits. Report the receipt.
Something doesn't add up: a price, a quantity, a missing delivery signature, a limit, or a new address still in its 72-hour wait. Nothing is marked used. Fix it, then submit again.
It tried to pay somewhere it must not, was forged, or repeats something already used. Don't retry. Tell a person.
{ "receipt_id": 16,
"verdict": "Seized",
"reasons": ["invalid_supplier_signature", "payee_mismatch"],
"payment": null }Holding a fenced key, your agent can at worst submit an invoice that gets HELD or SEIZED, or burn its gas allowance. Changing a supplier's address needs the supplier's key, your approval and 72 hours. Pausing, limits and approvals stay with your own wallet.