Skip to main content
Glama

get_token_balance_erc20

Retrieve the ERC20 token balance for a specific address on Ethereum or compatible networks using the token contract and network details.

Instructions

Get ERC20 token balance for an address

Input Schema

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

Implementation Reference

  • Registers the MCP tool 'get_token_balance' for retrieving ERC20 token balances of an address on EVM networks.
    "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
        };
      }
    }
  • The handler function executes the tool logic by calling services.getERC20Balance and formatting the response.
    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-based input schema and annotations defining parameters: address, tokenAddress, network (optional).
    {
      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
      }
  • Core helper function that fetches ERC20 token balance using viem contract reads, including symbol and decimals.
    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
        }
      };
    }
  • Minimal ERC20 ABI used for reading balanceOf, symbol, and decimals in getERC20Balance.
    // Standard ERC20 ABI (minimal for reading)
    const erc20Abi = [
      {
        inputs: [],
        name: 'symbol',
        outputs: [{ type: 'string' }],
        stateMutability: 'view',
        type: 'function'
      },
      {
        inputs: [],
        name: 'decimals',
        outputs: [{ type: 'uint8' }],
        stateMutability: 'view',
        type: 'function'
      },
      {
        inputs: [{ type: 'address', name: 'account' }],
        name: 'balanceOf',
        outputs: [{ type: 'uint256' }],
        stateMutability: 'view',
        type: 'function'
      }
    ] as const;
Behavior2/5

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

With no annotations provided, the description carries full burden for behavioral disclosure. While 'Get' implies a read-only operation, it doesn't specify whether this requires network access, has rate limits, returns cached data, or what format the balance comes in (e.g., raw wei vs formatted). For a blockchain query tool, this leaves significant behavioral questions unanswered.

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 communicates the core purpose without unnecessary words. It's appropriately sized for a straightforward query tool and gets directly to the point.

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

Completeness3/5

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

For a read-only query tool with good schema coverage but no output schema, the description is minimally adequate. It states what the tool does but lacks important context about return format, network dependencies, and differentiation from similar sibling tools. The absence of annotations means the description should do more to explain behavioral characteristics.

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%, so all parameters are documented in the schema. The description doesn't add any parameter-specific information beyond what's in the schema descriptions. The baseline score of 3 reflects adequate coverage through the schema alone.

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 resource ('ERC20 token balance for an address'), making the purpose immediately understandable. It distinguishes from generic 'get_balance' and 'get_token_balance' siblings by specifying ERC20 tokens, though it doesn't explicitly differentiate from the similar 'get_erc20_balance' sibling tool.

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 like 'get_erc20_balance' (which appears to serve a similar function) or 'get_token_balance' (which might handle different token types). There's no mention of prerequisites, error conditions, or comparison with sibling tools.

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