Import
Signature
Payment gate protecting FastMCP server tools with x402 on Algorand. Integrates payment challenges, verification, and settlement into FastMCP’s HTTP transports (Streamable HTTP and legacy SSE). Supports fixed, dynamic, subscription-based, pass-based, and tiered bulk pricing. Notes When a tool is invoked, the gate checks identity proofs to identify the caller. If the caller holds an unexpired pass or active subscription, price is 0. The fee payer pays the 2,000 microALGO group fee in ALGO whatever asset the price is in. Settles on chain only if tool succeeds.

Constructor parameters

ServerSigner
required
Signer (e.g., DefaultAvmSigner) providing merchant keys and node connection to sponsor transaction fees as fee payer.
str
required
Network name ("mainnet", "testnet", "localnet") or CAIP-2 identifier.
str
required
Algorand address where service payments are collected.
int | str
default:"0"
Asset ID accepted for payments (0 for native ALGO in microALGO, or ASA ID).
Prices | None
default:"None"
Tool pricing mapping or callable with signature (tool, args, sender) -> int.
int
default:"0"
Fallback price in base units for tools not explicitly listed in prices.
ChainLookup | None
default:"None"
ChainReader (or compatible object) for subscriptions, past usage, and passes.
int | str | None
default:"None"
ASA ID of the subscription token. If held by caller, tools are free.
float
default:"30.0"
Maximum age in days of latest subscription token transfer.
int | None
default:"None"
Cumulative spend threshold triggering the bulk discount rate.
float
default:"0.5"
Multiplier applied to base price when bulk threshold is exceeded.
int
default:"30"
Window in days for computing cumulative past spend.
int
default:"5000"
Maximum number of entries maintained in memory.
int
default:"1000"
Network minimum transaction fee in microALGO.
bool
default:"False"
If False, rejects or clamps ALGO prices in (0, 2000] that fail to cover the 2-transaction fee-payer cost.
str | None
default:"None"
Path to SQLite database file for multi-process replay protection.
str | None
default:"None"
Restricts valid subscription tokens to those received from this address.
dict[str, int] | None
default:"None"
Mapping of eligible pass scope names to minimum prices.
float
default:"30.0"
Validity window in days for vendor passes.

Raises

  • ConfigurationError: (a ValueError) If allow_loss=False, asset_id=0, and any configured price is in the loss-making range (0, 2 * min_fee_per_txn]; or if subscription_issuer is set but chain.has_active_subscription has no issuer parameter.

Examples

Methods

price_for

Compute the effective invocation price for a tool call. Evaluates pricing based on pass holdings, subscription status, caller identity, argument contents, and cumulative usage volume. Notes Clamps positive ALGO prices to 2001 microALGO (2 * minFee + 1) if allow_loss=False. Parameters
str
required
Name of the MCP tool requested.
ToolArguments | None
default:"None"
Invocation argument mapping supplied to the tool.
str | None
default:"None"
Verified Algorand address of the caller.
Returns
int
Effective price in base units (microALGO for ALGO, base units for ASA). Returns 0 if the call is free, covered by a pass, or covered by subscription.
Example

middleware

Generate Starlette middleware definitions for an ASGI application. The middleware inspects the JSON-RPC body of every HTTP POST (any path, any transport) and gates each tools/call. Use it directly when you build the FastMCP HTTP app yourself, together with SettleOnSuccessMiddleware (priced calls are refused with HTTP 500 until that middleware exists):: mcp.add_middleware(SettleOnSuccessMiddleware(gate)) app = mcp.http_app(middleware=gate.middleware()) wrap does both steps for you. Returns
list of StarletteMiddleware
Middleware stack handling x402 payment challenges and verification.

wrap

Wrap a FastMCP instance into an ASGI application protected by this PaymentGate. Adds SettleOnSuccessMiddleware to mcp and builds the FastMCP HTTP app with the gate’s ASGI middleware. Parameters
FastMCP
required
FastMCP application instance defining MCP tools.
Transport | None
default:"None"
HTTP transport to serve. "streamable-http" (alias "http") serves the MCP Streamable HTTP transport at /mcp; "sse" serves the legacy SSE transport at /sse + /messages/. When omitted, SSE is used (the 0.4 behaviour) and a FutureWarning is emitted: the default will change to "streamable-http" in a future release.
str | None
default:"None"
Endpoint path, passed to FastMCP.http_app (/mcp or /sse by default).
Any
default:"{}"
Returns
starlette.applications.Starlette
ASGI application ready to serve with uvicorn.
Raises
  • ConfigurationError: If transport is not one of the supported values.
Example

close

Close resources used by this PaymentGate, including the replay store. Returns
None
Example

See also

  • AIEconomySDK: High-level builder encapsulating PaymentGate.
  • DefaultAvmSigner: Default signing implementation.
Source: ai_economy_sdk/gate.py