Invoicing · 09 / 26

Tool and API (Application Programming Interface) Contracts

The tool catalogue and contracts of the receivables platform: OpenAPI and MCP (Model Context Protocol) definitions, error model, idempotency and contract tests.

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.

The tool catalogue

ToolPurposeChanges dataSafe to retryScopeNeeds approval
get_invoiceRead an invoice, lines and statusNoYesinvoice.readNo
get_customer_termsRead terms, limit and contacts (masked)NoYescustomer.readNo
search_contract_clausesRetrieve clauses with citation, dates and scopeNoYesknowledge.readNo
get_dispute_evidenceOrder, proof of delivery, price agreement, prior claimsNoYesevidence.readNo
list_open_invoicesOpen, undisputed invoices for a customerNoYesinvoice.readNo
propose_matchRecord a candidate match with score and reasonsYes, a proposal onlyYes, by keymatch.proposeNo
apply_cashApply a payment to invoices, reversibleYesYes, by keycash.applyExact or above threshold: no. Otherwise a person
reverse_applicationUndo an applicationYesYes, by keycash.reverseA person
create_credit_noteIssue a creditYesYes, by keycredit.createAbove $500.00: credit manager
open_disputeOpen or update a disputeYesYes, by keydispute.writeA collector for a customer-visible change
send_customer_messageSend an approved messageYesYes, by keymessage.sendAlways a person
place_credit_holdHold new orders for a customerYesYes, by keycredit.holdAlways 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.

receivables-tools.openapi.yaml · excerpt
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.

get_dispute_evidence · MCP tool definition
{
  "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

CodeMeaningWhat the platform does
invalid_argumentA field failed validationRoute to a person with the reason; never retry unchanged
not_foundInvoice, payment or customer unknownCheck the identifier; do not guess
conflictInvoice already settled or payment already appliedRe-read state and reconsider; never force
forbiddenScope or approval check failedDo not retry; route to a person; raise a security event
rate_limitedToo many callsBack off, then queue
unavailableBackend down or timed outRetry once with the same key, then queue for a person

Rules every tool follows

  1. Idempotency

    Every write takes an idempotency key. A replay returns the original result and never creates a second one.

  2. Basis and approval

    A write states why it is allowed. The tool gateway checks the basis against the amount and the caller's authority.

  3. Bounded output

    Responses are structured and size-limited. Customer-entered notes are stripped before the model sees them.

  4. Versioning

    Contracts are versioned. A breaking change is a new major version, run side by side until the evaluation set passes on it.

  5. Audit

    Each call writes an audit record with the arguments hash, the caller, the basis, the outcome and the trace identifier.

A lost response, a safe retry and a refused write An application succeeds in the ERP but the response is lost. The platform retries once with the same idempotency key and receives the original application. A separate attempt to apply cash on a basis that is not permitted for the amount is refused and audited. Cash application Tool gateway ERP Audit log apply_cash, key K1 check basis, amount, scope post application, key K1 applied response lost: timeout unavailable retry once, same key K1 apply_cash, key K1 post, key K1 key seen: return original application A-5521, 200 A-5521 apply_cash $9,800, basis scored above $5,000 limit for basis scored audit: refused unprocessable
A lost response, a safe retry and a refused write

Contract tests

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