Skip to main content

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

Policy Checks Before Payment

This example auto-approves payments for local testing. In production, the MCP bridge should check the payment requirements before creating or signing a payment payload. The client hooks are the right place to enforce those rules. At minimum, compare the selected requirement against your expected tool call:
  • maximum amount and currency
  • expected network and scheme
  • expected resource server or facilitator
  • tool name and user-requested operation
  • per-user, per-agent, or per-session spend limits
Pass policies directly to createx402MCPClient to filter payment requirements before any payment is created. Policies run after built-in spendControls and before the wallet signs. By default the underlying x402Client only allows recognized default assets and applies a $1 USD spend cap unless you override spendControls (raise/disable maxAmountPerPayment, opt into other tokens via allowedAssets, or set spendControls: false):

Capping tool-call timeouts

By default, the x402 MCP client caps derived tool-call timeouts at 10 minutes (600 seconds). The cap applies to both the initial probe call and the paid retry. You can lower or raise this limit using maxRequestTimeoutSeconds (TypeScript/Python) or MaxRequestTimeout (Go):
Per-call overrides are also supported via callTool’s options.timeout (milliseconds), which takes precedence over the cap.
The timeout is derived from the payment requirement’s maxTimeoutSeconds field, capped by maxRequestTimeoutSeconds. If the accept’s maxTimeoutSeconds exceeds the cap, the cap wins.

Using the onPaymentRequested hook

For per-call logic (e.g. checking tool name or prompting the user), use onPaymentRequested:
If a payment requirement does not match the intended tool call, deny it before the wallet signs. Avoid relying only on model prompts for spend controls; keep the policy check in code near the payment client.

Multi-Network Support

The example supports both EVM (Base, 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 registered EVM schemes (exact, upto, batch-settlement, etc.)
  • solana:* networks use the SVM scheme
Batch settlement: Paid APIs that advertise scheme: "batch-settlement" require BatchSettlementEvmScheme on eip155:* (in addition to ExactEvmScheme). The Implementation section and the snippet above register both so tools work against exact servers and batch-settlement APIs. See Batch settlement.

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:

Architecture Diagram


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.


Making Your MCP Tools Discoverable via Bazaar

If you are building an MCP server (not just a client bridge), you can make your paid tools visible in the x402 Bazaar so AI agents and other buyers can discover them without prior knowledge of your server. Pass extensions in the payment wrapper config with the Bazaar discovery metadata:
When a client pays for the tool, the facilitator extracts the Bazaar extension from the payment payload and indexes the tool in /discovery/resources with type: "mcp". Buyers can then discover it by querying a Bazaar-enabled facilitator:
See the full Bazaar documentation for details on buyers querying and calling discovered MCP tools.

Next Steps