get_stock_price_by_date
Retrieve the historical stock price for a specific symbol on a given date using YYYY-MM-DD format. Ideal for analyzing past market performance and financial research.
Instructions
Get the stock price for a given stock symbol on a specific date.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| date | Yes | The date in YYYY-MM-DD format. | |
| symbol | Yes | Stock symbol in Yahoo Finance format. |
Input Schema (JSON Schema)
{
"properties": {
"date": {
"description": "The date in YYYY-MM-DD format.",
"type": "string"
},
"symbol": {
"description": "Stock symbol in Yahoo Finance format.",
"type": "string"
}
},
"required": [
"symbol",
"date"
],
"type": "object"
}
Implementation Reference
- src/mcp_yahoo_finance/server.py:72-81 (handler)Core handler function that executes the tool logic: fetches historical stock price for a specific date using yfinance Ticker.history and returns the closing price.def get_stock_price_by_date(self, symbol: str, date: str) -> str: """Get the stock price for a given stock symbol on a specific date. Args: symbol (str): Stock symbol in Yahoo Finance format. date (str): The date in YYYY-MM-DD format. """ stock = Ticker(ticker=symbol, session=self.session) price = stock.history(start=date, period="1d") return f"{price.iloc[0]['Close']:.4f}"
- src/mcp_yahoo_finance/server.py:204-218 (registration)Registers the get_stock_price_by_date tool (line 211) by generating its Tool object using generate_tool and including it in the list returned by the MCP list_tools handler.@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:226-228 (handler)Dispatches calls to the get_stock_price_by_date handler within the MCP call_tool implementation.case "get_stock_price_by_date": price = yf.get_stock_price_by_date(**args) return [TextContent(type="text", text=price)]
- src/mcp_yahoo_finance/utils.py:31-66 (schema)Generates the input schema for the tool dynamically from the function's signature, annotations, and Google-style docstring.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)