get_apt_balance
Retrieve the APT token balance for an Aptos blockchain account, displaying amounts in both Octas and APT units for accurate financial tracking.
Instructions
Get the native APT token balance of an Aptos account. This is used for checking the current balance of APT tokens in an account. Returns the account balance in both Octas and APT.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| account_address | Yes | Aptos account address, e.g., 0x1 or 0x742d35Cc6634C0532925a3b8D6Ac0C4db9c8b3 |
Implementation Reference
- src/tools/native/getBalance.ts:25-49 (handler)Main handler function for the get_apt_balance tool. Validates input arguments, calls the performance helper, and returns formatted success or error response.export async function getAptBalanceHandler(args: Record<string, any> | undefined) { if (!isGetAptBalanceArgs(args)) { throw new Error("Invalid arguments for get_apt_balance"); } const { account_address } = args; try { const results = await performGetAptBalance(account_address); return { content: [{ type: "text", text: results }], isError: false, }; } catch (error) { return { content: [ { type: "text", text: `Error getting APT balance: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } }
- src/tools/native/getBalance.ts:5-18 (schema)Tool schema definition including name, description, and input schema requiring 'account_address' string.export const GET_APT_BALANCE: Tool = { name: "get_apt_balance", description: "Get the native APT token balance of an Aptos account. This is used for checking the current balance of APT tokens in an account. Returns the account balance in both Octas and APT.", inputSchema: { type: "object", properties: { account_address: { type: "string", description: "Aptos account address, e.g., 0x1 or 0x742d35Cc6634C0532925a3b8D6Ac0C4db9c8b3", }, }, required: ["account_address"], }, };
- src/tools/native/getBalance.ts:56-84 (helper)Core logic to fetch APT balance using Aptos client, format output in Octas and APT, handle account not found cases.export async function performGetAptBalance(accountAddress: string): Promise<string> { try { const aptos = getAptosClient(); // Get account APT balance const balance = await aptos.getAccountAPTAmount({ accountAddress }); return `APT Balance Information: Account: ${formatAddress(accountAddress)} Balance: ${balance} Octas Balance: ${formatAPT(balance)} APT Full Account Address: ${accountAddress}`; } catch (error) { console.error('Error getting APT balance:', error); // Check if account doesn't exist if (error instanceof Error && error.message.includes('not found')) { return `APT Balance Information: Account: ${formatAddress(accountAddress)} Balance: 0 Octas (0 APT) Status: Account does not exist or has not been initialized Note: Account needs to receive at least one transaction to be initialized on-chain`; } throw new Error(`Failed to get APT balance: ${error instanceof Error ? error.message : String(error)}`); } }
- src/tools/native/getBalance.ts:91-98 (helper)Type guard function to validate input arguments contain a string 'account_address'.export function isGetAptBalanceArgs(args: unknown): args is { account_address: string } { return ( typeof args === "object" && args !== null && "account_address" in args && typeof (args as any).account_address === "string" ); }
- src/index.ts:120-121 (registration)Tool registration in the main MCP server switch statement for stdio transport, dispatching to getAptBalanceHandler.case "get_apt_balance": return await getAptBalanceHandler(args);