Skip to main content
Glama
mbarinov

OKX MCP Server

by mbarinov

get_portfolio

Read-onlyIdempotent

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
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • 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) }],
        };
      }
    }
  • Empty Zod schema indicating the tool takes no input parameters.
    export const schema = {};
  • 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,
      },
    };
  • 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;
      }
    }

Tool Definition Quality

Score is being calculated. Check back soon.

Install Server

Other Tools

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/mbarinov/okx-mcp'

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