Model Context Protocol (MCP) is a protocol for passing context between LLMs and other AI agents. This page shows how to use the x402 payment protocol with MCP to make paid API requests through an MCP server, and how to connect it to Claude Desktop.Documentation Index
Fetch the complete documentation index at: https://docs.cloud.coinbase.com/llms.txt
Use this file to discover all available pages before exploring further.
What is this integration?
This guide walks you through running an MCP server that can access paid APIs using the x402 protocol. The MCP server acts as a bridge between Claude Desktop (or any MCP-compatible client) and a paid API (such as the sample weather API in the x402 repo). When Claude (or another agent) calls a tool, the MCP server will:- Detect if the API requires payment (via HTTP 402 with
PAYMENT-REQUIREDheader) - Automatically handle the payment using your wallet via the registered x402 scheme
- Return the paid data to the client (e.g., Claude)
Prerequisites
- Node.js v20+ (install via nvm)
- pnpm v10 (install via pnpm.io/installation)
- An x402-compatible server to connect to (for this demo, we’ll use the sample express server with weather data from the x402 repo, or any external x402 API)
- An Ethereum wallet with USDC (on Base Sepolia, Base Mainnet, Polygon Mainnet, Arbitrum Mainnet, or World Mainnet) and/or a Solana wallet with USDC (on Devnet or Mainnet)
- Claude Desktop with MCP support
Quick Start
1. Install and Build
2. Configure Claude Desktop
Add the MCP server to your Claude Desktop configuration:3. Start the x402 Server
Make sure your x402-compatible server is running at the URL specified inRESOURCE_SERVER_URL:
4. Restart Claude Desktop
Restart Claude Desktop to load the new MCP server, then ask Claude to use theget-data-from-resource-server tool.
Environment Variables
| Variable | Description | Required |
|---|---|---|
EVM_PRIVATE_KEY | Your EVM wallet’s private key (0x prefixed) | One of EVM or SVM required |
SVM_PRIVATE_KEY | Your Solana wallet’s private key (base58 encoded) | One of EVM or SVM required |
RESOURCE_SERVER_URL | The base URL of the paid API | Yes |
ENDPOINT_PATH | The specific endpoint path (e.g., /weather) | Yes |
Implementation
The MCP server uses@x402/axios to wrap axios with automatic payment handling:
How It Works
The MCP server exposes a tool that, when called, fetches data from a paid API endpoint. If the endpoint requires payment, the x402 axios wrapper automatically handles the payment handshake:- 402 Response: The server returns HTTP 402 with
PAYMENT-REQUIREDheader - Parse Requirements: The wrapper extracts payment requirements from the header
- Create Payment: Uses the registered scheme (EVM or SVM) to create a payment payload
- Retry Request: Sends the original request with the
PAYMENT-SIGNATUREheader - Return Data: Once payment is verified, the data is returned to Claude
Multi-Network Support
The example supports both EVM (Base, Polygon, Ethereum) and Solana networks. The x402 client automatically selects the appropriate scheme based on the payment requirements:network field in the payment requirements:
eip155:*networks use the EVM schemesolana:*networks use the SVM scheme
Response Handling
Payment Required (402)
When a payment is required, the client receives:- Parses the payment requirements
- Creates and signs a payment using the appropriate scheme
- Retries the request with the
PAYMENT-SIGNATUREheader
Successful Response
After payment is processed, the MCP server returns:Dependencies
The example uses these x402 v2 packages:How the Pieces Fit Together
- x402-compatible server: Hosts the paid API (e.g., weather data). Responds with HTTP 402 and
PAYMENT-REQUIREDheader if payment is required. - MCP server (this implementation): Acts as a bridge, handling payment via
@x402/axiosand exposing tools to MCP clients. - Claude Desktop: Calls the MCP tool, receives the paid data, and displays it to the user.
Next Steps
- See the full example in the repo
- Try integrating with your own x402-compatible APIs
- Extend the MCP server with more tools or custom logic as needed
- Learn about building x402 servers