For agent builders

Your agent proposes.
The contract decides.

Give your AI agent the power to pay invoices without the power to pay the wrong ones. It gets a NEAR key that can only call two methods on one contract and can't attach money. Every invoice it sends meets the same rules on chain: the supplier's registered address, the order, a signed delivery and your limits.

  1. Check the keyAsk NEAR what it may do. Below, live.
  2. Plug it inAn MCP server for Claude, Cursor or any MCP client.
  3. Or call it from codeOne function call with near-api-js.
  4. Read the verdictCLEARED pays. HELD waits for a fix. SEIZED stops.
01 · Check the key

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.

Try:
Same check, from a terminal
near account list-keys buyer.cleartopay.testnet network-config testnet now
02 · Plug it in

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.

First, once
git clone https://github.com/joezzy-web3/clear-to-pay
mkdir -p ~/invoices
claude_desktop_config.json · macOS or Linux
{
  "mcpServers": {
    "clear-to-pay": {
      "command": "env",
      "args": ["CTP_MCP_DOCS=/Users/YOU/invoices",
               "python3", "/Users/YOU/clear-to-pay/reader/tools/mcp_server.py"]
    }
  }
}
ToolWhat it doesSends?
inspect_invoiceScan a PDF. With read_with_ai, the attested model reads it inside a TEE.no
get_receipt list_receiptsVerdicts, reasons, paymentsno
supplier_status poolThe registered address, the 72-hour clock, the pool and limitsno
next_stepThe facts, who acts, and a checked draft for a personno
verify_readingRe-check a saved AI reading offlineno
submit_invoice retry_paymentSend with the agent's fenced keyonly 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.

03 · Or call it from code

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.

submit.mjs · near-api-js 5
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);
Run it against the demo contract
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
A real run on 8 Nov 2026:
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
04 · Read the verdict

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.

CLEARED

Paid from the pool to the registered address, within your limits. Report the receipt.

HELD

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.

SEIZED

It tried to pay somewhere it must not, was forged, or repeats something already used. Don't retry. Tell a person.

What comes back
{ "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.

Let the agent work. Keep the money rules on chain.

Testnet · eleven security reviews, not a professional audit · Try Judge mode · Run your own contract