get_sections
Retrieve specific sections of a Wikipedia article by title to access targeted information directly, facilitating efficient data extraction and analysis.
Instructions
Get the sections of a Wikipedia article.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| title | Yes |
Implementation Reference
- wikipedia_mcp/server.py:144-149 (handler)MCP tool handler and registration for 'get_sections'. This function is decorated with @server.tool(), making it the primary entry point for the tool in the FastMCP server. It delegates the core logic to WikipediaClient.get_sections() and returns a formatted dictionary response.@server.tool() def get_sections(title: str) -> Dict[str, Any]: """Get the sections of a Wikipedia article.""" logger.info(f"Tool: Getting sections for: {title}") sections = wikipedia_client.get_sections(title) return {"title": title, "sections": sections}
- Core helper implementation of get_sections in the WikipediaClient class. Fetches the Wikipedia page using wikipediaapi.Wikipedia.page() and extracts sections using the recursive _extract_sections method.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 utility _extract_sections used by get_sections to build a hierarchical list of sections with titles, levels, text, and subsections from the Wikipedia page object.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