get-stock-data
Retrieve real-time and historical stock market data for analysis, including intraday prices with customizable time intervals and data ranges.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| symbol | Yes | Stock symbol (e.g., IBM, AAPL) | |
| interval | No | Time interval between data points (default: 5min) | |
| outputsize | No | Amount of data to return (compact: latest 100 data points, full: up to 20 years of data) |
Implementation Reference
- src/index.ts:46-58 (handler)Handler function for the 'get-stock-data' tool. It calls getStockData, handles errors, and returns MCP-formatted content.async ({ symbol, interval = "5min", outputsize = "compact" }) => { try { const data = await getStockData(symbol, interval, outputsize); return { content: [{ type: "text", text: data }] }; } catch (error) { return { content: [{ type: "text", text: `Error fetching stock data: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } }
- src/index.ts:41-45 (schema)Input schema for the 'get-stock-data' tool using Zod, defining parameters symbol, interval, and outputsize.{ symbol: z.string().describe("Stock symbol (e.g., IBM, AAPL)"), interval: z.enum(["1min", "5min", "15min", "30min", "60min"]).optional().describe("Time interval between data points (default: 5min)"), outputsize: z.enum(["compact", "full"]).optional().describe("Amount of data to return (compact: latest 100 data points, full: up to 20 years of data)") },
- src/index.ts:39-40 (registration)Registration of the 'get-stock-data' tool on the MCP server.server.tool( "get-stock-data",
- src/alphaVantage.ts:21-68 (helper)Main helper function getStockData that fetches intraday or daily stock data from Alpha Vantage API, processes the response, and formats it using formatTimeSeriesData.export async function getStockData(symbol: string | string[], interval: string | string[] | 'daily', outputsize: string = 'compact'): Promise<string> { try { // Ensure parameters are strings, not arrays const symbolStr = Array.isArray(symbol) ? symbol[0] : symbol; const intervalStr = Array.isArray(interval) ? interval[0] : interval; const outputsizeStr = Array.isArray(outputsize) ? outputsize[0] : outputsize; let url: string; let timeSeriesKey: string; if (intervalStr === 'daily') { // Use TIME_SERIES_DAILY endpoint url = `${BASE_URL}?function=TIME_SERIES_DAILY&symbol=${symbolStr}&outputsize=${outputsizeStr}&apikey=${API_KEY}`; timeSeriesKey = 'Time Series (Daily)'; } else { // Use TIME_SERIES_INTRADAY endpoint url = `${BASE_URL}?function=TIME_SERIES_INTRADAY&symbol=${symbolStr}&interval=${intervalStr}&outputsize=${outputsizeStr}&apikey=${API_KEY}`; timeSeriesKey = `Time Series (${intervalStr})`; } const response = await axios.get(url); // Check for error messages from Alpha Vantage if (response.data['Error Message']) { throw new Error(response.data['Error Message']); } if (response.data['Note']) { console.warn('API Usage Note:', response.data['Note']); } // Extract the time series data const timeSeries = response.data[timeSeriesKey]; if (!timeSeries) { throw new Error('No time series data found in the response'); } // Format the data const formattedData = formatTimeSeriesData(timeSeries, symbolStr, intervalStr); return formattedData; } catch (error) { if (axios.isAxiosError(error)) { throw new Error(`API request failed: ${error.message}`); } throw error; } }