get_news_by_keyword
Retrieve news articles matching a keyword from Google News, with options to set lookback period, number of results, and request full data or summaries.
Instructions
Find articles by keyword using Google News.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| keyword | Yes | Search term to find articles. | |
| period | No | Number of days to look back for articles. | |
| max_results | No | Maximum number of results to return. | |
| full_data | No | Return full data for each article. If False a summary should be created by setting the summarize flag | |
| summarize | No | Generate a summary of the article, will first try LLM Sampling but if unavailable will use nlp |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- Core async function that searches Google News by keyword, configures gnews client, fetches articles and processes them via process_gnews_articles.
async def get_news_by_keyword( keyword: str, period=7, max_results: int = 10, nlp: bool = True, report_progress: Optional[ProgressCallback] = None, ) -> list[newspaper.Article]: """ Find articles by keyword using Google News. """ google_news.period = f"{period}d" google_news.max_results = max_results gnews_articles = google_news.get_news(keyword) if not gnews_articles: logger.debug(f"No articles found for keyword '{keyword}' in the last {period} days.") return [] return await process_gnews_articles(gnews_articles, nlp=nlp, report_progress=report_progress) - MCP tool handler for get_news_by_keyword, registered with @mcp.tool decorator. Accepts keyword, period, max_results, full_data, summarize params, calls news.get_news_by_keyword, optionally summarizes, and returns ArticleOut list.
@mcp.tool( description=news.get_news_by_keyword.__doc__, tags={"news", "articles", "keyword"}, ) async def get_news_by_keyword( ctx: Context, keyword: Annotated[str, Field(description="Search term to find articles.")], period: Annotated[int, Field(description="Number of days to look back for articles.", ge=1)] = 7, max_results: Annotated[int, Field(description="Maximum number of results to return.", ge=1)] = 10, full_data: Annotated[ bool, Field( description="Return full data for each article. If False a summary should be created by setting the summarize flag" ), ] = False, summarize: Annotated[ bool, Field( description="Generate a summary of the article, will first try LLM Sampling but if unavailable will use nlp" ), ] = True, ) -> list[ArticleOut]: set_newspaper_article_fields(full_data) articles = await news.get_news_by_keyword( keyword=keyword, period=period, max_results=max_results, nlp=False, report_progress=ctx.report_progress, ) if summarize: await summarize_articles(articles, ctx) await ctx.report_progress(progress=len(articles), total=len(articles)) return [ArticleOut(**a.to_json(False)) for a in articles] - src/google_news_trends_mcp/server.py:161-163 (registration)Registration of the get_news_by_keyword tool via @mcp.tool decorator with description=news.get_news_by_keyword.__doc__ and tags={'news', 'articles', 'keyword'}.
@mcp.tool( description=news.get_news_by_keyword.__doc__, tags={"news", "articles", "keyword"}, - ArticleOut Pydantic model defining the output schema for article results returned by get_news_by_keyword.
class ArticleOut(BaseModelClean): title: Annotated[str, Field(description="Title of the article.")] url: Annotated[str, Field(description="Original article URL.")] read_more_link: Annotated[Optional[str], Field(description="Link to read more about the article.")] = None language: Annotated[Optional[str], Field(description="Language code of the article.")] = None meta_img: Annotated[Optional[str], Field(description="Meta image URL.")] = None movies: Annotated[Optional[list[str]], Field(description="List of movie URLs or IDs.")] = None meta_favicon: Annotated[Optional[str], Field(description="Favicon URL from meta data.")] = None meta_site_name: Annotated[Optional[str], Field(description="Site name from meta data.")] = None authors: Annotated[Optional[list[str]], Field(description="list of authors.")] = None publish_date: Annotated[Optional[str], Field(description="Publish date in ISO format.")] = None top_image: Annotated[Optional[str], Field(description="URL of the top image.")] = None images: Annotated[Optional[list[str]], Field(description="list of image URLs.")] = None text: Annotated[Optional[str], Field(description="Full text of the article.")] = None summary: Annotated[Optional[str], Field(description="Summary of the article.")] = None keywords: Annotated[Optional[list[str]], Field(description="Extracted keywords.")] = None tags: Annotated[Optional[list[str]], Field(description="Tags for the article.")] = None meta_keywords: Annotated[Optional[list[str]], Field(description="Meta keywords from the article.")] = None meta_description: Annotated[Optional[str], Field(description="Meta description from the article.")] = None canonical_link: Annotated[Optional[str], Field(description="Canonical link for the article.")] = None meta_data: Annotated[Optional[dict[str, str | int]], Field(description="Meta data dictionary.")] = None meta_lang: Annotated[Optional[str], Field(description="Language of the article.")] = None source_url: Annotated[Optional[str], Field(description="Source URL if different from original.")] = None - CLI command registration for get_news_by_keyword using Click, allowing command-line invocation with keyword, period, max_results, and --no-nlp options.
@cli.command(help=get_news_by_keyword.__doc__) @click.argument("keyword") @click.option("--period", type=int, default=7, help="Period in days to search for articles.") @click.option( "--max-results", "max_results", type=int, default=10, help="Maximum number of results to return.", ) @click.option("--no-nlp", is_flag=True, default=False, help="Disable NLP processing for articles.") def keyword(keyword, period, max_results, no_nlp): @BrowserManager() async def _keyword(): articles = await get_news_by_keyword(keyword, period=period, max_results=max_results, nlp=not no_nlp) print_articles(articles) logger.info(f"Found {len(articles)} articles for keyword '{keyword}'.") asyncio.run(_keyword())