Skip to main content
Glama
badger3000

OKX MCP Server

by badger3000

get_live_ticker

Retrieve real-time cryptocurrency ticker data from OKX exchange WebSocket subscriptions for trading analysis and price monitoring.

Instructions

Get the latest ticker data from WebSocket subscription

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
instrumentYesInstrument ID (e.g. BTC-USDT)
formatNoOutput format (json or markdown)markdown

Implementation Reference

  • The core handler logic for the 'get_live_ticker' tool. It checks if subscribed to the ticker channel, auto-subscribes if not, retrieves the latest data from the WebSocket cache, and returns formatted JSON or rich Markdown visualization.
    if (request.params.name === "get_live_ticker") { const isSubscribed = this.wsClient.isSubscribed( "tickers", args.instrument ); if (!isSubscribed) { console.error( `[WebSocket] Auto-subscribing to ticker for ${args.instrument}` ); this.wsClient.subscribe("tickers", args.instrument); // Give it a moment to connect and receive data await new Promise((resolve) => setTimeout(resolve, 1000)); } const tickerData = this.wsClient.getLatestData( "tickers", args.instrument ); if (!tickerData || tickerData.length === 0) { return { content: [ { type: "text", text: `No live data available yet for ${args.instrument}. If you just subscribed, please wait a moment and try again.`, }, ], }; } const ticker = tickerData[0]; if (args.format === "json") { return { content: [ { type: "text", text: JSON.stringify(ticker, null, 2), }, ], }; } else { // Calculate change for markdown formatting const last = parseFloat(ticker.last); const open24h = parseFloat(ticker.open24h); const priceChange = last - open24h; const priceChangePercent = (priceChange / open24h) * 100; const changeSymbol = priceChange >= 0 ? "▲" : "▼"; // Create price range visual const low24h = parseFloat(ticker.low24h); const high24h = parseFloat(ticker.high24h); const range = high24h - low24h; const position = Math.min(Math.max((last - low24h) / range, 0), 1) * 100; const priceBar = `Low ${low24h.toFixed(2)} [${"▮".repeat( Math.floor(position / 5) )}|${"▯".repeat(20 - Math.floor(position / 5))}] ${high24h.toFixed( 2 )} High`; return { content: [ { type: "text", text: `# ${args.instrument} Live Price Data\n\n` + `## Current Price: $${last.toLocaleString()}\n\n` + `**24h Change:** ${changeSymbol} $${Math.abs( priceChange ).toLocaleString()} (${ priceChangePercent >= 0 ? "+" : "" }${priceChangePercent.toFixed(2)}%)\n\n` + `**Bid:** $${parseFloat( ticker.bidPx ).toLocaleString()} | **Ask:** $${parseFloat( ticker.askPx ).toLocaleString()}\n\n` + `### 24-Hour Price Range\n\n` + `\`\`\`\n${priceBar}\n\`\`\`\n\n` + `**24h High:** $${parseFloat( ticker.high24h ).toLocaleString()}\n` + `**24h Low:** $${parseFloat( ticker.low24h ).toLocaleString()}\n\n` + `**24h Volume:** ${parseFloat( ticker.vol24h ).toLocaleString()} units\n\n` + `**Last Updated:** ${new Date( parseInt(ticker.ts) ).toLocaleString()}\n\n` + `*Data source: Live WebSocket feed*`, }, ], }; } }
  • Tool schema definition including name, description, and input schema for validation (instrument required, optional format).
    { name: "get_live_ticker", description: "Get the latest ticker data from WebSocket subscription", inputSchema: { type: "object", properties: { instrument: { type: "string", description: "Instrument ID (e.g. BTC-USDT)", }, format: { type: "string", description: "Output format (json or markdown)", default: "markdown", }, }, required: ["instrument"], }, },
  • src/index.ts:284-386 (registration)
    Registration of all tools including 'get_live_ticker' in the MCP listTools request handler.
    this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ { name: "get_price", description: "Get latest price for an OKX instrument with formatted visualization", inputSchema: { type: "object", properties: { instrument: { type: "string", description: "Instrument ID (e.g. BTC-USDT)", }, format: { type: "string", description: "Output format (json or markdown)", default: "markdown", }, }, required: ["instrument"], }, }, { name: "get_candlesticks", description: "Get candlestick data for an OKX instrument with visualization options", inputSchema: { type: "object", properties: { instrument: { type: "string", description: "Instrument ID (e.g. BTC-USDT)", }, bar: { type: "string", description: "Time interval (e.g. 1m, 5m, 1H, 1D)", default: "1m", }, limit: { type: "number", description: "Number of candlesticks (max 100)", default: 100, }, format: { type: "string", description: "Output format (json, markdown, or table)", default: "markdown", }, }, required: ["instrument"], }, }, { name: "subscribe_ticker", description: "Subscribe to real-time ticker updates for an instrument", inputSchema: { type: "object", properties: { instrument: { type: "string", description: "Instrument ID (e.g. BTC-USDT)", }, }, required: ["instrument"], }, }, { name: "get_live_ticker", description: "Get the latest ticker data from WebSocket subscription", inputSchema: { type: "object", properties: { instrument: { type: "string", description: "Instrument ID (e.g. BTC-USDT)", }, format: { type: "string", description: "Output format (json or markdown)", default: "markdown", }, }, required: ["instrument"], }, }, { name: "unsubscribe_ticker", description: "Unsubscribe from real-time ticker updates for an instrument", inputSchema: { type: "object", properties: { instrument: { type: "string", description: "Instrument ID (e.g. BTC-USDT)", }, }, required: ["instrument"], }, }, ], }));
  • Helper method in OKXWebSocketClient that retrieves the latest cached WebSocket data for a specific channel and instrument, used by get_live_ticker.
    getLatestData(channel: string, instId: string): any | null { const key = `${channel}:${instId}`; return this.dataCache.get(key) || null; }

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

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