Import
Signature
Open an MCP session that pays for tool calls with x402 on Algorand. Connects to a remote Model Context Protocol server over Streamable HTTP or the legacy SSE transport, initializing a ClientSession configured with an x402-aware HTTP client. Outgoing requests automatically carry identity headers (if enabled) and auto-sign 402 payment challenges for exact Algorand scheme payments. Notes On receiving HTTP 402 with PAYMENT-REQUIRED, the client verifies requirements, constructs an Algorand transaction or 2-txn atomic group (where the server sponsors transaction fees as fee payer), signs it, and resubmits under PAYMENT-SIGNATURE. Funds are only deducted from the buyer’s account if the tool execution succeeds.

Parameters

str
required
URL of the server’s MCP endpoint: "http://localhost:8000/mcp" for Streamable HTTP or "http://localhost:8000/sse" for SSE. (The parameter keeps its 0.4 name for compatibility; it accepts either kind of URL.)
str
required
Base64-encoded 32-byte Algorand private key used by the buyer to sign payments and identity proofs.
str
required
Target Algorand network name ("mainnet", "testnet", "localnet"), canonical CAIP-2 identifier ("algorand:<32-char-prefix>"), or legacy long ID.
str | None
default:"None"
Custom URL for the algod node. If None, defaults to the standard endpoint for the given network (AlgoNode for public networks, localhost for localnet).
str | None
default:"None"
Custom authentication token header for the algod node. Default is None.
bool
default:"True"
Whether to attach cryptographic identity headers (x-sender-*) proving account ownership on every request to enable vendor discounts and passes.
float
default:"30.0"
HTTP request timeout in seconds for normal requests. Stream reads allow up to 300 seconds.
SpendControlsArg
default:"None"
Client-side spending limits enforced by x402Client before payment authorization:
  • None (default): official x402 defaults apply — only default assets (native ALGO and USDC) are allowed, capped at 1,000,000 base atomic units (1 ALGO / 1 USDC) per payment.
  • False: disables all spend controls (any asset, unlimited amount).
  • dict: custom spend controls following the x402 SpendControls schema, e.g.: {"max_amount_per_payment": False} removes the amount cap while keeping default assets, {"allowed_assets": True} allows any asset while keeping default payment cap, or {"allowed_assets": [{"network": network, "asset": "12345", "max_amount_per_payment": "5000000"}]} whitelists a specific ASA.
ClientTransport | None
default:"None"
MCP transport to use. When omitted, Streamable HTTP is used if the URL path ends in /mcp and SSE otherwise, which keeps 0.4 behaviour for SSE URLs.

Yields

mcp.ClientSession
Active, initialized MCP client session capable of invoking tools via session.call_tool(tool_name, arguments).

Raises

  • ConfigurationError: If transport is not "streamable-http" or "sse".
  • SpendLimitExceededError: If a tool call needs a payment that spend_controls forbid (amount above the cap, or an asset that is not allowed). Nothing is signed and no funds move. The x402 error is its __cause__. Other errors propagate unchanged.

Examples

See also

  • PaymentGate: Server-side payment middleware.
  • AIEconomySDK: High-level server framework.
Source: ai_economy_sdk/client.py