Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}