From 746fa0631c4faf2ad83063411dde1b25ffff3023 Mon Sep 17 00:00:00 2001 From: ak10082247-max Date: Mon, 5 Oct 2026 18:35:23 +0100 Subject: [PATCH] Add x402 agent monetization tutorial --- .../x402_agent_monetization.ipynb | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 python/samples/agentchat_x402_monetization/x402_agent_monetization.ipynb diff --git a/python/samples/agentchat_x402_monetization/x402_agent_monetization.ipynb b/python/samples/agentchat_x402_monetization/x402_agent_monetization.ipynb new file mode 100644 index 000000000000..a90f56600ae0 --- /dev/null +++ b/python/samples/agentchat_x402_monetization/x402_agent_monetization.ipynb @@ -0,0 +1,84 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Monetizing AI Agents with the x402 Protocol (Base Network)\n", + "\n", + "In this tutorial, we will learn how to give our autonomous AI agent a crypto wallet and let it pay for its own tools and data using the **x402 protocol** on the Base network.\n", + "\n", + "We will connect to the `x402-agent-tollbooth` MCP server, which requires agents to pay USDC per-call to execute smart contract audits and token analytics.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "source": [ + "# 1. Install prerequisites\n", + "!pip install mcp langchain-anthropic web3" + ], + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Setting up the Agent Wallet\n", + "Your agent needs a funded wallet on Base to execute micro-transactions." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "source": [ + "from web3 import Web3\n", + "import os\n", + "\n", + "# Connect to Base\n", + "w3 = Web3(Web3.HTTPProvider('https://mainnet.base.org'))\n", + "\n", + "# Load your agent's private key\n", + "AGENT_PRIVATE_KEY = os.getenv('AGENT_PRIVATE_KEY', '0x...')\n", + "if AGENT_PRIVATE_KEY != '0x...':\n", + " account = w3.eth.account.from_key(AGENT_PRIVATE_KEY)\n", + " print(f'Agent Wallet Address: {account.address}')\n" + ], + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Connecting the MCP Tollbooth\n", + "We configure LangChain (or your framework of choice) to load tools from the remote MCP server." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "source": [ + "# Pseudo-code for connecting to the Tollbooth MCP Server\n", + "# The agent will automatically call this when it needs to audit a contract.\n", + "TOLLBOOTH_MCP_URL = \"https://glama.ai/mcp/servers/ak10082247-max/x402-agent-tollbooth\"\n", + "\n", + "print(\"MCP Tollbooth connected. Available tools: ['contract.audit', 'wallet.profile', 'token.analyze']\")\n" + ], + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Execution\n", + "When the agent decides to use the `contract.audit` tool, it must first execute a transaction on Base to pay the 1.00 USDC toll, and pass the `paymentHash` into the tool call." + ] + } + ], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +}