Skip to main content
Glama
0xKoda
by 0xKoda

eth_getBalance

Retrieve the current or historical Ether balance of any Ethereum wallet address to verify holdings or track transactions.

Instructions

Retrieves the balance of a given Ethereum address

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
addressYesThe Ethereum address to check balance
blockParameterNoBlock parameter (default: "latest")latest

Implementation Reference

  • The handler function that performs the eth_getBalance RPC call via makeRpcCall, converts the balance from wei to ETH, and returns a formatted text response.
    async (args) => {
      try {
        console.error(`Getting balance for address: ${args.address} at block: ${args.blockParameter}`);
        
        const balance = await makeRpcCall('eth_getBalance', [args.address, args.blockParameter]);
        // Convert hex balance to decimal and then to ETH for readability
        const balanceWei = parseInt(balance, 16);
        const balanceEth = balanceWei / 1e18;
        
        return {
          content: [{ 
            type: "text", 
            text: `Balance for ${args.address}:\n${balanceWei} Wei\n${balanceEth.toFixed(6)} ETH` 
          }]
        };
      } catch (error) {
        return {
          content: [{ type: "text", text: `Error: Failed to get balance. ${error.message}` }],
          isError: true
        };
      }
    }
  • Zod input schema defining the parameters for the eth_getBalance tool: address (required, Ethereum address format) and blockParameter (optional, defaults to 'latest').
    {
      address: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe('The Ethereum address to check balance'),
      blockParameter: z.string().default('latest').describe('Block parameter (default: "latest")')
    },
  • index.js:165-194 (registration)
    MCP server.tool registration for the eth_getBalance tool, including name, description, input schema, and handler function.
    server.tool(
      'eth_getBalance',
      'Retrieves the balance of a given Ethereum address',
      {
        address: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe('The Ethereum address to check balance'),
        blockParameter: z.string().default('latest').describe('Block parameter (default: "latest")')
      },
      async (args) => {
        try {
          console.error(`Getting balance for address: ${args.address} at block: ${args.blockParameter}`);
          
          const balance = await makeRpcCall('eth_getBalance', [args.address, args.blockParameter]);
          // Convert hex balance to decimal and then to ETH for readability
          const balanceWei = parseInt(balance, 16);
          const balanceEth = balanceWei / 1e18;
          
          return {
            content: [{ 
              type: "text", 
              text: `Balance for ${args.address}:\n${balanceWei} Wei\n${balanceEth.toFixed(6)} ETH` 
            }]
          };
        } catch (error) {
          return {
            content: [{ type: "text", text: `Error: Failed to get balance. ${error.message}` }],
            isError: true
          };
        }
      }
    );
  • Helper function used by eth_getBalance (and other tools) to make POST requests to the Ethereum RPC endpoint and handle responses/errors.
    async function makeRpcCall(method, params = []) {
      try {
        const response = await axios.post(ETH_RPC_URL, {
          jsonrpc: '2.0',
          id: 1,
          method,
          params
        });
    
        if (response.data.error) {
          throw new Error(`RPC Error: ${response.data.error.message}`);
        }
    
        return response.data.result;
      } catch (error) {
        console.error(`Error making RPC call to ${method}:`, error.message);
        throw error;
      }
    }
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/0xKoda/eth-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server