This guide provides a fast track for MCP Server Developers, to help you easily integrate AgentPay and start monetizing your services.
AgentPay is currently in Early Access. To get early access to AgentPay, please sign up for the Waitlist here.
We’ll cover:
  1. Registering your MCP Server on the AgentPay Hub and getting your Service Token.
  2. Integrating the AgentPay SDK, by:
    • Installing the AgentPay Python SDK (Node.js coming soon!).
    • Initializing AgentPayClient with your Service Token.
    • Using the consume() method to charge for tool usage.
    • Running to a full example implementation.
  3. Tracking your earnings and getting paid via AgentPay Hub.
Let’s start monetizing your MCP Server!

Prerequisites

This quickstart guide assumes a working knowledge of:

1. Register & Get Service Token

First, you’ll need to register your MCP Server on the AgentPay Hub. This is where you’ll also receive your unique Service Token, which is essential for authenticating your server with AgentPay. Steps:
  • Sign Up / Log In: Head over to AgentPay and sign up or log in.
As AgentPay is in Early Access, access is currently managed via our Waitlist, which you can sign up for here.
  • Navigate to Servers: Once logged in, you’ll find a “My Servers” tab in the main navigation.
  • Register New Server: Within the “My Servers” section, click on “Register New Server” and complete the relevant fields, like name and description (all information can be updated later).
    • Please note: You will see fields to describe your server’s pricing. This is display-only information for users to understand more about your service offering.
    • However, the actual charging logic and amounts are defined by you in your SDK integration. For more details on pricing, see Defining Your Pricing Models.
  • Receive Your Service Token: Upon successful registration, your unique Service Token will be displayed.
    • It will look something like this: st-apay-7sj3...-98dk...
    • Critical: This token is shown to you only once. Copy it immediately and store it in a secure place (e.g. a password manager or environment file).
    • If you lose it, you can regenerate a new one, but this will invalidate the old token, requiring you to update your server configuration.
For more details about this setup process, as well as ongoing management, please see Registering Your MCP Server.

2. Integrating the AgentPay SDK

Next, let’s go through the key steps to integrate the AgentPay SDK.

i. Install AgentPay Python SDK

Open your terminal or command prompt, activate your project’s virtual environment (recommended), and run the following command:
pip install agentpay-sdk
This will download and install the latest version of the agentpay-sdk and its dependencies.
The agentpay-sdk package currently on PyPI is a placeholder to reserve the name during Early Access. To get the actual SDK now, join the Waitlist.

ii. Initialize AgentPayClient

In your Python server code, import and initialize AgentPayClient. This client object is your main interface for interacting with AgentPay.
from agentpay_sdk import AgentPayClient
import os

# Best practice: Store your Service Token as an environment variable
SERVICE_TOKEN = os.getenv("AGENTPAY_SERVICE_TOKEN")

if not SERVICE_TOKEN:
    # In a real application, you might want to log this error
    # and potentially disable paid features if the token is missing.
    raise ValueError("AGENTPAY_SERVICE_TOKEN environment variable not set.")

agentpay_client = AgentPayClient(service_token=SERVICE_TOKEN)

print("AgentPayClient initialized successfully.")
You can find more details about AgentPayClient in the AgentPayClient Initialization Guide or the AgentPayClient SDK Reference.

iii. Add a Consumption Call

When a user calls one of your paid tools, you’ll use the consume() method to record the usage and charge them using their User API Key.
  • Please note: This quickstart assumes you have already extracted the user’s User API Key from the incoming request (typically from a header like X-AGENTPAY-API-KEY).
  • For guidance on how to extract the User API Key in various Python web frameworks, please see Extracting API Keys from Requests.
You also have the option to validate a User API Key independently before processing a request, e.g. immediately after extraction in middleware. Here’s a simplified example of how you might integrate the consume() call into a tool function, following the best practice of processing your tool’s core logic before attempting to charge:
import uuid

# Your tool processing logic
async def process_tool_logic(tool_params: dict):
    # Replace this with your tool's actual operations
    print(f"Executing core logic for the tool with params: {tool_params}")
    # Simulate a successful operation
    return {"result": "Tool logic executed successfully", "params_received": tool_params}

# Your MCP Server tool handler
async def my_paid_tool(tool_params: dict):
    # 1. --- Execute your tool's core logic ---
    print(f"Processing tool with params: {tool_params}")
    tool_output = await process_tool_logic(tool_params)
    print(f"Tool logic completed. Output: {tool_output}")

    # 2. --- If core logic is successful, then attempt to charge ---
    COST_CENTS = 10 # Example: 10 cents for this tool usage
    usage_event_id = str(uuid.uuid4()) # Unique ID for idempotency

    print(f"Attempting to charge {COST_CENTS} cents for tool usage (ID: {usage_event_id})...")
    
    consumption_result = agentpay_client.consume(
        api_key=user_api_key, # Extracted previously from the request (see notes for more info)
        amount_cents=COST_CENTS,
        usage_event_id=usage_event_id
        # You can also pass optional metadata, description, etc.
    )

    if consumption_result.success:
        print(f"Successfully charged.")
        # Return the result of your tool's core logic
        return {"status": "success", "data": tool_output, "payment_info": "Charged successfully"}
    else:
        # If charging failed (e.g. insufficient funds, invalid API key post-tool execution)
        # AgentPay automatically handles complex payment failures, helping you easily handle different scenarios based on your business logic (see "Charging for Usage" guide for more details)
        print(f"CRITICAL: Tool executed but failed to charge: {consumption_result.error_message} (Code: {consumption_result.error_code})")
        return {"status": "error", "data": tool_output, "message": f"Tool executed, but payment failed: {consumption_result.error_message}"}
Key terms:
  • Service Token: Your server’s secret key for AgentPay.
  • User API Key: The key your client users use to authenticate with your server, which AgentPay uses for billing.
  • Consumption: The act of recording usage and charging a user via AgentPay.
AgentPay handles complex payment scenarios automatically. If a user’s balance is insufficient, the system will track the outstanding payment and automatically process it when they add funds. This means you can focus on building great tools while AgentPay manages the payment complexities.
For comprehensive details on the consume() method, including all parameters, response objects, and advanced error handling, please see the detailed guide on Charging for Usage and the Consume Method SDK Reference.

iv. Run a Full Example

To see a more complete, runnable example of an MCP Server integrated with AgentPay, including how to set it up and test the flows, please refer to our Example Implementation.

3. Tracking Earnings & Getting Paid

Once your server is operational and processing paid requests, you’ll be able to track your accumulated earnings directly within the AgentPay Hub. AgentPay facilitates payouts of these earnings, typically on a monthly basis. You can configure your preferred payout method in the Hub, with options including:
  • Stripe Connect Express: For receiving payouts in fiat currency to your bank account.
  • Stablecoins: For receiving payouts in supported cryptocurrencies.
For detailed information on setting up your payout preferences, payout schedules, and any applicable fees, please refer to the Configuring Payouts guide. This quickstart provides the basic building blocks. Explore the MCP Server Developers section of this documentation for more detailed guides on each aspect of building and monetizing your MCP Server with AgentPay!