Skip to main content
Glama

get_token_balance

Retrieve the token balance and USD value for a wallet address using a specified token contract, powered by SailFish as the price oracle.

Instructions

Get the token balance of a wallet address with USD value using SailFish as price oracle

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tokenAddressYesToken contract address
walletAddressYesWallet address to check

Implementation Reference

  • Core implementation of the get_token_balance tool: queries ERC20 contract for balance, decimals, symbol, name; formats balance; fetches optional USD value from SailFish subgraph.
    export async function getTokenBalance(
      tokenAddress: string, 
      walletAddress: string
    ): Promise<{ 
      balance: string, 
      decimals: number, 
      symbol: string, 
      name: string, 
      formattedBalance: string,
      usdValue?: string
    }> {
      try {
        const provider = getProvider();
        const tokenContract = new ethers.Contract(tokenAddress, ERC20_ABI, provider);
        
        // Get token details
        const [balance, decimals, symbol, name] = await Promise.all([
          tokenContract.balanceOf(walletAddress),
          tokenContract.decimals(),
          tokenContract.symbol(),
          tokenContract.name()
        ]);
        
        // Convert BigInt to string and number
        const balanceStr = bigIntToString(balance);
        const decimalsNum = Number(decimals);
        
        const formattedBalance = ethers.formatUnits(balance, decimalsNum);
        
        // Try to get USD value from SailFish
        let usdValue: string | undefined;
        try {
          const tokenPrice = await subgraph.getTokenPrice(tokenAddress);
          if (tokenPrice) {
            const valueInUsd = parseFloat(formattedBalance) * parseFloat(tokenPrice);
            usdValue = valueInUsd.toString();
          }
        } catch (error) {
          console.error('Error fetching token price:', error);
          // Continue without USD value
        }
        
        return {
          balance: balanceStr,
          decimals: decimalsNum,
          symbol: String(symbol),
          name: String(name),
          formattedBalance,
          usdValue
        };
      } catch (error) {
        console.error('Error fetching token balance:', error);
        throw error;
      }
    }
  • src/index.ts:886-905 (registration)
    MCP tool dispatch/registration: handles CallToolRequest for 'get_token_balance' by validating params and calling blockchain.getTokenBalance, returning JSON result.
    case 'get_token_balance': {
      if (!args.tokenAddress || typeof args.tokenAddress !== 'string') {
        throw new McpError(ErrorCode.InvalidParams, 'Token address is required');
      }
      
      if (!args.walletAddress || typeof args.walletAddress !== 'string') {
        throw new McpError(ErrorCode.InvalidParams, 'Wallet address is required');
      }
      
      const balance = await blockchain.getTokenBalance(args.tokenAddress, args.walletAddress);
      
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(balance, null, 2),
          },
        ],
      };
    }
  • Input schema definition and tool registration in ListToolsRequest handler for 'get_token_balance'.
      name: 'get_token_balance',
      description: 'Get the token balance of a wallet address with USD value using SailFish as price oracle',
      inputSchema: {
        type: 'object',
        properties: {
          tokenAddress: {
            type: 'string',
            description: 'Token contract address',
          },
          walletAddress: {
            type: 'string',
            description: 'Wallet address to check',
          },
        },
        required: ['tokenAddress', 'walletAddress'],
      },
    },
  • src/index.ts:16-17 (registration)
    Import of the blockchain module containing the getTokenBalance handler.
    import * as blockchain from './blockchain.js';
    import * as swap from './swap.js';
Behavior2/5

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

No annotations are provided, so the description carries full burden. It mentions the price oracle (SailFish) which adds useful context, but doesn't disclose other behavioral traits like whether this is a read-only operation (implied by 'Get'), error conditions, rate limits, authentication needs, or response format. The description is minimal beyond the core functionality.

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 front-loads the core purpose and includes key scope details (USD value, price oracle). Every word earns its place with zero waste or redundancy.

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?

Given 2 parameters with full schema coverage and no output schema, the description covers the basic purpose and price oracle context adequately. However, as a financial tool with no annotations, it should ideally mention more about error handling, response format, or limitations to be fully complete for agent use.

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 the schema already documents both parameters (tokenAddress, walletAddress) adequately. The description doesn't add any parameter-specific details beyond what the schema provides, such as format examples or constraints. Baseline 3 is appropriate when schema does the heavy lifting.

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

Purpose5/5

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

The description clearly states the specific action ('Get the token balance') with the resource ('wallet address') and includes important scope details ('with USD value using SailFish as price oracle'). It distinguishes from siblings like get_edu_balance (specific token) and get_multiple_token_balances (multiple vs single).

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

Usage Guidelines3/5

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

The description implies usage context (checking a specific token balance with USD conversion) but doesn't explicitly state when to use this tool versus alternatives like get_multiple_token_balances (for multiple tokens) or get_token_price (price only). No exclusion criteria or prerequisites are mentioned.

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/SailFish-Finance/educhain-ai-agent-kit'

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