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:
- Discovers available tools
- Calls a free tool (ping)
- Calls a paid tool (get_weather) with automatic payment
- Shows how to check payment requirements before calling
Setup
- Copy
.env-exampleto.envand fill in your values:
cp .env-example .env
-
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)
-
Install dependencies:
go mod download
Running
First, start the MCP server:
cd ../../servers/mcp
go run . simple
Then run the client:
Simple Mode (Recommended)
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
})