Skip to main content
Glama

extract_key_facts

Extract structured key facts from Wikipedia articles to quickly identify essential information, optionally focusing on specific topics within the article.

Instructions

Extract key facts from a Wikipedia article, optionally focused on a topic.

Returns a dictionary containing a list of facts.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
titleYes
topic_within_articleNo
countNo

Implementation Reference

  • The primary handler function for the 'extract_key_facts' MCP tool, registered via @server.tool(). Defines the input schema through type hints and Annotated fields. Executes the tool by calling WikipediaClient.extract_facts and returning a formatted dictionary response.
    @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.
    
        Returns a dictionary containing a list of facts.
        """
        logger.info(f"Tool: Extracting key facts for article: {title}, topic: {topic_within_article}")
        topic = topic_within_article if topic_within_article.strip() else None
        facts = wikipedia_client.extract_facts(title, topic, count=count)
        return {"title": title, "topic_within_article": topic_within_article, "facts": facts}
  • Supporting utility method in WikipediaClient that implements the core fact extraction logic. Retrieves article content (summary or specific section), splits into sentences, and returns the top 'count' facts. Called by the tool handler.
    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)
            sentences = [s.strip() for s in text_to_process.split(".") if s.strip()]
    
            facts: List[str] = []
            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)}"]

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Rudra-ravi/wikipedia-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server