Skip to main content
Glama
buildwithgrove

Grove's MCP Server for Pocket Network

get_solana_token_balance

Retrieve SPL token balances for a Solana wallet address, optionally filtering by specific token mint or network type.

Instructions

Get SPL token balance(s) for a Solana wallet

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
walletAddressYesSolana wallet address
mintAddressNoOptional: SPL token mint address (if not provided, returns all token balances)
networkNoNetwork type (defaults to mainnet)

Implementation Reference

  • Tool registration definition including name, description, and input schema for get_solana_token_balance
    {
      name: 'get_solana_token_balance',
      description: 'Get SPL token balance(s) for a Solana wallet',
      inputSchema: {
        type: 'object',
        properties: {
          walletAddress: {
            type: 'string',
            description: 'Solana wallet address',
          },
          mintAddress: {
            type: 'string',
            description: 'Optional: SPL token mint address (if not provided, returns all token balances)',
          },
          network: {
            type: 'string',
            enum: ['mainnet', 'testnet'],
            description: 'Network type (defaults to mainnet)',
          },
        },
        required: ['walletAddress'],
      },
  • Handler logic for the tool: extracts parameters and delegates to SolanaService.getTokenBalance, formats response
    case 'get_solana_token_balance': {
      const walletAddress = args?.walletAddress as string;
      const mintAddress = args?.mintAddress as string | undefined;
      const network = (args?.network as 'mainnet' | 'testnet') || 'mainnet';
    
      const result = await solanaService.getTokenBalance(walletAddress, mintAddress, network);
    
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(result, null, 2),
          },
        ],
        isError: !result.success,
      };
    }
  • Core implementation of token balance retrieval: calls Solana RPC getTokenAccountsByOwner, parses single or all token accounts, computes balances with decimals
    async getTokenBalance(
      walletAddress: string,
      mintAddress?: string,
      network: 'mainnet' | 'testnet' = 'mainnet'
    ): Promise<EndpointResponse> {
      const service = this.blockchainService.getServiceByBlockchain('solana', network);
    
      if (!service) {
        return {
          success: false,
          error: `Solana service not found for ${network}`,
        };
      }
    
      try {
        // Get all token accounts for the wallet
        const params = mintAddress
          ? [
              walletAddress,
              {
                mint: mintAddress,
              },
              {
                encoding: 'jsonParsed',
              },
            ]
          : [
              walletAddress,
              {
                programId: SolanaService.TOKEN_PROGRAM_ID,
              },
              {
                encoding: 'jsonParsed',
              },
            ];
    
        const result = await this.blockchainService.callRPCMethod(
          service.id,
          'getTokenAccountsByOwner',
          params
        );
    
        if (!result.success) {
          return result;
        }
    
        const accounts = result.data?.value || [];
    
        if (mintAddress) {
          // Return specific token balance
          if (accounts.length === 0) {
            return {
              success: true,
              data: {
                mint: mintAddress,
                owner: walletAddress,
                balance: '0',
                decimals: 0,
              },
              metadata: result.metadata,
            };
          }
    
          const account = accounts[0];
          const tokenAmount = account.account?.data?.parsed?.info?.tokenAmount;
    
          return {
            success: true,
            data: {
              mint: mintAddress,
              owner: walletAddress,
              balance: tokenAmount?.amount || '0',
              uiAmount: tokenAmount?.uiAmount || 0,
              decimals: tokenAmount?.decimals || 0,
            },
            metadata: result.metadata,
          };
        } else {
          // Return all token balances
          const balances = accounts.map((account: any) => {
            const info = account.account?.data?.parsed?.info;
            const tokenAmount = info?.tokenAmount;
    
            return {
              mint: info?.mint,
              balance: tokenAmount?.amount || '0',
              uiAmount: tokenAmount?.uiAmount || 0,
              decimals: tokenAmount?.decimals || 0,
            };
          });
    
          return {
            success: true,
            data: {
              owner: walletAddress,
              tokens: balances,
              count: balances.length,
            },
            metadata: result.metadata,
          };
        }
      } catch (error) {
        return {
          success: false,
          error: error instanceof Error ? error.message : 'Failed to get Solana token balance',
        };
      }
    }

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/buildwithgrove/mcp-pocket'

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