get-stock-data
Retrieve historical stock price data for any symbol with customizable time intervals and data output sizes to support investment analysis and market research.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| 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) | |
| symbol | Yes | Stock symbol (e.g., IBM, AAPL) |
Implementation Reference
- src/index.ts:64-83 (handler)MCP tool handler for 'get-stock-data' that invokes getStockData helper with provided parameters and returns formatted text content or error.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:51-63 (schema)Input schema using Zod for tool parameters: symbol (string), interval (enum optional), outputsize (enum optional).{ 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:49-84 (registration)Full registration of the 'get-stock-data' tool on the MCP server with name, input schema, and handler function.server.tool( 'get-stock-data', { 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)', ), }, 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/alphaVantage.ts:21-68 (helper)Primary helper function implementing the core logic: constructs Alpha Vantage API URL for intraday or daily time series, fetches data via axios, validates response, and formats output 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; } }
- src/alphaVantage.ts:73-96 (helper)Supporting utility that formats raw time series data into a human-readable string, showing OHLCV for the 10 most recent data points.function formatTimeSeriesData(timeSeries: any, symbol: string, interval: string | 'daily'): string { const dates = Object.keys(timeSeries).sort().reverse(); // Most recent first let result = `Stock data for ${symbol.toUpperCase()} (${interval === 'daily' ? 'Daily' : interval} intervals):\n\n`; // Limit to 10 data points to avoid overwhelming responses const limitedDates = dates.slice(0, 10); for (const date of limitedDates) { const data = timeSeries[date]; result += `${date}:\n`; result += ` Open: ${data['1. open']}\n`; result += ` High: ${data['2. high']}\n`; result += ` Low: ${data['3. low']}\n`; result += ` Close: ${data['4. close']}\n`; result += ` Volume: ${data['5. volume']}\n\n`; } if (dates.length > 10) { result += `... and ${dates.length - 10} more data points available.\n`; } return result; }