summarize_article_for_query
Generate concise Wikipedia article summaries focused on specific queries using the MCP server, providing targeted insights for efficient information retrieval.
Instructions
Get a summary of a Wikipedia article tailored to a specific query.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| max_length | No | ||
| query | Yes | ||
| title | Yes |
Implementation Reference
- wikipedia_mcp/server.py:95-105 (handler)The FastMCP tool handler function 'summarize_article_for_query' that registers the tool and delegates execution to the WikipediaClient's summarize_for_query method.@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.""" logger.info(f"Tool: Getting query-focused summary for article: {title}, query: {query}") # Assuming wikipedia_client has a method like summarize_for_query summary = wikipedia_client.summarize_for_query(title, query, max_length=max_length) return {"title": title, "query": query, "summary": summary}
- The supporting utility method in WikipediaClient that implements the core logic for generating a query-tailored summary by locating the query in the article text and extracting surrounding context.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)}"