search_news_info
Search for news articles using keywords to retrieve titles, sources, dates, and descriptions from the Brave News API.
Instructions
使用Brave新闻API搜索新闻
参数:
query (str): 新闻搜索关键词
返回:
str: 新闻搜索结果,包含标题、来源、日期和描述
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes |
Implementation Reference
- mcp2brave.py:464-474 (handler)Handler function for the 'search_news_info' MCP tool. Registered via @mcp.tool() decorator. Takes a query string and delegates to the internal _do_news_search helper.@mcp.tool() def search_news_info(query: str) -> str: """使用Brave新闻API搜索新闻 参数: query (str): 新闻搜索关键词 返回: str: 新闻搜索结果,包含标题、来源、日期和描述 """ return _do_news_search(query)
- mcp2brave.py:356-417 (helper)Core helper function implementing the news search logic: constructs API request to Brave News API, parses response, formats results with title, source, date, URL, description. Handles language detection, errors.def _do_news_search(query: str, country: str = "all", search_lang: str = None) -> str: """Internal function to handle news search using Brave News API""" try: query = query.encode('utf-8').decode('utf-8') # 如果未指定语言,自动检测 if search_lang is None: search_lang = _detect_language(query) logger.debug(f"Detected language: {search_lang} for query: {query}") url = "https://api.search.brave.com/res/v1/news/search" headers = { "Accept": "application/json", "Accept-Encoding": "gzip", "X-Subscription-Token": API_KEY } params = { "q": query, "count": 10, "country": country, "search_lang": search_lang, "spellcheck": 1 } logger.debug(f"Searching news for query: {query}") response = requests.get(url, headers=headers, params=params) response.raise_for_status() data = response.json() # 处理新闻搜索结果 results = [] if 'results' in data: for news in data['results']: title = news.get('title', 'No title').encode('utf-8').decode('utf-8') url = news.get('url', 'No URL') description = news.get('description', 'No description').encode('utf-8').decode('utf-8') date = news.get('published_time', 'Unknown date') source = news.get('source', {}).get('name', 'Unknown source') news_item = [ f"- {title}", f" Source: {source}", f" Date: {date}", f" URL: {url}", f" Description: {description}\n" ] results.append("\n".join(news_item)) if not results: return "No news found for your query." return "News Results:\n\n" + "\n".join(results) except requests.exceptions.RequestException as e: logger.error(f"News API request error: {str(e)}") return f"Error searching news: {str(e)}" except Exception as e: logger.error(f"News search error: {str(e)}") logger.exception("Detailed error trace:") return f"Error searching news: {str(e)}"