Skip to content

feat(server): add getBalance() for balance lookups#141

Merged
emilioacc merged 1 commit intomainfrom
feat/get-balance
Mar 16, 2026
Merged

feat(server): add getBalance() for balance lookups#141
emilioacc merged 1 commit intomainfrom
feat/get-balance

Conversation

@emilioacc
Copy link
Contributor

Summary

Add getBalance() to @atxp/server so MCP servers can check a user's available balance without charging. This calls the auth server's new POST /balance endpoint (merged in auth#227).

Usage

import { getBalance, requirePayment } from '@atxp/server';

// Pre-flight: check if user can afford the upper bound
const balance = await getBalance();
if (balance.lt(upperBoundCost)) {
  // Trigger 402 payment required flow
}

// Do expensive work...
const result = await generateImage(prompt);

// Charge actual cost
await requirePayment({ price: actualCost });

Changes

packages/atxp-server

  • src/types.ts — Add BalanceRequest type, add getBalance to PaymentServer interface
  • src/paymentServer.ts — Implement getBalance in ATXPPaymentServer (calls POST /balance)
  • src/getBalance.ts — New exported helper that uses ATXP context (same pattern as requirePayment)
  • src/index.ts — Export getBalance and BalanceRequest
  • src/serverTestHelpers.ts — Update mock paymentServer() to include getBalance
  • Bump version to 0.10.7

Tests

  • paymentServer.test.ts — Tests for ATXPPaymentServer.getBalance (success, error, missing field)
  • getBalance.test.ts — Tests for the exported helper (context handling, token passing, error propagation)

Part of

ATXP-1712 — Part 2 of 3 (auth server ✅ → SDK → image service)

Add a getBalance() function to @atxp/server that allows MCP servers to
check a user's available balance without charging. This calls the auth
server's new POST /balance endpoint.

Changes:
- Add BalanceRequest type and getBalance to PaymentServer interface
- Implement getBalance in ATXPPaymentServer (calls POST /balance)
- Add getBalance() exported helper (uses ATXP context like requirePayment)
- Update serverTestHelpers mock to include getBalance
- Add tests for ATXPPaymentServer.getBalance and getBalance() helper
- Bump @atxp/server to 0.10.7

Linear-issue: ATXP-1712
@emilioacc emilioacc merged commit 8755587 into main Mar 16, 2026
1 check passed
@emilioacc emilioacc deleted the feat/get-balance branch March 16, 2026 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant