Prerequisites
- Python 3.10 or higher
- Basic understanding of MCP (from the official tutorial)
- An AgentPay account and Service Token (see Quickstart Guide)
AgentPay is currently in Early Access. To get early access to AgentPay, please sign up for the Waitlist here.
Step 1: Project Setup
First, create a new project directory and set up your environment: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..env
file to store your AgentPay Service Token:
Step 2: Basic Server Implementation
Createweather_server.py
with the basic MCP server structure per the official MCP Server tutorial:
Step 3: Add Remote Server Capabilities
Now, let’s modify the server to run as a remote HTTP server using Starlette. Updateweather_server.py
to add the necessary imports and server setup:
- Mounts the FastMCP SSE app at the root path to handle MCP protocol
- Enables CORS for development (you should customize this for production)
- Uses uvicorn to run the server
http://localhost:8000
and ready to accept requests from MCP clients.
Step 4: Add AgentPay Integration
Now, let’s integrate AgentPay following our four key steps:i. Initialize AgentPayClient
Add the AgentPay client initialization near the top of the file:ii. Extract User API Key
Add the context variable and middleware to extract the API key from theX-AGENTPAY-API-KEY
header:
iii. Validate User API Key
Update the middleware to validate the API key:iv. Consume Usage
Finally, update the MCP tools to charge for usage:Next Steps
- Learn how to test your server with an MCP Client.
- Review the working example repo on GitHub.