Loyalty · 09 / 26

Tool and API (Application Programming Interface) Contracts

The tool catalogue and contracts of the loyalty 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 a balance, a hold or a message. 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_member_summaryRead tier, balance and status, with identity maskedNoYesmember.readNo
get_flight_activityRead a segment, class and prior creditsNoYesactivity.readNo
search_program_termsRetrieve program terms with citation, effective date and tierNoYesknowledge.readNo
get_claim_evidenceGather ticket, flight and prior-claim evidenceNoYesevidence.readNo
calculate_milesCompute miles and qualifying miles by the rulesNoYesrules.readNo
price_awardPrice an award with a plain-language reasonNoYespricing.readNo
propose_creditRecord a proposed credit with evidenceYes, a proposal onlyYes, by keycredit.proposeNo
post_milesCredit miles to an accountYesYes, by keyledger.postWithin 10,000 and equal to the rule result: no. Otherwise a person
reverse_entryReverse a ledger entryYesYes, by keyledger.reverseA person
redeem_awardDebit miles for an awardYesYes, by keyredeem.writeStep-up result required
place_account_holdPlace a time-limited holdYesYes, by keyaccount.holdFraud analyst for high-risk, rule for step-up
check_consentRead channel and purpose permissionNoYesconsent.readNo
send_member_messageSend an approved messageYesYes, by keymessage.sendApproved template and a consent check

An operation, in OpenAPI

Posting miles is the most sensitive write. The operation requires an idempotency key and a stated basis, the miles must equal the rule result, and its responses say exactly what happens on a replay or a conflict.

loyalty-tools.openapi.yaml · excerpt
openapi: 3.1.0
info: { title: Loyalty tools, version: 1.6.0 }
paths:
  /ledger/entries:
    post:
      operationId: post_miles
      summary: Credit miles to an account. Append-only; corrections are reversals.
      parameters:
        - { name: Idempotency-Key, in: header, required: true, schema: { type: string, minLength: 16 } }
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [member_id, miles, source, basis]
              properties:
                member_id: { type: string }
                miles: { type: integer, minimum: 1, maximum: 250000 }
                qualifying: { type: integer, minimum: 0 }
                source: { type: string, enum: [flight, partner, claim, goodwill] }
                source_ref: { type: string }
                basis: { type: string, enum: [rule, approved_by_person, goodwill_approved],
                         description: Why this credit is allowed; the last two need approver_id }
                approver_id: { type: string }
      responses:
        "201": { description: Posted }
        "200": { description: Same Idempotency-Key replayed; returns the original entry }
        "409": { description: Segment already credited to this or another member }
        "422": { description: Basis not permitted for this amount, or miles not equal to the rule result }
        "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: a pattern for flight numbers, required fields and read-only hints.

get_flight_activity · MCP tool definition
{
  "name": "get_flight_activity",
  "description": "Read the segment a member claims: whether it was flown by this member, in which booking class, and whether miles were already credited. Read only.",
  "inputSchema": {
    "type": "object",
    "required": ["member_id", "flight_number", "flight_date"],
    "properties": {
      "member_id": { "type": "string" },
      "flight_number": { "type": "string", "pattern": "^[A-Z0-9]{2}[0-9]{1,4}$" },
      "flight_date": { "type": "string", "format": "date" }
    }
  },
  "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_foundSegment, member or claim unknownCheck the identifier; do not guess
conflictSegment already credited, or an entry already reversedRe-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. Rules decide amounts

    Credits must equal the rule engine's result for the source. A model-supplied number that differs is refused.

  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 credit A credit posts but the response is lost. The platform retries once with the same idempotency key and receives the original entry. A separate attempt to post miles that differ from the rule result is refused and audited. Claims agent Tool gateway Ledger Audit log post_miles, key K1 check basis, amount, scope post entry, key K1 entry E-77120 posted response lost: timeout unavailable retry once, same key K1 post_miles, key K1 post, key K1 key seen: return original E-77120, 200 E-77120 post_miles 9,000, rule says 5,177 miles differ from rule result audit: refused unprocessable
A lost response, a safe retry and a refused credit

Contract tests

Build this for your business

This example is a loyalty platform for an airline, but the same pattern, deterministic financial rules, a governed gateway, runtime security, consent-aware personalisation and measured quality, fits any program that holds a customer balance and makes offers. We can scope a pilot on your own systems.

Book a discovery call