get_news
Fetch news articles for any stock symbol using Yahoo Finance data. Retrieve relevant updates to stay informed on market trends and company developments.
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:155-162 (handler)The core handler function that fetches the latest news for a given stock symbol using yfinance's Ticker.news property and serializes it 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/server.py:231-231 (registration)Registers the 'get_news' tool in the MCP server's list_tools() by wrapping the handler function with generate_tool, which also generates the input schema from the function signature and docstring.generate_tool(yf.get_news),
- src/mcp_yahoo_finance/server.py:264-266 (handler)Dispatches execution of the 'get_news' tool call to the underlying YahooFinance.get_news handler and formats the result as MCP TextContent.case "get_news": price = yf.get_news(**args) return [TextContent(type="text", text=price)]
- src/mcp_yahoo_finance/utils.py:31-65 (helper)Helper function used to generate the Tool schema (including input schema) for get_news and other tools from Python function introspection and docstring parsing.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)