Skip to main content
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.

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:
  1. Detect if the API requires payment (via HTTP 402 with PAYMENT-REQUIRED header)
  2. Automatically handle the payment using your wallet via the registered x402 scheme
  3. Return the paid data to the client (e.g., Claude)
This lets you (or your agent) access paid APIs programmatically, with no manual payment steps.

Prerequisites

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 in RESOURCE_SERVER_URL:

4. Restart Claude Desktop

Restart Claude Desktop to load the new MCP server, then ask Claude to use the get-data-from-resource-server tool.

Environment Variables

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:
  1. 402 Response: The server returns HTTP 402 with PAYMENT-REQUIRED header
  2. Parse Requirements: The wrapper extracts payment requirements from the header
  3. Create Payment: Uses the registered scheme (EVM or SVM) to create a payment payload
  4. Retry Request: Sends the original request with the PAYMENT-SIGNATURE header
  5. 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:
When the server returns a 402 response, the client checks the network field in the payment requirements:
  • eip155:* networks use the EVM scheme
  • solana:* networks use the SVM scheme

Response Handling

Payment Required (402)

When a payment is required, the client receives:
The wrapper automatically:
  1. Parses the payment requirements
  2. Creates and signs a payment using the appropriate scheme
  3. Retries the request with the PAYMENT-SIGNATURE header

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-REQUIRED header if payment is required.
  • MCP server (this implementation): Acts as a bridge, handling payment via @x402/axios and exposing tools to MCP clients.
  • Claude Desktop: Calls the MCP tool, receives the paid data, and displays it to the user.

Porting to the CDP SDK (TypeScript)

The example above manages a raw private key directly in the MCP server’s environment. If you’d rather pay from a CDP-managed wallet — no private key in your .env — swap the client setup for CdpX402Client from the CDP SDK. Everything else (the MCP server, the tool definition, wrapAxiosWithPayment) stays the same, since CdpX402Client extends x402Client.

1. Install the CDP SDK instead of raw signer packages

You no longer need viem, @solana/kit, or @scure/base for signing — the CDP SDK provisions and signs with its own managed wallet.

2. Replace private keys with CDP credentials

EVM_PRIVATE_KEY and SVM_PRIVATE_KEY are gone — CdpX402Client provisions and signs with a CDP-managed EVM + Solana wallet using the credentials above.

3. Swap createClient() for CdpX402Client

The tool definition and MCP wiring are unchanged — only the client construction (and its dependencies/env vars) differ.
Passing a payment over the MCP transport instead of paying inline? Every CDP account exposes account.signX402Payment(paymentRequired, acceptedIndex), which signs a payment payload directly with no HTTP round-trip and no /x402 subpath import. Return it in the tool result’s _meta so the caller can settle it:
See x402 in the CDP SDK.
Learn more about what the CDP SDK adds — spend controls, the hosted facilitator, and more — in x402 in the CDP SDK.
For a first-class MCP payment integration, @x402/mcp wraps the MCP client and server directly (wrapMCPClientWithPayment / createPaymentWrapper) instead of wrapping an HTTP client inside a tool. See the CDP SDK examples: a paid MCP server, a minimal paying client, and an LLM-driven chatbot client, all using CdpX402Client for CDP-managed payment.

Next Steps