import httpx
import sys
import logging
from mcp_app import mcp
logging.basicConfig(
stream=sys.stderr, level=logging.INFO, format="[LOG] %(message)s", force=True
)
# A list of free, public RPC endpoints for different chains
CHAINS = {
"Ethereum": {"url": "https://eth.llamarpc.com", "symbol": "ETH"},
"Arbitrum": {"url": "https://arb1.arbitrum.io/rpc", "symbol": "ETH"},
"Optimism": {"url": "https://mainnet.optimism.io", "symbol": "ETH"},
"Polygon": {"url": "https://polygon-rpc.com", "symbol": "MATIC"},
"Base": {"url": "https://mainnet.base.org", "symbol": "ETH"},
}
@mcp.tool()
async def wallet_check(address: str) -> dict:
"""
Check the native token balance of a wallet across multiple blockchains (ETH, Arb, Op, Poly, Base).
Args:
address: The wallet address (must start with 0x)
"""
if not address or len(address) != 42 or not isinstance(address, str):
return "Error: invalid wallet address"
logging.info(f"Checking balances for {address} across {len(CHAINS)} chains...")
results = {}
async with httpx.AsyncClient() as client:
for chain_name, info in CHAINS.items():
try:
payload = {
"jsonrpc": "2.0",
"method": "eth_getBalance",
"params": [address, "latest"],
"id": 1,
}
response = await client.post(info["url"], json=payload, timeout=5.0)
data = response.json()
if "result" in data:
wei_balance = int(data["result"], 16)
eth_balance = wei_balance / 10**18
results[chain_name] = f"{eth_balance:.4f} {info['symbol']}"
else:
results[chain_name] = "Error: No data"
except Exception as e:
results[chain_name] = "Offline"
return results