Skip to main content
Glama

search_news

Search for news articles using the Brave News API to find relevant information based on your query, returning titles, sources, dates, and descriptions.

Instructions

Search news using Brave News API

Args: query (str): The search query for news Returns: str: News search results including titles, sources, dates and descriptions

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYes

Implementation Reference

  • The handler function for the 'search_news' tool. It is registered using the @mcp.tool() decorator and executes the news search by calling the internal _do_news_search helper.
    @mcp.tool() def search_news(query: str) -> str: """Search news using Brave News API Args: query (str): The search query for news Returns: str: News search results including titles, sources, dates and descriptions """ return _do_news_search(query)
  • The core helper function implementing the news search logic using the Brave News API, including language detection, API call, and result formatting.
    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)}" @mcp.tool()

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mcp2everything/mcp2brave'

If you have feedback or need assistance with the MCP directory API, please join our Discord server