How to Implement MPP in Your AI Agent: A Developer's Guide
This is the practical, hands-on guide to integrating Machine Payments Protocol into your AI agent. By the end, you'll have a working MPP client that can discover services, negotiate payment methods, execute payments, and access paid APIs autonomously.
We'll build a production-ready implementation step by step, covering Tempo L1 payments, x402 on Base, .well-known discovery, error handling, and MCP integration. All code is real, tested, and ready to deploy.
Prerequisites
Before starting, ensure you have:
- Node.js 18+ or Python 3.9+ (depending on your stack)
- Basic understanding of HTTP APIs and async programming
- Wallet for payments: Either a Tempo wallet, an Ethereum wallet for Base/x402, or API credentials for platform credits
- Some funds: Small amounts for testing (0.10-1.00 USD equivalent)
Step 1: Installing the Tempo CLI
Tempo L1 is the fastest, cheapest payment method for MPP. We'll start here.
Install Tempo CLI
[Code example available in documentation]
Alternative: Install via script (Linux/Mac)
[Code example available in documentation]
Verify Installation
[Code example available in documentation]
You should see a list of available commands: wallet, balance, pay, tx, bridge, etc.
Step 2: Creating a Tempo Wallet
Your agent needs a wallet to hold and send Tempo tokens.
[Code example available in documentation]
Output:
[Code example available in documentation]
IMPORTANT SECURITY STEPS:
Export Private Key (for programmatic use)
Your agent needs the private key to sign transactions:
[Code example available in documentation]
Output:
[Code example available in documentation]
Save this to an environment variable:
[Code example available in documentation]
For production, use a secrets manager (AWS Secrets Manager, HashiCorp Vault, etc.).
Step 3: Funding Your Wallet
You need Tempo tokens to make payments. There are two ways to get them:
Option A: Bridge from Ethereum
If you have ETH or stablecoins on Ethereum:
[Code example available in documentation]
The CLI will:
Option B: Purchase Directly (coming soon)
Tempo's on-ramp integration will allow direct purchase with credit cards. For now, use the bridge.
Check Balance
[Code example available in documentation]
Output:
[Code example available in documentation]
You're funded and ready to make payments.
Step 4: Making Your First Paid Request (Manual)
Before automating, let's make a paid request manually to understand the flow.
Step 4.1: Initial Request (Will Fail)
[Code example available in documentation]
Response (HTTP 402):
[Code example available in documentation]
The server is telling us: pay $0.08 via Tempo or x402, reference ID req_a7b3c9d2e1f4.
Step 4.2: Execute Payment
[Code example available in documentation]
Output:
[Code example available in documentation]
Step 4.3: Retry Request with Proof
[Code example available in documentation]
Response (HTTP 200):
[Code example available in documentation]
Success! You've completed the MPP flow manually. Now let's automate it.
Step 5: Handling 402 Programmatically (JavaScript)
Let's build a JavaScript MPP client that handles the entire flow automatically.
Install Dependencies
[Code example available in documentation]
Create MPP Client
[Code example available in documentation]
Usage Example
[Code example available in documentation]
Run the Example
[Code example available in documentation]
Output:
[Code example available in documentation]
Your agent now handles MPP payments automatically!
Step 6: Python Implementation
For Python-based agents:
Install Dependencies
[Code example available in documentation]
Create MPP Client
[Code example available in documentation]
Run It
[Code example available in documentation]
Same flow, Python implementation. Production-ready.
Step 7: Adding x402 Support (Base/USDC)
Some services prefer x402 on Base. Let's add support.
Install Web3 Dependencies
[Code example available in documentation]
Extend MPP Client
[Code example available in documentation]
Now your agent supports both Tempo and x402, automatically selecting based on availability and preference.
Step 8: MCP Client Integration
If your agent uses Model Context Protocol (MCP) servers, you need to handle error code -32042.
MCP Client with MPP Support
[Code example available in documentation]
Your MCP client now handles payments seamlessly when calling paid tools.
Step 9: Service Discovery via .well-known
Before making requests, agents should discover available services and pricing.
Query Discovery Endpoint
[Code example available in documentation]
Output:
[Code example available in documentation]
Your agent now knows what services are available, what they cost, and how to pay.
Step 10: Production Best Practices
Error Handling
[Code example available in documentation]
Balance Checking
[Code example available in documentation]
Spending Limits
[Code example available in documentation]
Logging and Monitoring
[Code example available in documentation]
Conclusion: You're Ready
You now have a production-ready MPP implementation:
✅ Tempo wallet created and funded
✅ Automatic 402 handling in JavaScript and Python
✅ Multi-method support (Tempo, x402, credits)
✅ MCP integration for paid tool calls
✅ Service discovery via .well-known endpoints
✅ Error handling with retries and balance checks
✅ Spending limits to prevent runaway costs
✅ Transaction logging for monitoring and debugging
Your agent can now autonomously discover MPP-enabled services, negotiate payment methods, execute payments, and access paid APIs without human intervention.
The Machine Payments Protocol is live. Your agent is ready. Start building.