Skip to main content
Glama
code-rabi

Interactive Brokers MCP Server

by code-rabi

get_market_data

Retrieve real-time market data for specified symbols from Interactive Brokers to inform trading decisions and monitor financial instruments.

Instructions

Get real-time market data. Usage: { "symbol": "AAPL" } or { "symbol": "AAPL", "exchange": "NASDAQ" }.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
symbolYes
exchangeNo

Implementation Reference

  • The main tool handler function that ensures prerequisites and delegates to IBClient.getMarketData, then formats the response as MCP ToolHandlerResult.
    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),
            },
          ],
        };
      }
    }
  • Zod shape definition for get_market_data input validation used in tool registration.
    export const GetMarketDataZodShape = {
      symbol: z.string(),
      exchange: z.string().optional()
    };
  • src/tools.ts:66-71 (registration)
    MCP tool registration calling server.tool with name, description, 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)
    );
  • Core implementation that resolves symbol to contract ID and fetches market data snapshot from 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}`);
      }
    }

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/code-rabi/interactive-brokers-mcp'

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