get_wallet_balance
Retrieve wallet balance details for a Bybit account by specifying account type and cryptocurrency. Use this tool to access real-time balance information for UNIFIED, CONTRACT, or SPOT accounts.
Instructions
Get wallet balance information for the authenticated user
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| accountType | Yes | Account type | |
| coin | No | Cryptocurrency symbol, e.g., BTC, ETH, USDT. If not specified, returns all coins. |
Implementation Reference
- src/tools/GetWalletBalance.ts:66-112 (handler)The toolCall method implements the core logic of the get_wallet_balance tool: validates input, fetches wallet balance from Bybit API using this.client.getWalletBalance, handles errors, and formats the response.async toolCall(request: z.infer<typeof CallToolRequestSchema>) { try { this.logInfo("Starting get_wallet_balance tool call") if (this.isDevMode) { throw new Error("Cannot get wallet balance in development mode - API credentials required") } // Parse and validate input const validationResult = inputSchema.safeParse(request.params.arguments) if (!validationResult.success) { const errorDetails = validationResult.error.errors.map(err => ({ field: err.path.join('.'), message: err.message, code: err.code })) throw new Error(`Invalid input: ${JSON.stringify(errorDetails)}`) } const { accountType, coin } = validationResult.data this.logInfo(`Validated arguments - accountType: ${accountType}${coin ? `, coin: ${coin}` : ''}`) // Execute API request with rate limiting and retry logic const response = await this.executeRequest(async () => { return await this.getWalletData(accountType, coin) }) // Format response const result: FormattedWalletResponse = { accountType, coin, data: { list: response.list }, timestamp: new Date().toISOString(), meta: { requestId: crypto.randomUUID() } } this.logInfo(`Successfully retrieved wallet balance for ${accountType}${coin ? ` (${coin})` : ''}`) return this.formatResponse(result) } catch (error) { this.logInfo(`Error in get_wallet_balance: ${error instanceof Error ? error.message : String(error)}`) return this.handleError(error) } }
- src/tools/GetWalletBalance.ts:35-53 (schema)MCP Tool definition including name, description, and JSON input schema for get_wallet_balance.toolDefinition: Tool = { name: this.name, description: "Get wallet balance information for the authenticated user", inputSchema: { type: "object", properties: { accountType: { type: "string", description: "Account type", enum: ["UNIFIED", "CONTRACT", "SPOT"], }, coin: { type: "string", description: "Cryptocurrency symbol, e.g., BTC, ETH, USDT. If not specified, returns all coins.", }, }, required: ["accountType"], }, }
- src/tools/GetWalletBalance.ts:12-15 (schema)Zod schema used internally for validating tool input parameters.const inputSchema = z.object({ accountType: z.enum(["UNIFIED", "CONTRACT", "SPOT"]), coin: z.string().optional() })
- src/utils/toolLoader.ts:23-81 (registration)Dynamically loads all tool implementations from src/tools/ directory by importing and instantiating classes like GetWalletBalance, validating they are valid tools, and returning an array of instances. This registers get_wallet_balance among others.export async function loadTools(): Promise<BaseToolImplementation[]> { try { const toolsPath = await findToolsPath() const files = await fs.readdir(toolsPath) const tools: BaseToolImplementation[] = [] for (const file of files) { if (!isToolFile(file)) { continue } try { const modulePath = `file://${join(toolsPath, file)}` const { default: ToolClass } = await import(modulePath) if (!ToolClass || typeof ToolClass !== 'function') { console.warn(JSON.stringify({ type: "warning", message: `Invalid tool class in ${file}` })) continue } const tool = new ToolClass() if ( tool instanceof BaseToolImplementation && tool.name && tool.toolDefinition && typeof tool.toolCall === "function" ) { tools.push(tool) console.info(JSON.stringify({ type: "info", message: `Loaded tool: ${tool.name}` })) } else { console.warn(JSON.stringify({ type: "warning", message: `Invalid tool implementation in ${file}` })) } } catch (error) { console.error(JSON.stringify({ type: "error", message: `Error loading tool from ${file}: ${error instanceof Error ? error.message : String(error)}` })) } } return tools } catch (error) { console.error(JSON.stringify({ type: "error", message: `Failed to load tools: ${error instanceof Error ? error.message : String(error)}` })) return [] } }
- src/index.ts:134-151 (registration)In the main server startup, calls loadTools() to get tool instances including get_wallet_balance, creates a map by name, and uses it in MCP request handlers for listing and calling tools.const tools = await loadTools() toolsMap = createToolsMap(tools) if (tools.length === 0) { console.log(JSON.stringify(formatJsonRpcMessage( "warning", "No tools were loaded. Server will start but may have limited functionality." ))) } else { console.log(JSON.stringify(formatJsonRpcMessage( "info", `Loaded ${tools.length} tools: ${tools.map(t => t.name).join(", ")}` ))) } const transport = new StdioServerTransport() await server.connect(transport)