get_historical_stock_prices
Retrieve historical stock prices for a specific symbol, with customizable period and interval options, to analyze past market performance using Yahoo Finance data.
Instructions
Get historical stock prices for a given stock symbol.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| interval | No | The interval beween data points. Defaults to "1d". Valid intervals: "1d", "5d", "1wk", "1mo", "3mo" | |
| period | No | The period for historical data. Defaults to "1mo". Valid periods: "1d", "5d", "1mo", "3mo", "6mo", "1y", "2y", "5y", "10y", "ytd", "max" | |
| symbol | Yes | Stock symbol in Yahoo Finance format. |
Input Schema (JSON Schema)
{
"properties": {
"interval": {
"description": "The interval beween data points. Defaults to \"1d\". Valid intervals: \"1d\", \"5d\", \"1wk\", \"1mo\", \"3mo\"",
"type": "string"
},
"period": {
"description": "The period for historical data. Defaults to \"1mo\". Valid periods: \"1d\", \"5d\", \"1mo\", \"3mo\", \"6mo\", \"1y\", \"2y\", \"5y\", \"10y\", \"ytd\", \"max\"",
"type": "string"
},
"symbol": {
"description": "Stock symbol in Yahoo Finance format.",
"type": "string"
}
},
"required": [
"symbol"
],
"type": "object"
}
Implementation Reference
- src/mcp_yahoo_finance/server.py:98-120 (handler)The core handler function in the YahooFinance class that fetches historical stock prices using yfinance's Ticker.history method based on symbol, period, and interval, processes the index, and returns JSON of the Close prices.def get_historical_stock_prices( self, symbol: str, period: Literal[ "1d", "5d", "1mo", "3mo", "6mo", "1y", "2y", "5y", "10y", "ytd", "max" ] = "1mo", interval: Literal["1d", "5d", "1wk", "1mo", "3mo"] = "1d", ) -> str: """Get historical stock prices for a given stock symbol. Args: symbol (str): Stock symbol in Yahoo Finance format. period (str): The period for historical data. Defaults to "1mo". Valid periods: "1d", "5d", "1mo", "3mo", "6mo", "1y", "2y", "5y", "10y", "ytd", "max" interval (str): The interval beween data points. Defaults to "1d". Valid intervals: "1d", "5d", "1wk", "1mo", "3mo" """ stock = Ticker(ticker=symbol, session=self.session) prices = stock.history(period=period, interval=interval) if hasattr(prices.index, "date"): prices.index = prices.index.date.astype(str) # type: ignore return f"{prices['Close'].to_json(orient='index')}"
- src/mcp_yahoo_finance/server.py:213-213 (registration)Registers the get_historical_stock_prices tool by generating its Tool object using generate_tool and including it in the server's list_tools response.generate_tool(yf.get_historical_stock_prices),
- src/mcp_yahoo_finance/server.py:232-234 (registration)Dispatches tool calls named 'get_historical_stock_prices' to the handler function with arguments and returns the result as TextContent.case "get_historical_stock_prices": price = yf.get_historical_stock_prices(**args) return [TextContent(type="text", text=price)]
- src/mcp_yahoo_finance/utils.py:31-65 (helper)Helper function that generates the MCP Tool object, including input schema derived from the function signature, type annotations, and docstring descriptions, used for all tools including get_historical_stock_prices.def generate_tool(func: Any) -> Tool: """Generates a tool schema from a Python function.""" signature = inspect.signature(func) docstring = inspect.getdoc(func) or "" param_descriptions = parse_docstring(docstring) schema = { "name": func.__name__, "description": docstring.split("Args:")[0].strip(), "inputSchema": { "type": "object", "properties": {}, }, } for param_name, param in signature.parameters.items(): param_type = ( "number" if param.annotation is float else "string" if param.annotation is str else "string" ) schema["inputSchema"]["properties"][param_name] = { "type": param_type, "description": param_descriptions.get(param_name, ""), } if "required" not in schema["inputSchema"]: schema["inputSchema"]["required"] = [param_name] else: if "=" not in str(param): schema["inputSchema"]["required"].append(param_name) return Tool(**schema)