Skip to main content

Overview

This example demonstrates how to use the Model Context Protocol (MCP) with x402 payments to enable AI agents to make paid API calls to Snack Money.

Features

  • JSON-RPC 2.0 protocol implementation
  • x402 payment integration
  • Support for Base (EVM) and Solana networks
  • Tool discovery and invocation
  • Automatic payment handling

Installation

Clone the example repository:

Configuration

Create a .env file with your credentials:

Usage

List Available Tools

Discover all available MCP tools:
Output:

Send Payment via Base Network

This will:
  1. Call the MCP endpoint to send payment
  2. Receive payment requirements (error -32402)
  3. Sign the payment with x402 EVM client
  4. Retry with signed payment
  5. Display transaction receipt

Send Payment via Solana Network

Similar flow to Base, but using Solana blockchain.

Code Example

Here’s a simplified example of making an MCP call with payment:

Protocol Flow

The MCP payment flow follows this sequence:
  1. Initial Call - Client calls MCP tool without payment
  2. Payment Required - Server returns error -32402 with:
    • taskId - Unique identifier for this task
    • paymentRequired - x402 payment requirements
  3. Sign Payment - Client signs payment using x402 client
  4. Retry with Payment - Client retries with:
    • Original parameters
    • _meta.taskId - Task identifier
    • _meta.x402Payment - Signed payment proof
  5. Success - Server returns result with receipt

Error Handling

Common error codes:

Supported Networks

  • Base (Chain ID: 8453) - USDC on Base mainnet
  • Base Sepolia (Chain ID: 84532) - USDC on Base testnet
  • Solana - USDC on Solana mainnet
  • Solana Devnet - USDC on Solana devnet

Integration with AI Agents

This MCP implementation is compatible with:

Full Example

View the complete working example: GitHub: snack-money-examples/mcp