extract_key_facts
Extract key facts from a Wikipedia article, optionally filtered by a specific topic, to quickly obtain essential information for research or analysis.
Instructions
Extract key facts from a Wikipedia article, optionally focused on a topic.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| count | No | ||
| title | Yes | ||
| topic_within_article | No |
Implementation Reference
- wikipedia_mcp/server.py:119-135 (handler)The primary handler function for the 'extract_key_facts' tool, registered via @server.tool(). It processes input parameters and delegates to WikipediaClient.extract_facts for the core logic.@server.tool() def extract_key_facts( title: str, topic_within_article: Annotated[str, Field(title="Topic Within Article")] = "", count: int = 5, ) -> Dict[str, Any]: """Extract key facts from a Wikipedia article, optionally focused on a topic.""" logger.info(f"Tool: Extracting key facts for article: {title}, topic: {topic_within_article}") # Convert empty string to None for backward compatibility topic = topic_within_article if topic_within_article.strip() else None # Assuming wikipedia_client has a method like extract_facts facts = wikipedia_client.extract_facts(title, topic, count=count) return { "title": title, "topic_within_article": topic_within_article, "facts": facts, }
- The helper method in WikipediaClient that performs the actual fact extraction by locating relevant article text (summary or topic section) and splitting it into the first N sentences as facts.def extract_facts(self, title: str, topic_within_article: Optional[str] = None, count: int = 5) -> List[str]: """ Extract key facts from a Wikipedia article. This is a simplified implementation returning the first few sentences of the summary or a relevant section if topic_within_article is provided. Args: title: The title of the Wikipedia article. topic_within_article: Optional topic/section to focus fact extraction. count: The number of facts to extract. Returns: A list of key facts (strings). """ try: page = self.wiki.page(title) if not page.exists(): return [f"No Wikipedia article found for '{title}'."] text_to_process = "" if topic_within_article: # Try to find the section text def find_section_text_recursive(sections_list, target_title): for sec in sections_list: if sec.title.lower() == target_title.lower(): return sec.text found_in_subsection = find_section_text_recursive(sec.sections, target_title) if found_in_subsection: return found_in_subsection return None section_text = find_section_text_recursive(page.sections, topic_within_article) if section_text: text_to_process = section_text else: # Fallback to summary if specific topic section not found text_to_process = page.summary else: text_to_process = page.summary if not text_to_process: return ["No content found to extract facts from."] # Basic sentence splitting (can be improved with NLP libraries like nltk or spacy) sentences = [s.strip() for s in text_to_process.split(".") if s.strip()] facts = [] for sentence in sentences[:count]: if sentence: # Ensure not an empty string after strip facts.append(sentence + ".") # Add back the period return facts if facts else ["Could not extract facts from the provided text."] except Exception as e: logger.error(f"Error extracting key facts for '{title}': {e}") return [f"Error extracting key facts for '{title}': {str(e)}"]
- wikipedia_mcp/server.py:119-135 (registration)The @server.tool() decorator registers the extract_key_facts function as an MCP tool.@server.tool() def extract_key_facts( title: str, topic_within_article: Annotated[str, Field(title="Topic Within Article")] = "", count: int = 5, ) -> Dict[str, Any]: """Extract key facts from a Wikipedia article, optionally focused on a topic.""" logger.info(f"Tool: Extracting key facts for article: {title}, topic: {topic_within_article}") # Convert empty string to None for backward compatibility topic = topic_within_article if topic_within_article.strip() else None # Assuming wikipedia_client has a method like extract_facts facts = wikipedia_client.extract_facts(title, topic, count=count) return { "title": title, "topic_within_article": topic_within_article, "facts": facts, }
- wikipedia_mcp/server.py:120-122 (schema)The function signature defines the input schema using type annotations and Pydantic Field for the tool parameters.def extract_key_facts( title: str, topic_within_article: Annotated[str, Field(title="Topic Within Article")] = "",