import httpx
import logging
import sys
from mcp_app import mcp
logging.basicConfig(
stream=sys.stderr, level=logging.INFO, format="[LOG] %(message)s", force=True
)
CHAINS = {
"Ethereum": "https://eth.llamarpc.com",
"Arbitrum": "https://arb1.arbitrum.io/rpc",
"Optimism": "https://mainnet.optimism.io",
"Polygon": "https://polygon-rpc.com",
"Base": "https://mainnet.base.org",
}
@mcp.tool()
async def get_gas_prices() -> dict:
"""
Check the current gas fees (in Gwei) across multiple blockchains.
Low (<10) is cheap. High (>50) is expensive.
"""
results = {}
async with httpx.AsyncClient() as client:
for name, url in CHAINS.items():
try:
# Standard JSON-RPC call for gas price
payload = {
"jsonrpc": "2.0",
"method": "eth_gasPrice",
"params": [],
"id": 1,
}
response = await client.post(url, json=payload, timeout=5.0)
data = response.json()
if "result" in data:
# Convert Hex -> Integer -> Gwei (Divide by 10^9)
wei = int(data["result"], 16)
gwei = wei / 10**9
status = "🟢" # Cheap
if gwei > 20:
status = "🟡" # Normal
if gwei > 50:
status = "🔴" # Expensive
results[name] = f"{status} {gwei:.2f} Gwei"
else:
results[name] = "Error"
except Exception:
results[name] = "Offline"
return results