Skip to main content

MCP tools for AI agents — HTTP requests with automatic x402 micropayments

Project description

x402-mcp

PyPI CI Python 3.11+ License: MIT

Give your AI agent a wallet. It pays for APIs automatically.

x402-mcp is an MCP server that adds HTTP request tools with automatic x402 micropayments to any AI agent. When a request hits a 402-gated endpoint, payment happens automatically — no API keys, no accounts, no billing dashboards.

Works with any x402-enabled service. Not locked to a single provider.

How it works

Agent calls x402_fetch("https://some-api.com/data")
    → Server responds 402 Payment Required
    → x402-mcp returns payment details (amount, network, recipient)
    → Agent shows details to user, user approves
    → Agent calls x402_fetch(url, confirm_payment=True)
    → x402-mcp signs a gasless EIP-712 payment authorization
    → Agent gets the response ✓

By default, your agent confirms payments with the user before paying. Set X402_CONFIRM_PAYMENTS=false to auto-pay without confirmation (original behavior).

Quick start

# Install and run the setup wizard
uvx x402-mcp-setup

The wizard will:

  1. Generate a fresh EVM wallet (or use your existing key with --key)
  2. Show a QR code for your wallet address
  3. Print funding instructions (testnet USDC is free)
  4. Output the MCP config JSON, ready to paste

Add to your MCP client

Paste the config into ~/.claude/mcp.json (Claude Code) or your Cursor MCP settings:

{
  "mcpServers": {
    "x402-mcp": {
      "command": "uvx",
      "args": ["x402-mcp"],
      "env": {
        "EVM_PRIVATE_KEY": "0xYourPrivateKey",
        "X402_CONFIRM_PAYMENTS": "true"
      }
    }
  }
}

Your agent now has two tools:

Tool Description
x402_fetch HTTP requests to any URL — shows payment details for confirmation, then pays on approval
wallet_info Returns your wallet's public address for funding/verification

Usage examples

Fetch from an x402-gated API (with payment confirmation):

# Step 1: Agent makes request, gets payment details
x402_fetch(url="https://api.example.com/data")
# → Returns: {payment_required: True, human_readable: {amount: "1000000", ...}}

# Step 2: After user approves, agent confirms payment
x402_fetch(url="https://api.example.com/data", confirm_payment=True)
# → Returns: {status: 200, body: "..."}

POST with JSON body:

x402_fetch(
    url="https://api.example.com/submit",
    method="POST",
    headers={"Content-Type": "application/json"},
    body='{"query": "hello"}'
)

Check your wallet address:

wallet_info()

Architecture

src/x402_mcp/
├── mcp_server.py   # FastMCP server — x402_fetch + wallet_info tools
└── setup.py        # Wallet generation wizard, prints MCP config JSON

Two entry points:

  • x402-mcp — MCP stdio server (what your agent connects to)
  • x402-mcp-setup — Interactive setup wizard

The server lazy-initializes an x402HttpxClient on first tool call. Payments are gasless EIP-712 signed authorizations — no on-chain transaction per request. Binary responses (images, etc.) are returned as base64-encoded data.

Development

uv sync --extra dev    # Install deps
uv run ruff check src/ # Lint
uv run pytest tests/ -v # Test

E2E test against a real x402 endpoint:

EVM_PRIVATE_KEY=0x... X402_TEST_URL=https://some-x402-api.com/endpoint \
  uv run python scripts/test_e2e.py

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

x402_mcp-0.3.2.tar.gz (180.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

x402_mcp-0.3.2-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file x402_mcp-0.3.2.tar.gz.

File metadata

  • Download URL: x402_mcp-0.3.2.tar.gz
  • Upload date:
  • Size: 180.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for x402_mcp-0.3.2.tar.gz
Algorithm Hash digest
SHA256 d3ca708253aacae229eb174ffbb8e6b12137d8eb8c74a31cd50cf03c0cb1bdf4
MD5 4195eee09ae25619d851a1376865ee61
BLAKE2b-256 4844e06f072be2e27b14f3be9a02b4c9e495c43b505ff7a0f3a9abd67d663ce7

See more details on using hashes here.

Provenance

The following attestation bundles were made for x402_mcp-0.3.2.tar.gz:

Publisher: ci.yml on dsr-restyn/x402-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file x402_mcp-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: x402_mcp-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for x402_mcp-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f71ac3d681bfc53cc4579a08b72e791992671a5e8f3627bd3eddb4a26e6e8d47
MD5 5f9458ef1a3991b9bee440b97a28df0b
BLAKE2b-256 fa20e09f98c1d2c55c231c8db2cde6b9a4852f64891b0b2783d87f884326a1da

See more details on using hashes here.

Provenance

The following attestation bundles were made for x402_mcp-0.3.2-py3-none-any.whl:

Publisher: ci.yml on dsr-restyn/x402-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page