Skip to main content

Payment-gated MCP server SDK for Algorand

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>=0.1.1"

Quickstart

import uvicorn
from algosdk import account, mnemonic
from algosdk.v2client import algod
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)

# For testing, you must pass an algod_client to the signer!
algod_client = algod.AlgodClient("a"*64, "http://localhost:4001")
signer = DefaultAvmSigner(server_pk, algod_client=algod_client)

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

# 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.2.tar.gz (6.9 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.2-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ai_economy_sdk-0.1.2.tar.gz
  • Upload date:
  • Size: 6.9 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.2.tar.gz
Algorithm Hash digest
SHA256 b51c23881919fce3aa599e712d5dfc3c317891cce5acbf9596f8a244cf4858a4
MD5 e049e0b2c3eb1f517bc18eb650ab2c8b
BLAKE2b-256 1add9c42e8fe35d8b49491e46b7bc7c555c12c3f47c55968743e98e8c3ce3855

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ai_economy_sdk-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.2 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 12e28765536791f0c84b45c250e5afca90d9fff9e1f237c9cc60d1825b10b41d
MD5 07d6565bd1904ae0f0a61716269cdaa4
BLAKE2b-256 879d3541b8497f59131803889c9c98a1d459379ffb60a1eb457ef7bd2b0782c0

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