docs.businys.dev

Core features

MCP Revenue

Per-call billing instrumentation for MCP servers. Define a pricing config, top up agent credit wallets, and let the middleware handle the rest — budget checks before calls, credit deduction on success, and a full ledger per agent.

How it works

The Revenue middleware sits between Confirmation and Audit in the pipeline:

  1. Budget check — before forwarding the call, checks the agent's wallet balance against the tool price. If the balance is insufficient and enforceBudget is true, returns a 402 insufficient_credits error.
  2. Call executes — the tool handler runs normally.
  3. Deduct on success — credits are deducted only if the call succeeds. Errors are not charged.

Setup

server.tsts
import { createMCPProxy, FileAdapter } from "@businys/ops"

const proxy = createMCPProxy({
  revenue: {
    pricing: {
      // Default price for any tool not listed in overrides
      default: { perCall: 1 },

      // Per-tool overrides (exact tool name)
      overrides: {
        create_invoice:  { perCall: 5 },
        send_email:      { perCall: 3 },
        generate_report: { perCall: 10 },
      },
    },

    // Block calls when balance is 0 (default: true)
    // Set to false for record-only mode (no enforcement)
    enforceBudget: true,
  },
})

Managing wallets

Wallets are keyed by agentId. Top up wallets before sending traffic:

server.tsts
// Top up an agent's wallet
await proxy.storage.topUpCredits("agent-abc", 1000, "Initial grant")
await proxy.storage.topUpCredits("agent-abc", 500, "Monthly refill")

// Check balance
const wallet = await proxy.storage.getWallet("agent-abc")
console.log(wallet?.balance) // 1500

// View ledger
const ledger = await proxy.storage.getLedger("agent-abc", { limit: 20 })
// ledger[0] = { type: "topup", amount: 500, balanceAfter: 1500, ... }

Insufficient credits response

When an agent's balance is too low, the middleware returns a structured error instead of forwarding the call:

Responsejson
{
  "error": "insufficient_credits",
  "message": "Insufficient credits for create_invoice. Balance: 3, required: 5.",
  "balance": 3,
  "required": 5
}

The HTTP status is 402 Payment Required. The call is not forwarded to the tool handler and is not charged.

Record-only mode

Set enforceBudget: false to meter usage without blocking calls. Useful for analytics before enabling enforcement, or for internal agents that should never be blocked:

server.tsts
revenue: {
  pricing: { default: { perCall: 1 } },
  enforceBudget: false, // record usage, never block
}

Wallets can go negative in record-only mode. Use this to audit usage patterns before deciding on pricing.

Ledger entry types

TypeDescription
chargeCredit deducted after a successful tool call
topupCredits added (grant, refill, or Stripe webhook)
refundCredits returned (e.g. after a disputed call)

PricingConfig

FieldTypeDescription
default{ perCall: number }Applied to any tool not in overrides
overrides?Record<string, { perCall: number }>Per-tool price overrides (exact tool name match)

Stripe integration

The hosted dashboard at businys.dev shows per-agent wallet balances, ledger history, and revenue analytics. Connect Stripe to automate top-ups from your billing system:

  1. Set up a Stripe webhook for checkout.session.completed
  2. In the webhook handler, call storage.topUpCredits(agentId, amount)
  3. The agent's balance updates immediately and appears in the dashboard