yahoo_finance_search
Retrieve stock data and financial insights from Yahoo Finance using a specific symbol and time period. Integrates with Agentic AI to provide accurate, context-aware responses for market analysis.
Instructions
Get stock information from Yahoo Finance.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| period | No | 1mo | |
| symbol | Yes |
Implementation Reference
- server.py:129-182 (handler)The @mcp.tool()-decorated async function that implements the core logic of the yahoo_finance_search tool. It validates the period, makes an API request to Yahoo Finance for stock chart data using the symbol and period parameters, parses the response to extract metadata and latest trading information, and formats it into a readable string response.@mcp.tool() async def yahoo_finance_search(symbol: str, period: str = "1mo") -> str: """Get stock information from Yahoo Finance.""" try: valid_periods = ["1d", "5d", "1mo", "3mo", "6mo", "1y", "5y"] if period not in valid_periods: return f"Invalid period. Must be one of: {', '.join(valid_periods)}" params = { "symbol": symbol, "range": period, "interval": "1d", "includePrePost": "false" } data = await make_api_request(f"{YAHOO_FINANCE_BASE}{symbol}", params=params) if not data or "chart" not in data or not data["chart"]["result"]: return f"Could not retrieve data for symbol {symbol}" result = data["chart"]["result"][0] meta = result["meta"] indicators = result["indicators"]["quote"][0] timestamps = result["timestamp"] dates = [datetime.fromtimestamp(ts).strftime('%Y-%m-%d') for ts in timestamps] latest_idx = -1 latest_date = dates[latest_idx] latest_open = indicators["open"][latest_idx] latest_high = indicators["high"][latest_idx] latest_low = indicators["low"][latest_idx] latest_close = indicators["close"][latest_idx] latest_volume = indicators["volume"][latest_idx] response = [ f"Stock: {meta['symbol']} ({meta['exchangeName']})", f"Currency: {meta['currency']}", f"Current Price: {meta['regularMarketPrice']}", f"Previous Close: {meta['chartPreviousClose']}", "\nLatest Trading Day:", f"Date: {latest_date}", f"Open: {latest_open}", f"High: {latest_high}", f"Low: {latest_low}", f"Close: {latest_close}", f"Volume: {latest_volume}" ] return "\n".join(response) except Exception as e: logger.error(f"Error in yahoo_finance_search: {e}") return f"Failed to retrieve finance data for {symbol} due to an internal error."
- server.py:129-129 (registration)Registration of the yahoo_finance_search tool using the @mcp.tool() decorator on the FastMCP instance.@mcp.tool()
- server.py:130-131 (schema)Input schema defined by function parameters: symbol: str (required), period: str = "1mo" (optional), with docstring describing the tool's purpose and valid periods.async def yahoo_finance_search(symbol: str, period: str = "1mo") -> str: """Get stock information from Yahoo Finance."""