summarize_article_for_query
Extract relevant Wikipedia article summaries focused on specific queries to quickly find targeted information within articles.
Instructions
Get a summary of a Wikipedia article tailored to a specific query.
The summary is a snippet around the query within the article text or summary. The max_length parameter controls the length of the snippet.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| title | Yes | ||
| query | Yes | ||
| max_length | No |
Implementation Reference
- wikipedia_mcp/server.py:155-170 (handler)Handler function for the summarize_article_for_query tool. Decorated with @server.tool() for registration. Calls WikipediaClient.summarize_for_query and formats the response.@server.tool() def summarize_article_for_query( title: str, query: str, max_length: Annotated[int, Field(title="Max Length")] = 250, ) -> Dict[str, Any]: """ Get a summary of a Wikipedia article tailored to a specific query. The summary is a snippet around the query within the article text or summary. The max_length parameter controls the length of the snippet. """ logger.info(f"Tool: Getting query-focused summary for article: {title}, query: {query}") summary = wikipedia_client.summarize_for_query(title, query, max_length=max_length) return {"title": title, "query": query, "summary": summary}
- wikipedia_mcp/server.py:155-155 (registration)The @server.tool() decorator registers the summarize_article_for_query function as an MCP tool.@server.tool()
- Core helper method in WikipediaClient that implements the query-focused summary by extracting a snippet of article text around occurrences of the query.def summarize_for_query(self, title: str, query: str, max_length: int = 250) -> str: """ Get a summary of a Wikipedia article tailored to a specific query. This is a simplified implementation that returns a snippet around the query. Args: title: The title of the Wikipedia article. query: The query to focus the summary on. max_length: The maximum length of the summary. Returns: A query-focused summary. """ try: page = self.wiki.page(title) if not page.exists(): return f"No Wikipedia article found for '{title}'." text_content = page.text query_lower = query.lower() text_lower = text_content.lower() start_index = text_lower.find(query_lower) if start_index == -1: # If query not found, return the beginning of the summary or article text summary_part = page.summary[:max_length] if not summary_part: summary_part = text_content[:max_length] return summary_part + "..." if len(summary_part) >= max_length else summary_part # Try to get context around the query context_start = max(0, start_index - (max_length // 2)) context_end = min(len(text_content), start_index + len(query) + (max_length // 2)) snippet = text_content[context_start:context_end] if len(snippet) > max_length: snippet = snippet[:max_length] return snippet + "..." if len(snippet) >= max_length or context_end < len(text_content) else snippet except Exception as e: logger.error(f"Error generating query-focused summary for '{title}': {e}") return f"Error generating query-focused summary for '{title}': {str(e)}"