Skip to main content

A FastAPI/MCP integration for Algorand x402 payment gating.

Project description

AI Economy SDK

AI Economy SDK is an extension for the Model Context Protocol (MCP) that instantly turns your free AI tools into paid, on-chain resources. It wraps any standard MCP tool with the x402 payment middleware, requiring users to pay per-request via Algorand transactions before the tool executes.

Features

  • Seamless MCP Integration: Just decorate your Python functions and build your app.
  • Dynamic Pricing: Use custom callbacks to charge different amounts based on user inputs or location.
  • Subscription Support (Method B): Query the blockchain to bypass payments if the user holds a valid Subscription ASA.
  • Bulk Discounts: Query indexers to apply automatic half-price or free tiers to power users based on their trailing 30-day usage.
  • Replay Protection: Automatically drops double-spend attempts based on hash verification.

Installation

Because this SDK relies on advanced AVM extensions for the x402 protocol, you must install both packages:

pip install x402-algorand ai-economy-sdk

Quickstart

import uvicorn
from algosdk import account, mnemonic
from ai_economy_sdk import AIEconomySDK

# 1. Setup Algorand Signer for your Facilitator Wallet
server_pk = mnemonic.to_private_key("YOUR_MNEMONIC_HERE")
server_addr = account.address_from_private_key(server_pk)

class MyAvmSigner:
    # See examples/ for a full implementation of AvmSigner
    pass

signer = MyAvmSigner(server_pk)

# 2. Initialize the SDK
sdk = AIEconomySDK(
    server_signer=signer,
    network="algorand:SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=", # CAIP-2 Network
    asset_id="1003", # Use "0" for ALGO or your custom ASA ID for USDC
    pay_to_address=server_addr
)

# 3. Define your Dynamic Pricing Callback
def my_pricing(arguments: dict, sender_address: str = None) -> int:
    return 15_000 if arguments.get("premium") else 5_000

# 4. Decorate your Tool
@sdk.add_tool(name="do_work", description="Executes AI task", pricing_func=my_pricing)
async def do_work(premium: bool = False) -> str:
    return "Done! Enjoy your premium service!" if premium else "Done with standard service!"

# 5. Build and Serve
app = sdk.build_app()

if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8001)

Subscriptions and Discounts

The SDK provides helper methods has_active_subscription() and get_past_usage_amount() out of the box to build complex tokenomics quickly. See the examples/ directory for an implementation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ai_economy_sdk-0.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ai_economy_sdk-0.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file ai_economy_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: ai_economy_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for ai_economy_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 261ebf5758e476d3dbb76dadc660f83ed81f6f75bd332625f69985ddd3662465
MD5 4580bcbce1cdcf2889bb5ac9e5610fe0
BLAKE2b-256 e15ddf81a01180ee28d1a489f35496db26171f9160f2bad1ecaff9ca6344c6b9

See more details on using hashes here.

File details

Details for the file ai_economy_sdk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ai_economy_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for ai_economy_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 80e0c6193999a7290e529073061f1cb791abfecc89dfbd7ac7e7d03bffd4341a
MD5 0af097c317c55555359d7a2edb0aa232
BLAKE2b-256 002838c900981fac7a13824047a607989b3726d31f9e51415062d24a50575569

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page