get_portfolio
Retrieve a complete list of all assets in your OKX trading account to monitor holdings and analyze portfolio composition.
Instructions
Get a list of all assets in the account
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/tools/get_portfolio.ts:21-33 (handler)The default exported async function implementing the core logic of the 'get_portfolio' MCP tool. It invokes the OKX API client to fetch portfolio data and formats it as JSON text response, with error handling.export default async function get_portfolio({}: InferSchema<typeof schema>) { try { const portfolio = await okxApiClient.getPortfolio(); return { content: [{ type: 'text', text: JSON.stringify(portfolio, null, 2) }], }; } catch (error) { const message = error instanceof Error ? error.message : 'An unknown error occurred'; return { content: [{ type: 'text', text: JSON.stringify({ error: message }, null, 2) }], }; } }
- src/tools/get_portfolio.ts:8-8 (schema)Empty Zod schema indicating the tool takes no input parameters.export const schema = {};
- src/tools/get_portfolio.ts:10-19 (registration)Metadata export defining the tool's name, description, and annotations for registration in the MCP server.export const metadata = { name: 'get_portfolio', description: 'Get a list of all assets in the account', annotations: { title: 'Get Portfolio', readOnlyHint: true, destructiveHint: false, idempotentHint: true, }, };
- src/services/okxApiClient.ts:18-55 (helper)Supporting method in OkxApiClient class that fetches account balances from OKX API, converts non-USDT balances to USDT equivalent using ticker prices, and returns formatted portfolio data.async getPortfolio() { try { const response = await client.getBalance(); const details = response[0].details; const portfolio = []; for (const item of details) { const ccy = item.ccy; let usdtValue = 0; if (ccy !== "USDT") { try { const tickerResponse = await client.getTicker({ instId: `${ccy}-USDT`, }); if (tickerResponse.length > 0) { usdtValue = parseFloat(tickerResponse[0].last); } } catch (error) { // ignore errors for tickers that don't exist } } portfolio.push({ currency: ccy, totalBalance: parseFloat(item.eq), frozenBalance: parseFloat(item.frozenBal), usdtValue: ccy === "USDT" ? parseFloat(item.eq) : parseFloat(item.eq) * usdtValue, }); } return portfolio; } catch (error) { console.error("Error fetching portfolio:", error); throw error; } }