Skip to main content
Glama

get_erc20_balance

Retrieve the ERC20 token balance for a specified Ethereum address by providing the wallet address and token contract address. Works across multiple Ethereum-compatible networks.

Instructions

Get the ERC20 token balance of an Ethereum address

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
addressYesThe Ethereum address to check
networkNoNetwork name or chain ID. Defaults to Ethereum mainnet.
tokenAddressYesThe ERC20 token contract address

Implementation Reference

  • Handler function for the 'get_token_balance' MCP tool that invokes the getERC20Balance service, formats the response, and handles errors.
    async ({ address, tokenAddress, network = "ethereum" }) => {
      try {
        const balance = await services.getERC20Balance(tokenAddress as Address, address as Address, network);
        return {
          content: [{
            type: "text",
            text: JSON.stringify({
              network,
              tokenAddress,
              address,
              balance: {
                raw: balance.raw.toString(),
                formatted: balance.formatted,
                symbol: balance.token.symbol,
                decimals: balance.token.decimals
              }
            }, null, 2)
          }]
        };
      } catch (error) {
        return {
          content: [{ type: "text", text: `Error fetching token balance: ${error instanceof Error ? error.message : String(error)}` }],
          isError: true
        };
      }
    }
  • Zod input schema defining parameters for the ERC20 balance tool: address (wallet/ENS), tokenAddress (ERC20 contract), and optional network.
    inputSchema: {
      address: z.string().describe("The wallet address or ENS name"),
      tokenAddress: z.string().describe("The ERC20 token contract address"),
      network: z.string().optional().describe("Network name or chain ID. Defaults to Ethereum mainnet.")
    },
  • Registration of the MCP tool 'get_token_balance' (functionally equivalent to get_erc20_balance) including description, schema, annotations, and handler reference.
    server.registerTool(
      "get_token_balance",
      {
        description: "Get the ERC20 token balance for an address",
        inputSchema: {
          address: z.string().describe("The wallet address or ENS name"),
          tokenAddress: z.string().describe("The ERC20 token contract address"),
          network: z.string().optional().describe("Network name or chain ID. Defaults to Ethereum mainnet.")
        },
        annotations: {
          title: "Get ERC20 Token Balance",
          readOnlyHint: true,
          destructiveHint: false,
          idempotentHint: true,
          openWorldHint: true
        }
      },
      async ({ address, tokenAddress, network = "ethereum" }) => {
        try {
          const balance = await services.getERC20Balance(tokenAddress as Address, address as Address, network);
          return {
            content: [{
              type: "text",
              text: JSON.stringify({
                network,
                tokenAddress,
                address,
                balance: {
                  raw: balance.raw.toString(),
                  formatted: balance.formatted,
                  symbol: balance.token.symbol,
                  decimals: balance.token.decimals
                }
              }, null, 2)
            }]
          };
        } catch (error) {
          return {
            content: [{ type: "text", text: `Error fetching token balance: ${error instanceof Error ? error.message : String(error)}` }],
            isError: true
          };
        }
      }
    );
  • Core helper function getERC20Balance that resolves ENS names, fetches balance, symbol, and decimals from the ERC20 contract using viem, and formats the balance.
    export async function getERC20Balance(
      tokenAddressOrEns: string,
      ownerAddressOrEns: string,
      network = 'ethereum'
    ): Promise<{
      raw: bigint;
      formatted: string;
      token: {
        symbol: string;
        decimals: number;
      }
    }> {
      // Resolve ENS names to addresses if needed
      const tokenAddress = await resolveAddress(tokenAddressOrEns, network);
      const ownerAddress = await resolveAddress(ownerAddressOrEns, network);
      
      const publicClient = getPublicClient(network);
    
      const contract = getContract({
        address: tokenAddress,
        abi: erc20Abi,
        client: publicClient,
      });
    
      const [balance, symbol, decimals] = await Promise.all([
        contract.read.balanceOf([ownerAddress]),
        contract.read.symbol(),
        contract.read.decimals()
      ]);
    
      return {
        raw: balance,
        formatted: formatUnits(balance, decimals),
        token: {
          symbol,
          decimals
        }
      };
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the action but lacks details on permissions, rate limits, network behavior, or response format. For a read operation with no annotation coverage, this is a significant gap in transparency.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's purpose without unnecessary words. It's front-loaded and appropriately sized, making it easy to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of blockchain operations and lack of annotations or output schema, the description is incomplete. It doesn't cover behavioral aspects like error handling, network defaults, or return values, which are crucial for an agent to use this tool effectively in context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, with clear descriptions for all parameters in the input schema. The description adds no additional semantic context beyond what's in the schema, such as format examples or constraints. Baseline 3 is appropriate as the schema handles the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb 'Get' and the resource 'ERC20 token balance of an Ethereum address', making the purpose specific and understandable. However, it doesn't explicitly distinguish this tool from similar siblings like 'get_token_balance' or 'get_token_balance_erc20', which likely serve overlapping functions, leaving some ambiguity about differentiation.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. With siblings like 'get_token_balance' and 'get_token_balance_erc20' present, there's no indication of differences in scope, token types, or use cases, leaving the agent without explicit or implied context for selection.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

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/mcpdotdirect/evm-mcp-server'

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