..
2026-05-17 20:24:58 +03:00
2026-05-17 20:24:58 +03:00
2026-05-17 20:24:58 +03:00
2026-05-17 20:24:58 +03:00
2026-05-17 20:24:58 +03:00
2026-05-17 20:24:58 +03:00
mcp
2026-05-17 20:24:58 +03:00
2026-05-17 20:24:58 +03:00
2026-05-17 20:24:58 +03:00

MCP Client with x402 Payment Support

This example demonstrates how to create an MCP (Model Context Protocol) client that can automatically pay for tool calls using the x402 payment protocol.

Overview

The client connects to an x402-enabled MCP server and:

  1. Discovers available tools
  2. Calls a free tool (ping)
  3. Calls a paid tool (get_weather) with automatic payment
  4. Shows how to check payment requirements before calling

Setup

  1. Copy .env-example to .env and fill in your values:
cp .env-example .env
  1. Configure environment variables:

    • EVM_PRIVATE_KEY: Your EVM wallet private key (must have testnet funds)
    • MCP_SERVER_URL: MCP server URL (default: http://localhost:4022)
  2. Install dependencies:

go mod download

Running

First, start the MCP server:

cd ../../servers/mcp
go run . simple

Then run the client:

Uses the CreateX402MCPClient factory function for easy setup:

go run . simple

Advanced Mode

Uses X402MCPClient with manual setup for full control:

go run . advanced

Advanced mode demonstrates:

  • Manual MCP client and x402Client creation
  • Client-side hooks (OnPaymentRequired, OnBeforePayment, OnAfterPayment)
  • Accessing underlying client instances

Expected Output

πŸ”Œ Connecting to MCP server at: http://localhost:4022
πŸ’³ Using wallet: 0x...
βœ… Connected to MCP server

πŸ“‹ Discovering available tools...
Available tools:
   - get_weather: Get current weather for a city. Requires payment of $0.001.
   - ping: A free tool that returns pong

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
πŸ†“ Test 1: Calling free tool (ping)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Response: pong
Payment made: false

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
πŸ’° Test 2: Calling paid tool (get_weather)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

πŸ’° Payment required for tool: get_weather
   Amount: 1000 (0x036CbD53842c5426634e7929541eC2318f3dCF7e)
   Network: eip155:84532
   Approving payment...

Response: {
  "city": "San Francisco",
  "weather": "sunny",
  "temperature": 65
}
Payment made: true

πŸ“¦ Payment Receipt:
   Success: true
   Transaction: 0x...

βœ… Demo complete!

Payment Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  MCP Client  β”‚                    β”‚  MCP Server  β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜                    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚                                   β”‚
       β”‚  1. callTool("get_weather")       β”‚
       │──────────────────────────────────▢│
       β”‚                                   β”‚
       β”‚  2. 402 PaymentRequired           β”‚
       │◀──────────────────────────────────│
       β”‚                                   β”‚
       β”‚  3. createPaymentPayload()        β”‚
       β”‚  (signs transaction)              β”‚
       β”‚                                   β”‚
       β”‚  4. callTool + PaymentPayload     β”‚
       │──────────────────────────────────▢│
       β”‚                                   β”‚
       β”‚                    5. verify()    β”‚
       β”‚                    6. execute()   β”‚
       β”‚                    7. settle()    β”‚
       β”‚                                   β”‚
       β”‚  8. Result + SettleResponse       β”‚
       │◀──────────────────────────────────│
       β”‚                                   β”‚

Configuration Options

X402MCPClientOptions

x402Mcp := mcp.NewX402MCPClient(session, paymentClient, mcp.Options{
    // Enable automatic payment (default: true)
    AutoPayment: mcp.BoolPtr(true),

    // Custom approval logic (optional)
    OnPaymentRequested: func(context mcp.PaymentRequiredContext) (bool, error) {
        fmt.Printf("Pay %s?\n", context.PaymentRequired.Accepts[0].Amount)
        return true, nil // or false to deny
    },
})

Hooks

// Called before payment is made
x402Mcp.OnBeforePayment(func(context mcp.PaymentRequiredContext) error {
    fmt.Println("About to pay for:", context.ToolName)
    return nil
})

// Called after payment settles
x402Mcp.OnAfterPayment(func(context mcp.AfterPaymentContext) error {
    fmt.Println("Payment settled:", context.SettleResponse.Transaction)
    return nil
})