get_news
Retrieve stock-related news for a specified symbol using Yahoo Finance data. Stay updated on company developments and market trends directly through structured input.
Instructions
Get news for a given stock symbol.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| symbol | Yes | Stock symbol in Yahoo Finance format. |
Implementation Reference
- src/mcp_yahoo_finance/server.py:190-197 (handler)Core implementation of the get_news tool handler in the YahooFinance class. Fetches news headlines for the stock symbol using yfinance.Ticker.news and serializes to JSON.def get_news(self, symbol: str) -> str: """Get news for a given stock symbol. Args: symbol (str): Stock symbol in Yahoo Finance format. """ stock = Ticker(ticker=symbol, session=self.session) return json.dumps(stock.news, indent=2)
- src/mcp_yahoo_finance/utils.py:31-65 (schema)The generate_tool utility generates the JSON schema for the 'get_news' tool (and others) from the Python function signature, type annotations, and 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)
- src/mcp_yahoo_finance/server.py:204-218 (registration)Registration of the get_news tool in the MCP server's list_tools() handler via generate_tool(yf.get_news).@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), ]
- Dispatch logic in the call_tool() handler that invokes the get_news method and formats the response as TextContent.case "get_news": price = yf.get_news(**args) return [TextContent(type="text", text=price)]