fetch_news_articles
Retrieve news articles from WorldNewsAPI by specifying search terms, date ranges, and result limits to gather relevant information.
Instructions
Fetches news articles from the WorldNewsAPI based on specified parameters
and returns them as a list of dictionaries.
Args:
query_text (str): The text to search for in news articles
earliest_date (str): Earliest publication date in YYYY-MM-DD format
latest_date (str): Latest publication date in YYYY-MM-DD format
max_results (int): Maximum number of results to return
Returns:
list: A list of dictionaries containing article details
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query_text | No | politics | |
| earliest_date | No | 2025-04-17 | |
| latest_date | No | 2025-04-23 | |
| max_results | No |
Implementation Reference
- server.py:22-95 (handler)The @mcp.tool() decorated function implementing fetch_news_articles. It fetches news articles from WorldNewsAPI based on query_text, date ranges, and max_results, paginating results and formatting them into a dictionary list.@mcp.tool() def fetch_news_articles( query_text="politics", earliest_date="2025-04-17", latest_date="2025-04-23", max_results=5, ) -> dict: """ Fetches news articles from the WorldNewsAPI based on specified parameters and returns them as a list of dictionaries. Args: query_text (str): The text to search for in news articles earliest_date (str): Earliest publication date in YYYY-MM-DD format latest_date (str): Latest publication date in YYYY-MM-DD format max_results (int): Maximum number of results to return Returns: list: A list of dictionaries containing article details """ try: newsapi_instance = worldnewsapi.NewsApi( worldnewsapi.ApiClient(newsapi_configuration) ) offset = 0 all_results = [] max_results = int(max_results) # Ensure max_results is an integer while len(all_results) < max_results: request_count = min( 100, max_results - len(all_results) ) # request 100 or the remaining number of articles response = newsapi_instance.search_news( text=query_text, earliest_publish_date=earliest_date, latest_publish_date=latest_date, sort="publish-time", sort_direction="desc", # min_sentiment=-0.8, # Ensuring this is a float # max_sentiment=0.8, # Ensuring this is a float offset=offset, number=request_count, ) # print(f"Retrieved {len(response.news)} articles. Offset: {offset}/{max_results}. " # f"Total available: {response.available}.") if len(response.news) == 0: break all_results.extend(response.news) offset += 100 # Convert API response objects to dictionaries articles_list = [] for article in all_results[:max_results]: # Use max_results here article_dict = { "title": article.title, "author": article.authors, "url": article.url, "text_preview": article.text[:80] + "..." if article.text else "", "full_text": article.text, "publish_date": article.publish_date, } articles_list.append(article_dict) return {"all_data": articles_list} except worldnewsapi.ApiException as e: # print(f"Exception when calling NewsApi->search_news: {e}") return {"error": str(e)}