get_related_topics
Discover related topics for any Wikipedia article by analyzing links and categories, enabling deeper exploration and context generation for research or content creation.
Instructions
Get topics related to a Wikipedia article based on links and categories.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | ||
| title | Yes |
Implementation Reference
- wikipedia_mcp/server.py:137-142 (handler)MCP tool handler decorated with @server.tool(), which registers and implements the tool by calling the WikipediaClient's method.@server.tool() def get_related_topics(title: str, limit: int = 10) -> Dict[str, Any]: """Get topics related to a Wikipedia article based on links and categories.""" logger.info(f"Tool: Getting related topics for: {title}") related = wikipedia_client.get_related_topics(title, limit=limit) return {"title": title, "related_topics": related}
- Core helper function in WikipediaClient that implements the logic to fetch related topics from links and categories of the given page.def get_related_topics(self, title: str, limit: int = 10) -> List[Dict[str, Any]]: """Get topics related to a Wikipedia article based on links and categories. Args: title: The title of the Wikipedia article. limit: Maximum number of related topics to return. Returns: A list of related topics. """ try: page = self.wiki.page(title) if not page.exists(): return [] # Get links from the page links = list(page.links.keys()) # Get categories categories = list(page.categories.keys()) # Combine and limit related = [] # Add links first for link in links[:limit]: link_page = self.wiki.page(link) if link_page.exists(): related.append( { "title": link, "summary": ( link_page.summary[:200] + "..." if len(link_page.summary) > 200 else link_page.summary ), "url": link_page.fullurl, "type": "link", } ) if len(related) >= limit: break # Add categories if we still have room remaining = limit - len(related) if remaining > 0: for category in categories[:remaining]: # Remove "Category:" prefix if present clean_category = category.replace("Category:", "") related.append({"title": clean_category, "type": "category"}) return related except Exception as e: logger.error(f"Error getting related topics: {e}") return []