getEthBalance
Retrieve the Ethereum balance of any wallet address using this tool. Simplify blockchain analysis by querying on-chain data for accurate wallet insights.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| address | Yes |
Implementation Reference
- tools/balance.js:13-26 (handler)The handler function that implements the core logic of the getEthBalance tool: retrieves the ETH balance for the given address using Web3, converts from Wei to Ether, and returns a formatted text response or error message.async ({ address }) => { try { const balanceWei = await web3.eth.getBalance(address); const balanceEth = web3.utils.fromWei(balanceWei, 'ether'); return { content: [{ type: "text", text: `Balance for ${address}: ${balanceEth} ETH` }] }; } catch (error) { return { content: [{ type: "text", text: `Error fetching balance: ${error.message}` }] }; } } );
- tools/balance.js:12-12 (schema)Zod schema defining the input parameter 'address' with validation for valid Ethereum address format.{ address: z.string().regex(/^0x[a-fA-F0-9]{40}$/, "Invalid Ethereum address") },
- tools/balance.js:11-26 (registration)Direct registration of the getEthBalance tool on the MCP server using server.tool, specifying name, input schema, and inline handler function.server.tool("getEthBalance", { address: z.string().regex(/^0x[a-fA-F0-9]{40}$/, "Invalid Ethereum address") }, async ({ address }) => { try { const balanceWei = await web3.eth.getBalance(address); const balanceEth = web3.utils.fromWei(balanceWei, 'ether'); return { content: [{ type: "text", text: `Balance for ${address}: ${balanceEth} ETH` }] }; } catch (error) { return { content: [{ type: "text", text: `Error fetching balance: ${error.message}` }] }; } } );
- main.js:60-60 (registration)High-level registration invocation in the main server setup that calls registerBalanceTools to add the getEthBalance tool (and transaction count tool) to the MCP server.registerBalanceTools(server, web3);
- tools/balance.js:9-44 (helper)Helper function that encapsulates the registration of balance-related tools, including getEthBalance, providing modularity for tool setup.export function registerBalanceTools(server, web3) { // Add ETH balance tool server.tool("getEthBalance", { address: z.string().regex(/^0x[a-fA-F0-9]{40}$/, "Invalid Ethereum address") }, async ({ address }) => { try { const balanceWei = await web3.eth.getBalance(address); const balanceEth = web3.utils.fromWei(balanceWei, 'ether'); return { content: [{ type: "text", text: `Balance for ${address}: ${balanceEth} ETH` }] }; } catch (error) { return { content: [{ type: "text", text: `Error fetching balance: ${error.message}` }] }; } } ); // Add transaction count (nonce) tool server.tool("getTransactionCount", { address: z.string().regex(/^0x[a-fA-F0-9]{40}$/, "Invalid Ethereum address") }, async ({ address }) => { try { const transactionCount = await web3.eth.getTransactionCount(address); return { content: [{ type: "text", text: `Transaction count (nonce) for ${address}: ${transactionCount}` }] }; } catch (error) { return { content: [{ type: "text", text: `Error fetching transaction count: ${error.message}` }] }; } } ); }