get_historical_stock_prices
Retrieve historical stock prices for a specific company using its ticker symbol, date range, and interval options. Analyze price trends, compare data, and make informed decisions with accurate financial data.
Instructions
Gets historical stock prices for a company.
Args:
ticker: Ticker symbol of the company (e.g. AAPL, GOOGL)
start_date: Start date of the price data (e.g. 2020-01-01)
end_date: End date of the price data (e.g. 2020-12-31)
interval: Interval of the price data (e.g. minute, hour, day, week, month)
interval_multiplier: Multiplier of the interval (e.g. 1, 2, 3)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| end_date | Yes | ||
| interval | No | day | |
| interval_multiplier | No | ||
| start_date | Yes | ||
| ticker | Yes |
Implementation Reference
- server.py:165-198 (handler)The @mcp.tool() decorator registers the tool, and the async function implements the logic: constructs API URL, calls make_request helper, extracts 'prices' from response, and returns JSON string. Includes input schema via type hints and docstring.@mcp.tool() async def get_historical_stock_prices( ticker: str, start_date: str, end_date: str, interval: str = "day", interval_multiplier: int = 1, ) -> str: """Gets historical stock prices for a company. Args: ticker: Ticker symbol of the company (e.g. AAPL, GOOGL) start_date: Start date of the price data (e.g. 2020-01-01) end_date: End date of the price data (e.g. 2020-12-31) interval: Interval of the price data (e.g. minute, hour, day, week, month) interval_multiplier: Multiplier of the interval (e.g. 1, 2, 3) """ # Fetch data from the API url = f"{FINANCIAL_DATASETS_API_BASE}/prices/?ticker={ticker}&interval={interval}&interval_multiplier={interval_multiplier}&start_date={start_date}&end_date={end_date}" data = await make_request(url) # Check if data is found if not data: return "Unable to fetch prices or no prices found." # Extract the prices prices = data.get("prices", []) # Check if prices are found if not prices: return "Unable to fetch prices or no prices found." # Stringify the prices return json.dumps(prices, indent=2)
- server.py:25-41 (helper)Shared helper function used by get_historical_stock_prices (and other tools) to make authenticated HTTP GET requests to the Financial Datasets API.async def make_request(url: str) -> dict[str, any] | None: """Make a request to the Financial Datasets API with proper error handling.""" # Load environment variables from .env file load_dotenv() headers = {} if api_key := os.environ.get("FINANCIAL_DATASETS_API_KEY"): headers["X-API-KEY"] = api_key async with httpx.AsyncClient() as client: try: response = await client.get(url, headers=headers, timeout=30.0) response.raise_for_status() return response.json() except Exception as e: return {"Error": str(e)}