Skip to main content
Glama
cuongpo

Rootstock MCP Server

by cuongpo

get_balance

Retrieve the balance of a wallet address for native or ERC20 tokens on the Rootstock blockchain using standardized APIs from the MCP Server.

Instructions

Get the balance of a wallet address (native tokens or ERC20 tokens)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
addressYesWallet address to check balance for
tokenAddressNoOptional ERC20 token contract address

Implementation Reference

  • MCP tool registration, input schema, and handler for 'get_balance'. Handles both native and ERC20 token balances by delegating to RootstockClient methods.
    server.tool(
      "get_balance",
      "Get the balance of a wallet address (native tokens or ERC20 tokens)",
      {
        address: z.string().describe("Wallet address to check balance for"),
        tokenAddress: z.string().optional().describe("Optional ERC20 token contract address"),
      },
      async ({ address, tokenAddress }) => {
        try {
          if (tokenAddress) {
            const tokenBalance = await rootstockClient.getTokenBalance(address, tokenAddress);
            return {
              content: [
                {
                  type: "text",
                  text: `Token Balance:\n\nAddress: ${address}\nToken: ${tokenBalance.name} (${tokenBalance.symbol})\nBalance: ${tokenBalance.balance} ${tokenBalance.symbol}`,
                },
              ],
            };
          } else {
            const balance = await rootstockClient.getBalance(address);
            return {
              content: [
                {
                  type: "text",
                  text: `Native Balance:\n\nAddress: ${address}\nBalance: ${balance} ${rootstockClient.getCurrencySymbol()}`,
                },
              ],
            };
          }
        } catch (error) {
          return {
            content: [
              {
                type: "text",
                text: `Error getting balance: ${error instanceof Error ? error.message : String(error)}`,
              },
            ],
          };
        }
      }
    );
  • Core helper function to fetch native token (tRBTC) balance using ethers.JsonRpcProvider.getBalance and format to ether units.
    async getBalance(address: string): Promise<string> {
      try {
        const balance = await this.getProvider().getBalance(address);
        return ethers.formatEther(balance);
      } catch (error) {
        throw new Error(`Failed to get balance: ${error}`);
      }
    }
  • Core helper function to fetch ERC20 token balance, including token metadata (name, symbol, decimals), using direct contract calls.
    async getTokenBalance(address: string, tokenAddress: string): Promise<TokenBalance> {
      try {
        const tokenContract = new ethers.Contract(
          tokenAddress,
          [
            'function balanceOf(address) view returns (uint256)',
            'function decimals() view returns (uint8)',
            'function symbol() view returns (string)',
            'function name() view returns (string)',
          ],
          this.getProvider()
        );
    
        const [balance, decimals, symbol, name] = await Promise.all([
          tokenContract.balanceOf(address),
          tokenContract.decimals(),
          tokenContract.symbol(),
          tokenContract.name(),
        ]);
    
        return {
          tokenAddress,
          balance: ethers.formatUnits(balance, decimals),
          decimals,
          symbol,
          name,
        };
      } catch (error) {
        throw new Error(`Failed to get token balance: ${error}`);
      }
    }
  • Registration of the 'get_balance' tool in the MCP server.
    server.tool(
      "get_balance",
      "Get the balance of a wallet address (native tokens or ERC20 tokens)",
      {
        address: z.string().describe("Wallet address to check balance for"),
        tokenAddress: z.string().optional().describe("Optional ERC20 token contract address"),
      },
      async ({ address, tokenAddress }) => {
        try {
          if (tokenAddress) {
            const tokenBalance = await rootstockClient.getTokenBalance(address, tokenAddress);
            return {
              content: [
                {
                  type: "text",
                  text: `Token Balance:\n\nAddress: ${address}\nToken: ${tokenBalance.name} (${tokenBalance.symbol})\nBalance: ${tokenBalance.balance} ${tokenBalance.symbol}`,
                },
              ],
            };
          } else {
            const balance = await rootstockClient.getBalance(address);
            return {
              content: [
                {
                  type: "text",
                  text: `Native Balance:\n\nAddress: ${address}\nBalance: ${balance} ${rootstockClient.getCurrencySymbol()}`,
                },
              ],
            };
          }
        } catch (error) {
          return {
            content: [
              {
                type: "text",
                text: `Error getting balance: ${error instanceof Error ? error.message : String(error)}`,
              },
            ],
          };
        }
      }
    );
  • Input schema for the get_balance tool using Zod validation.
      address: z.string().describe("Wallet address to check balance for"),
      tokenAddress: z.string().optional().describe("Optional ERC20 token contract address"),
    },

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/cuongpo/rootstock-mcp'

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