get_price
Retrieve current cryptocurrency prices from OKX exchange for any trading instrument to monitor market values and inform trading decisions.
Instructions
Get latest price for an OKX instrument
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| instrument | Yes | Instrument ID (e.g. BTC-USDT) |
Implementation Reference
- src/index.ts:147-182 (handler)The main execution logic for the 'get_price' tool. It queries the OKX /market/ticker API, handles errors, extracts the ticker data, and returns a formatted JSON response with instrument details, prices, and timestamp.if (request.params.name === 'get_price') { console.error(`[API] Fetching price for instrument: ${args.instrument}`); const response = await this.axiosInstance.get<OKXTickerResponse>( '/market/ticker', { params: { instId: args.instrument }, } ); if (response.data.code !== '0') { throw new Error(`OKX API error: ${response.data.msg}`); } if (!response.data.data || response.data.data.length === 0) { throw new Error('No data returned from OKX API'); } const ticker = response.data.data[0]; return { content: [ { type: 'text', text: JSON.stringify({ instrument: ticker.instId, lastPrice: ticker.last, bid: ticker.bidPx, ask: ticker.askPx, high24h: ticker.high24h, low24h: ticker.low24h, volume24h: ticker.vol24h, timestamp: new Date(parseInt(ticker.ts)).toISOString(), }, null, 2), }, ], }; } else {
- src/index.ts:84-97 (registration)Registers the 'get_price' tool in the MCP listTools handler, specifying its name, description, and input schema requiring an 'instrument' parameter.{ name: 'get_price', description: 'Get latest price for an OKX instrument', inputSchema: { type: 'object', properties: { instrument: { type: 'string', description: 'Instrument ID (e.g. BTC-USDT)', }, }, required: ['instrument'], }, },
- src/index.ts:13-28 (schema)Type definition for the OKX ticker API response, providing type safety for the data parsed in the get_price handler.interface OKXTickerResponse { code: string; msg: string; data: Array<{ instId: string; last: string; askPx: string; bidPx: string; open24h: string; high24h: string; low24h: string; volCcy24h: string; vol24h: string; ts: string; }>; }
- src/index.ts:127-127 (registration)Validates that the requested tool name is either 'get_price' or 'get_candlesticks' in the callTool handler, effectively registering the allowed tools.if (!['get_price', 'get_candlesticks'].includes(request.params.name)) {