A comprehensive playground project demonstrating x402 payment integration with both MCP (Model Context Protocol) servers and traditional web APIs. This project showcases how to build payment-enabled tools and services using x402, enabling monetization of MCP tools and API endpoints.
- Payment-enabled MCP tools using x402
- AI Agent integration with multi-step tool calling
- x402 mcp tool support using Cloudflare Agent x402 MCP
- RESTful API endpoints with x402 payment middleware
- Shared service library for code reuse between MCP and web APIs
- Password Generator - Generate secure random passwords with customizable options
- URL Shortener - Shorten URLs using is.gd service
- Inspirational Quotes - Get random motivational quotes by category
- Try with Agent - Interactive chat interface with AI agent using MCP tools
- Try Yourself - Direct API testing interface with wallet integration
- Real-time payment confirmation with USDC balance display
- Tool call visualization with detailed execution results
git clone <repository-url>
cd x402-playgroundEach application has its own dependencies:
# Install frontend dependencies
cd frontend
pnpm install
# Install backend dependencies
cd ../backend
pnpm install# Payment Configuration
PAYEE_ADDRESS=0x958543756A4c7AC6fB361f0efBfeCD98E4D297Db
PAYMENT_PRIVATE_KEY=your_private_key_here
FACILITATOR_URL=https://x402.org/facilitator
# Server Configuration
BACKEND_PORT=3001
MCP_SERVER_URL=http://localhost:3001/mcp
# OpenAI (for agent)
OPENAI_API_KEY=your_openai_api_key_here# Backend URL
NEXT_PUBLIC_BACKEND_URL=http://localhost:3001
# Thirdweb Configuration
NEXT_PUBLIC_THIRDWEB_CLIENT_ID=your_thirdweb_client_id
# Agent Configuration
NEXT_PUBLIC_AGENT_WALLET_ADDRESS=your_agent_wallet_addressFrom the root directory:
# Terminal 1: Start backend
pnpm backend:dev
# Terminal 2: Start frontend
pnpm frontend:devOr run them individually:
# Backend
cd backend
pnpm dev
# Frontend
cd frontend
pnpm dev- Frontend: http://localhost:3000
- Backend API: http://localhost:3001
- MCP Server: http://localhost:3001/mcp
- Health Check: http://localhost:3001/health
x402-playground/
βββ backend/
β βββ lib/
β β βββ services.ts # Shared service functions
β βββ routes/
β β βββ agent.ts # AI agent route with MCP integration
β β βββ mcp.ts # MCP server with x402 payment support
β β βββ x402-endpoint.ts # REST API endpoints with payment middleware
β βββ server.ts # Express server configuration
βββ frontend/
β βββ app/
β β βββ try-with-agent/ # AI agent chat interface
β β βββ try-yourself/ # Direct API testing interface
β β βββ components/ # Reusable UI components
β βββ hooks/ # Custom React hooks
βββ README.md
- Frontend: Next.js 16, React 19, TypeScript, Tailwind CSS
- Backend: Express.js, TypeScript
- Payment: x402 protocol for USDC payments
- AI: Vercel AI SDK with OpenAI GPT-4o-mini
- MCP: Model Context Protocol SDK
- Wallet: Thirdweb for wallet integration
- Blockchain: Base Sepolia testnet
- Node.js 18+
- pnpm (or npm/yarn)
- Wallet with USDC on Base Sepolia testnet
- OpenAI API Key (for agent functionality)
We welcome contributions! Here's how you can help:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
-
Add to shared services (
backend/lib/services.ts):export function myNewService(params: MyParams): MyResult { // Implementation }
-
Add to MCP server (
backend/routes/mcp.ts):server.paidTool( "my_new_tool", "Description", 0.01, // Price in USD { /* zod schema */ }, {}, async (params) => { const result = myNewService(params); return { content: [{ type: "text", text: JSON.stringify(result) }] }; } );
-
Add to REST API (
backend/routes/x402-endpoint.ts):router.post("/my-endpoint", async (req, res) => { const result = myNewService(req.body); res.json({ success: true, data: result }); });
-
Update payment config (
backend/server.ts):"POST /api/x402-endpoint/my-endpoint": { price: "$0.01", network: "base-sepolia", }
Please use GitHub Issues to report bugs or suggest features. Include:
- Description of the issue
- Steps to reproduce
- Expected vs actual behavior
- Environment details
- x402 - Payment protocol
- Cloudflare Agent SDK - MCP tool
- MCP - Model Context Protocol
- Vercel AI SDK - AI SDK
- Thirdweb - Web3 infrastructure
Built with β€οΈ by Brewit for the x402 community