Skip to main content
This guide builds upon the official MCP Server tutorial to create a remote MCP server with AgentPay integration. We’ll start with a basic weather server and enhance it with remote capabilities and monetization.

Prerequisites

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.
Create a .env file to store your AgentPay Service Token:

Step 2: Basic Server Implementation

Create weather_server.py with the basic MCP server structure per the official MCP Server tutorial:
Test the basic server:

Step 3: Add Remote Server Capabilities

Now, let’s modify the server to run as a remote HTTP server using Starlette. Update weather_server.py to add the necessary imports and server setup:
This 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
Test the remote server:
Your server is now running at 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 the X-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:
Your server is now fully integrated with AgentPay, handling API key validation and usage charging.

Next Steps