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
pip install ai-economy-sdk
Quickstart
import uvicorn
from algosdk import account, mnemonic
from ai_economy_sdk import AIEconomySDK, DefaultAvmSigner
# 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)
signer = DefaultAvmSigner(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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ai_economy_sdk-0.1.1.tar.gz.
File metadata
- Download URL: ai_economy_sdk-0.1.1.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf57b3ee65b7169d7176378a07ebce145d10b9ef55d71c116e2fafed8cc5f12f
|
|
| MD5 |
652558d8bb2eff0e68788c63245221f2
|
|
| BLAKE2b-256 |
590d10a8590fd2b0fd06b9a1d426f27ce3d63e521bab27d287407e588c8e7942
|
File details
Details for the file ai_economy_sdk-0.1.1-py3-none-any.whl.
File metadata
- Download URL: ai_economy_sdk-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9790722dc392b1c3d177cdceb58666434373500b669cede7805927c819693c0a
|
|
| MD5 |
db1c330b957c43f4ab46dd389b83485a
|
|
| BLAKE2b-256 |
4a8e4c77e327b6e41494978a9c8825c7576858e0294598ed2d759d9ede42d8b2
|