get_stock_price_by_date
Retrieve historical stock prices for specific dates using Yahoo Finance data. Enter a stock symbol and date to get the closing price for that trading day.
Instructions
Get the stock price for a given stock symbol on a specific date.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| symbol | Yes | Stock symbol in Yahoo Finance format. | |
| date | Yes | The date in YYYY-MM-DD format. |
Implementation Reference
- src/mcp_yahoo_finance/server.py:72-81 (handler)The handler function in the YahooFinance class that implements the tool logic: creates a yfinance Ticker, fetches 1-day history starting from the given date, and returns the closing price formatted to 4 decimal places.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:211-211 (registration)Registers the get_stock_price_by_date tool in the MCP server's list_tools() by passing the handler method to generate_tool.generate_tool(yf.get_stock_price_by_date),
- src/mcp_yahoo_finance/server.py:226-228 (registration)In the server's call_tool() dispatcher, matches the tool name and invokes the handler with arguments, returning the result as TextContent.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-65 (helper)Helper function that generates the MCP Tool object (including schema inferred from function signature, docstring, and type annotations) used to register all tools including get_stock_price_by_date.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)