Skip to main content
Glama
lienhage

Blockchain MCP Server

by lienhage

get-balance

Check the balance of any address on EVM-compatible chains like Ethereum, Polygon, or BSC. Input chain and address details to retrieve real-time or historical wallet balances.

Instructions

Get the balance of an address on any EVM-compatible chain

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
addressYesAddress to check balance for
blockTagNoBlock tag (latest, earliest, pending, or block number)latest
chainYesChain identifier. Available: ethereum, polygon, bsc, arbitrum, optimism, avalanche, fantom, sepolia

Implementation Reference

  • Handler function that retrieves the balance of a given address on the specified EVM chain using the ethers provider.
          async ({ chain, address, blockTag = "latest" }) => {
            try {
              const chainConfig = DEFAULT_CHAINS[chain.toLowerCase()];
              if (!chainConfig) {
                return {
                  content: [{
                    type: "text",
                    text: `Error: Unsupported chain "${chain}". Available chains: ${Object.keys(DEFAULT_CHAINS).join(', ')}`
                  }],
                  isError: true
                };
              }
    
              if (!ethers.isAddress(address)) {
                return {
                  content: [{
                    type: "text",
                    text: `Error: Invalid address: ${address}`
                  }],
                  isError: true
                };
              }
    
              const provider = this.providers.get(chain.toLowerCase());
              if (!provider) {
                return {
                  content: [{
                    type: "text",
                    text: `Error: Provider not initialized for chain: ${chain}`
                  }],
                  isError: true
                };
              }
    
              const balance = await provider.getBalance(address, blockTag);
    
              return {
                content: [{
                  type: "text",
                  text: `Balance Information:
    
    πŸ”— Chain: ${chainConfig.name} (${chainConfig.chainId})
    πŸ‘€ Address: ${address}
    🏷️ Block: ${blockTag}
    
    πŸ’° Balance: ${ethers.formatEther(balance)} ${chainConfig.symbol}
    πŸ”’ Wei: ${balance.toString()}
    
    ${chainConfig.explorerUrl ? `πŸ” Explorer: ${chainConfig.explorerUrl}/address/${address}` : ''}`
                }]
              };
            } catch (error) {
              return {
                content: [{
                  type: "text",
                  text: `Error getting balance: ${error instanceof Error ? error.message : String(error)}`
                }],
                isError: true
              };
            }
          }
  • Input schema definition for the get-balance tool using Zod, specifying chain, address, and optional blockTag.
    {
      title: "Get Balance",
      description: "Get the balance of an address on any EVM-compatible chain",
      inputSchema: {
        chain: z.string().describe(`Chain identifier. Available: ${Object.keys(DEFAULT_CHAINS).join(', ')}`),
        address: z.string().describe("Address to check balance for"),
        blockTag: z.string().optional().describe("Block tag (latest, earliest, pending, or block number)").default("latest")
      }
    },
  • Registration of the 'get-balance' tool with the MCP server, including schema and handler function.
        server.registerTool(
          "get-balance",
          {
            title: "Get Balance",
            description: "Get the balance of an address on any EVM-compatible chain",
            inputSchema: {
              chain: z.string().describe(`Chain identifier. Available: ${Object.keys(DEFAULT_CHAINS).join(', ')}`),
              address: z.string().describe("Address to check balance for"),
              blockTag: z.string().optional().describe("Block tag (latest, earliest, pending, or block number)").default("latest")
            }
          },
          async ({ chain, address, blockTag = "latest" }) => {
            try {
              const chainConfig = DEFAULT_CHAINS[chain.toLowerCase()];
              if (!chainConfig) {
                return {
                  content: [{
                    type: "text",
                    text: `Error: Unsupported chain "${chain}". Available chains: ${Object.keys(DEFAULT_CHAINS).join(', ')}`
                  }],
                  isError: true
                };
              }
    
              if (!ethers.isAddress(address)) {
                return {
                  content: [{
                    type: "text",
                    text: `Error: Invalid address: ${address}`
                  }],
                  isError: true
                };
              }
    
              const provider = this.providers.get(chain.toLowerCase());
              if (!provider) {
                return {
                  content: [{
                    type: "text",
                    text: `Error: Provider not initialized for chain: ${chain}`
                  }],
                  isError: true
                };
              }
    
              const balance = await provider.getBalance(address, blockTag);
    
              return {
                content: [{
                  type: "text",
                  text: `Balance Information:
    
    πŸ”— Chain: ${chainConfig.name} (${chainConfig.chainId})
    πŸ‘€ Address: ${address}
    🏷️ Block: ${blockTag}
    
    πŸ’° Balance: ${ethers.formatEther(balance)} ${chainConfig.symbol}
    πŸ”’ Wei: ${balance.toString()}
    
    ${chainConfig.explorerUrl ? `πŸ” Explorer: ${chainConfig.explorerUrl}/address/${address}` : ''}`
                }]
              };
            } catch (error) {
              return {
                content: [{
                  type: "text",
                  text: `Error getting balance: ${error instanceof Error ? error.message : String(error)}`
                }],
                isError: true
              };
            }
          }
        );
  • Constructor initializes ethers.JsonRpcProvider instances for each supported chain, used by the get-balance handler.
    constructor() {
      // Initialize providers for all default chains
      for (const [key, config] of Object.entries(DEFAULT_CHAINS)) {
        this.providers.set(key, new ethers.JsonRpcProvider(config.rpcUrl));
      }
    }
  • Configuration object defining supported chains with RPC URLs, chain IDs, symbols, and explorer URLs, used to select provider and format responses in get-balance.
    const DEFAULT_CHAINS: Record<string, ChainConfig> = {
      ethereum: {
        name: "Ethereum Mainnet",
        chainId: 1,
        rpcUrl: "https://rpc.ankr.com/eth",
        symbol: "ETH",
        explorerUrl: "https://etherscan.io"
      },
      polygon: {
        name: "Polygon",
        chainId: 137,
        rpcUrl: "https://rpc.ankr.com/polygon",
        symbol: "MATIC",
        explorerUrl: "https://polygonscan.com"
      },
      bsc: {
        name: "BSC",
        chainId: 56,
        rpcUrl: "https://rpc.ankr.com/bsc",
        symbol: "BNB",
        explorerUrl: "https://bscscan.com"
      },
      arbitrum: {
        name: "Arbitrum One",
        chainId: 42161,
        rpcUrl: "https://rpc.ankr.com/arbitrum",
        symbol: "ETH",
        explorerUrl: "https://arbiscan.io"
      },
      optimism: {
        name: "Optimism",
        chainId: 10,
        rpcUrl: "https://rpc.ankr.com/optimism",
        symbol: "ETH",
        explorerUrl: "https://optimistic.etherscan.io"
      },
      avalanche: {
        name: "Avalanche C-Chain",
        chainId: 43114,
        rpcUrl: "https://rpc.ankr.com/avalanche",
        symbol: "AVAX",
        explorerUrl: "https://snowtrace.io"
      },
      fantom: {
        name: "Fantom",
        chainId: 250,
        rpcUrl: "https://rpc.ankr.com/fantom",
        symbol: "FTM",
        explorerUrl: "https://ftmscan.com"
      },
      sepolia: {
        name: "Sepolia Testnet",
        chainId: 11155111,
        rpcUrl: "https://rpc.ankr.com/eth_sepolia",
        symbol: "ETH",
        explorerUrl: "https://sepolia.etherscan.io"
      }
    };
Install Server

Other Tools

Related 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/lienhage/blockchain-mcp'

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