get_stock_price_date_range
Retrieve historical stock prices for a specific symbol within a defined date range using YYYY-MM-DD format. Ideal for financial analysis and tracking market trends over time.
Instructions
Get the stock prices for a given date range for a given stock symbol.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| end_date | Yes | The end date in YYYY-MM-DD format. | |
| start_date | Yes | The start date in YYYY-MM-DD format. | |
| symbol | Yes | Stock symbol in Yahoo Finance format. |
Input Schema (JSON Schema)
{
"properties": {
"end_date": {
"description": "The end date in YYYY-MM-DD format.",
"type": "string"
},
"start_date": {
"description": "The start date in YYYY-MM-DD format.",
"type": "string"
},
"symbol": {
"description": "Stock symbol in Yahoo Finance format.",
"type": "string"
}
},
"required": [
"symbol",
"start_date",
"end_date"
],
"type": "object"
}
Implementation Reference
- src/mcp_yahoo_finance/server.py:83-96 (handler)The core handler function implementing the tool logic: fetches stock closing prices for a symbol over a date range using yfinance and returns JSON.def get_stock_price_date_range( self, symbol: str, start_date: str, end_date: str ) -> str: """Get the stock prices for a given date range for a given stock symbol. Args: symbol (str): Stock symbol in Yahoo Finance format. start_date (str): The start date in YYYY-MM-DD format. end_date (str): The end date in YYYY-MM-DD format. """ stock = Ticker(ticker=symbol, session=self.session) prices = stock.history(start=start_date, end=end_date) prices.index = prices.index.astype(str) return f"{prices['Close'].to_json(orient='index')}"
- src/mcp_yahoo_finance/utils.py:31-65 (schema)Generates the inputSchema and Tool object for get_stock_price_date_range based on its signature (symbol:str, start_date:str, end_date:str) and docstring descriptions.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)
- src/mcp_yahoo_finance/server.py:204-218 (registration)Registers the get_stock_price_date_range tool (line 212) in the MCP server's list_tools() handler using generate_tool.@server.list_tools() async def list_tools() -> list[Tool]: return [ generate_tool(yf.cmd_run), generate_tool(yf.get_recommendations), generate_tool(yf.get_news), generate_tool(yf.get_current_stock_price), generate_tool(yf.get_stock_price_by_date), generate_tool(yf.get_stock_price_date_range), generate_tool(yf.get_historical_stock_prices), generate_tool(yf.get_dividends), generate_tool(yf.get_income_statement), generate_tool(yf.get_cashflow), generate_tool(yf.get_earning_dates), ]
- src/mcp_yahoo_finance/server.py:229-231 (registration)Dispatches calls to the get_stock_price_date_range handler in the MCP server's call_tool() implementation.case "get_stock_price_date_range": price = yf.get_stock_price_date_range(**args) return [TextContent(type="text", text=price)]