get_eth_balance
dapp/app/lib/mcp/tools/eth-balance.ts provides a raw JSON-schema tool (no zod) that fetches native balances on any supported chain.
Implementation Highlights
- Validation — Ensures the
addressmatches0x…40andchainis one of the enumerated values (mainnet,sepolia,polygon, etc.). - RPC selection —
getRpcUrlpulls frompublic.env.ts/Alchemy for public chains and the local config forritonet. - Result shape — Emits both a helpful sentence and a JSON blob containing wei/ETH values, chain metadata, and the detected symbol.
// dapp/app/lib/mcp/tools/eth-balance.ts
const ethBalanceTool: Tool<EthBalanceParams> = {
name: 'get_eth_balance',
description: 'Get the native token balance of an address on various EVM chains.',
inputSchema: EthBalanceInputSchema,
async handler({ address, chain = 'mainnet' }) {
if (!isValidAddress(address)) fail('Invalid Ethereum address format');
if (!isSupportedChain(chain)) fail(`Unsupported chain: ${chain}`);
const rpcUrl = getRpcUrl(chain);
const balanceWei = await getBalance(rpcUrl, address);
const balanceEth = formatEther(balanceWei);
const text = textResult(`Address ${address} on ${formatChainName(chain)} has a balance of ${balanceEth} ${getTokenSymbol(chain)}`);
const json = jsonResult({ address, chain, chainName: formatChainName(chain), balanceWei: balanceWei.toString(), balanceEth, symbol: getTokenSymbol(chain) });
return { content: [...text.content, ...json.content] };
},
};Presenter Integration
dapp/components/chatBot/ToolActivity/catalog/presenters/eth_balance.presenter.ts keeps chips terse:
| Status | Label | Logic |
|---|---|---|
| pending | Fetching Balance | Shows no body text so the chip stays compact. |
| success | Fetched Balance. | Displays X ETH on Chain, pulling the symbol from the JSON payload, falling back to the text stream if needed. |
| error | Failed to Fetch Balance. | Special-cases wallet errors (“Your wallet must be connected…”) via regex. |
Notes
get_eth_balance is available to both rapBattle and freestyle modes. Only rap battles impose the “use once per battle” restriction (rapBattleConfig enforces it in the system instructions) while freestyle sessions can call it whenever the runtime allows.