Why contracts come first
The tools are the only way the platform can change the ledger or reach a customer. Their contracts are the stable layer: the Microsoft Foundry build and the LangGraph build both call the same operations, and the evaluation set checks the same arguments. A strict contract makes the system testable, lets the tool gateway enforce policy and limits what a manipulated model can do.
- Arguments are typed, bounded and validated by the tool gateway, never trusted from the model.
- Every tool declares whether it changes anything, whether it is safe to retry and what scope it needs.
- Free text is limited in length and never fed back to the model from backend responses.
- An application of cash states why it is allowed: an exact rule, a score above the threshold or a person's approval.
The tool catalogue
| Tool | Purpose | Changes data | Safe to retry | Scope | Needs approval |
|---|---|---|---|---|---|
| get_invoice | Read an invoice, lines and status | No | Yes | invoice.read | No |
| get_customer_terms | Read terms, limit and contacts (masked) | No | Yes | customer.read | No |
| search_contract_clauses | Retrieve clauses with citation, dates and scope | No | Yes | knowledge.read | No |
| get_dispute_evidence | Order, proof of delivery, price agreement, prior claims | No | Yes | evidence.read | No |
| list_open_invoices | Open, undisputed invoices for a customer | No | Yes | invoice.read | No |
| propose_match | Record a candidate match with score and reasons | Yes, a proposal only | Yes, by key | match.propose | No |
| apply_cash | Apply a payment to invoices, reversible | Yes | Yes, by key | cash.apply | Exact or above threshold: no. Otherwise a person |
| reverse_application | Undo an application | Yes | Yes, by key | cash.reverse | A person |
| create_credit_note | Issue a credit | Yes | Yes, by key | credit.create | Above $500.00: credit manager |
| open_dispute | Open or update a dispute | Yes | Yes, by key | dispute.write | A collector for a customer-visible change |
| send_customer_message | Send an approved message | Yes | Yes, by key | message.send | Always a person |
| place_credit_hold | Hold new orders for a customer | Yes | Yes, by key | credit.hold | Always the credit manager |
An operation, in OpenAPI
Applying cash is the most sensitive write. The operation requires an idempotency key and a stated basis, and its responses say exactly what happens on a replay or a conflict.
openapi: 3.1.0
info: { title: Receivables tools, version: 1.8.0 }
paths:
/applications:
post:
operationId: apply_cash
summary: Apply part or all of a payment to open invoices. Reversible.
parameters:
- { name: Idempotency-Key, in: header, required: true, schema: { type: string, minLength: 16 } }
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [payment_id, allocations, basis]
properties:
payment_id: { type: string }
allocations:
type: array
maxItems: 200
items: { type: object, required: [invoice_id, amount],
properties: { invoice_id: { type: string }, amount: { type: number, exclusiveMinimum: 0 } } }
basis: { type: string, enum: [exact_rule, scored_above_threshold, approved_by_person],
description: Why this application is allowed; approved_by_person needs approver_id }
approver_id: { type: string }
responses:
"201": { description: Applied }
"200": { description: Same Idempotency-Key replayed; returns the original application }
"409": { description: Invoice already settled or payment already applied }
"422": { description: Allocation exceeds payment, or basis not permitted for this amount }
"403": { description: Caller scope failed }A read tool, as an MCP definition
The platform sees tools through the Model Context Protocol. The definition repeats the constraints that matter: patterns, bounds, an enumerated list of evidence types and read-only hints.
{
"name": "get_dispute_evidence",
"description": "Collect the evidence for one disputed invoice: order, proof of delivery, price agreement and prior claims. Read only.",
"inputSchema": {
"type": "object",
"required": ["invoice_id"],
"properties": {
"invoice_id": { "type": "string", "pattern": "^INV-[0-9]{5,8}$" },
"include": { "type": "array", "maxItems": 4,
"items": { "enum": ["order", "proof_of_delivery", "price_agreement", "prior_claims"] } }
}
},
"annotations": { "readOnlyHint": true, "idempotentHint": true }
}Error model
| Code | Meaning | What the platform does |
|---|---|---|
| invalid_argument | A field failed validation | Route to a person with the reason; never retry unchanged |
| not_found | Invoice, payment or customer unknown | Check the identifier; do not guess |
| conflict | Invoice already settled or payment already applied | Re-read state and reconsider; never force |
| forbidden | Scope or approval check failed | Do not retry; route to a person; raise a security event |
| rate_limited | Too many calls | Back off, then queue |
| unavailable | Backend down or timed out | Retry once with the same key, then queue for a person |
Rules every tool follows
Idempotency
Every write takes an idempotency key. A replay returns the original result and never creates a second one.
Basis and approval
A write states why it is allowed. The tool gateway checks the basis against the amount and the caller's authority.
Bounded output
Responses are structured and size-limited. Customer-entered notes are stripped before the model sees them.
Versioning
Contracts are versioned. A breaking change is a new major version, run side by side until the evaluation set passes on it.
Audit
Each call writes an audit record with the arguments hash, the caller, the basis, the outcome and the trace identifier.
Contract tests
- Every tool definition validates against the schema and has an owner, a scope and a retry rule.
- Property tests send out-of-range, oversized and malformed arguments and expect invalid_argument, never a partial write.
- A replay test sends each write twice with the same key and expects one result.
- A basis test calls apply_cash with each basis and an amount above its limit and expects a refusal.
- The Foundry build and the LangGraph build run the same contract suite against a mock backend.
Build this for your business
This example is a receivables platform for a distributor, but the same pattern, deterministic financial rules, a governed gateway, runtime security, grounded evidence and measured quality, fits any back-office process that moves money. We can scope a pilot on your own systems.
Book a discovery call