Skip to main content
Glama

get_sections

Retrieve the section structure of a Wikipedia article to quickly navigate content and understand its organization.

Instructions

Get the sections of a Wikipedia article.

Returns a dictionary with the article title and list of sections.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
titleYes

Implementation Reference

  • Registration and thin handler for the 'get_sections' MCP tool using @server.tool() decorator. Delegates core logic to wikipedia_client.get_sections.
    @server.tool()
    def get_sections(title: str) -> Dict[str, Any]:
        """
        Get the sections of a Wikipedia article.
    
        Returns a dictionary with the article title and list of sections.
        """
        logger.info(f"Tool: Getting sections for: {title}")
        sections = wikipedia_client.get_sections(title)
        return {"title": title, "sections": sections}
  • Primary handler logic for retrieving Wikipedia article sections using wikipedia-api library. Fetches page.sections and processes with _extract_sections helper.
    def get_sections(self, title: str) -> List[Dict[str, Any]]:
        """
        Get the sections of a Wikipedia article.
    
        Args:
            title: The title of the Wikipedia article.
    
        Returns:
            A list of sections.
        """
        try:
            page = self.wiki.page(title)
    
            if not page.exists():
                return []
    
            return self._extract_sections(page.sections)
        except Exception as e:
            logger.error(f"Error getting Wikipedia sections: {e}")
            return []
  • Recursive helper function to extract hierarchical section structure from Wikipedia page.sections, including title, level, text, and subsections.
    def _extract_sections(self, sections, level=0) -> List[Dict[str, Any]]:
        """
        Extract sections recursively.
    
        Args:
            sections: The sections to extract.
            level: The current section level.
    
        Returns:
            A list of sections.
        """
        result = []
        for section in sections:
            section_data = {
                "title": section.title,
                "level": level,
                "text": section.text,
                "sections": self._extract_sections(section.sections, level + 1),
            }
            result.append(section_data)
        return result

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