Skip to main content
Glama

get_balance

Retrieve account balances for all assets or a specific cryptocurrency on supported exchanges like MEXC, Gate.io, Bitget, and Kraken.

Instructions

Get account balances for all assets (or a specific asset) on a supported exchange

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
exchangeYesExchange to query. Supported: mexc, gateio, bitget, kraken
assetNoOptional asset to filter by (e.g., USDT, BTC). Returns all assets if omitted.

Implementation Reference

  • The main handler function for get_balance tool. Validates exchange input, gets the exchange connector, calls connector.getBalance() to fetch balances, optionally filters by asset, and returns the formatted JSON response with balance information.
    async ({ exchange, asset }) => {
      const validExchange = validateExchange(exchange);
    
      const connector = await getConnectorSafe(exchange);
      const balances = await connector.getBalance();
    
      let entries = Object.values(balances);
      if (asset) {
        const upperAsset = asset.toUpperCase();
        entries = entries.filter((b) => b.asset.toUpperCase() === upperAsset);
        if (entries.length === 0) {
          return {
            content: [
              {
                type: 'text' as const,
                text: JSON.stringify(
                  {
                    asset: upperAsset,
                    message: `No balance found for asset: ${upperAsset}`,
                    exchange: validExchange,
                  },
                  null,
                  2
                ),
              },
            ],
          };
        }
      }
    
      return {
        content: [
          {
            type: 'text' as const,
            text: JSON.stringify(
              {
                balances: entries,
                totalAssets: entries.length,
                exchange: validExchange,
              },
              null,
              2
            ),
          },
        ],
      };
    }
  • The registration of the get_balance tool with the MCP server using server.tool(). Defines the tool name, description, input schema (exchange and optional asset), and associates it with the handler function.
    export function registerAccountTools(server: McpServer): void {
      server.tool(
        'get_balance',
        'Get account balances for all assets (or a specific asset) on a supported exchange',
        {
          exchange: ExchangeParam,
          asset: z
            .string()
            .optional()
            .describe('Optional asset to filter by (e.g., USDT, BTC). Returns all assets if omitted.'),
        },
        async ({ exchange, asset }) => {
          const validExchange = validateExchange(exchange);
    
          const connector = await getConnectorSafe(exchange);
          const balances = await connector.getBalance();
    
          let entries = Object.values(balances);
          if (asset) {
            const upperAsset = asset.toUpperCase();
            entries = entries.filter((b) => b.asset.toUpperCase() === upperAsset);
            if (entries.length === 0) {
              return {
                content: [
                  {
                    type: 'text' as const,
                    text: JSON.stringify(
                      {
                        asset: upperAsset,
                        message: `No balance found for asset: ${upperAsset}`,
                        exchange: validExchange,
                      },
                      null,
                      2
                    ),
                  },
                ],
              };
            }
          }
    
          return {
            content: [
              {
                type: 'text' as const,
                text: JSON.stringify(
                  {
                    balances: entries,
                    totalAssets: entries.length,
                    exchange: validExchange,
                  },
                  null,
                  2
                ),
              },
            ],
          };
        }
      );
  • Input parameter schema for get_balance tool. Defines 'exchange' parameter (required, using ExchangeParam) and 'asset' parameter (optional string to filter by specific asset like USDT, BTC).
    {
      exchange: ExchangeParam,
      asset: z
        .string()
        .optional()
        .describe('Optional asset to filter by (e.g., USDT, BTC). Returns all assets if omitted.'),
    },
  • Schema definition for ExchangeParam used by get_balance. A zod string schema that describes supported exchanges (mexc, gateio, bitget, kraken).
    export const ExchangeParam = z
      .string()
      .describe('Exchange to query. Supported: mexc, gateio, bitget, kraken');
  • getConnectorSafe helper function used by get_balance handler. Validates the exchange string and returns a BaseExchangeConnector instance from the ExchangeFactory, with error handling for connection failures.
    export async function getConnectorSafe(exchange: string): Promise<BaseExchangeConnector> {
      const validExchange = validateExchange(exchange);
      const { ExchangeFactory } = await import('@3rd-eye-labs/openmm');
      try {
        return await ExchangeFactory.getExchange(validExchange as any);
      } catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        throw new Error(`Failed to connect to ${validExchange}: ${message}`);
      }
    }

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/QBT-Labs/openmm-mcp'

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