get_market_data
Retrieve real-time market data for a specified trading symbol using the Interactive Brokers MCP Server. Input a symbol (e.g., AAPL, TSLA) and optional exchange to access up-to-date financial information.
Instructions
Get real-time market data for a symbol
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| exchange | No | Exchange (optional) | |
| symbol | Yes | Trading symbol (e.g., AAPL, TSLA) |
Implementation Reference
- src/tool-handlers.ts:373-402 (handler)Main MCP tool handler for get_market_data. Ensures gateway and auth readiness, delegates to IBClient.getMarketData, returns formatted JSON result or error.async getMarketData(input: GetMarketDataInput): Promise<ToolHandlerResult> { try { // Ensure Gateway is ready await this.ensureGatewayReady(); // Ensure authentication in headless mode if (this.context.config.IB_HEADLESS_MODE) { await this.ensureAuth(); } const result = await this.context.ibClient.getMarketData(input.symbol, input.exchange); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; } catch (error) { return { content: [ { type: "text", text: this.formatError(error), }, ], }; } }
- src/tools.ts:66-71 (registration)MCP server tool registration for get_market_data, linking schema and handler.server.tool( "get_market_data", "Get real-time market data. Usage: `{ \"symbol\": \"AAPL\" }` or `{ \"symbol\": \"AAPL\", \"exchange\": \"NASDAQ\" }`.", GetMarketDataZodShape, async (args) => await handlers.getMarketData(args) );
- src/tool-definitions.ts:24-27 (schema)Zod schema shape for get_market_data input validation (symbol required, exchange optional). Used in tool registration.export const GetMarketDataZodShape = { symbol: z.string(), exchange: z.string().optional() };
- src/ib-client.ts:325-364 (helper)Underlying IBClient implementation that performs symbol search, retrieves contract ID, and fetches market data snapshot via IB Gateway API.async getMarketData(symbol: string, exchange?: string): Promise<any> { try { // First, get the contract ID for the symbol const searchResponse = await this.client.get( `/iserver/secdef/search?symbol=${symbol}` ); if (!searchResponse.data || searchResponse.data.length === 0) { throw new Error(`Symbol ${symbol} not found`); } const contract = searchResponse.data[0]; const conid = contract.conid; // Get market data snapshot // Using corrected field IDs based on IB Client Portal API documentation: // 31=Last Price, 70=Day High, 71=Day Low, 82=Change, 83=Change%, // 84=Bid, 85=Ask Size, 86=Ask, 87=Volume, 88=Bid Size const response = await this.client.get( `/iserver/marketdata/snapshot?conids=${conid}&fields=31,70,71,82,83,84,85,86,87,88` ); return { symbol: symbol, contract: contract, marketData: response.data }; } catch (error) { Logger.error("Failed to get market data:", error); // Check if this is likely an authentication error if (this.isAuthenticationError(error)) { const authError = new Error(`Authentication required to retrieve market data for ${symbol}. Please authenticate with Interactive Brokers first.`); (authError as any).isAuthError = true; throw authError; } throw new Error(`Failed to retrieve market data for ${symbol}`); } }