get_related_topics
Find related Wikipedia topics by analyzing article links and categories to discover connected subjects and expand research scope.
Instructions
Get topics related to a Wikipedia article based on links and categories.
Returns a list of related topics up to the specified limit.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| title | Yes | ||
| limit | No |
Implementation Reference
- wikipedia_mcp/server.py:211-220 (handler)MCP tool handler for 'get_related_topics'. Registers the tool via @server.tool() decorator and implements minimal logic by delegating to WikipediaClient.get_related_topics().@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. Returns a list of related topics up to the specified limit. """ 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 implementation of get_related_topics in WikipediaClient. Fetches the page, extracts links and categories, retrieves summaries for top links up to the limit, and falls back to categories.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()) 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 []
- wikipedia_mcp/server.py:211-220 (registration)The @server.tool() decorator registers 'get_related_topics' as an MCP tool on the FastMCP server.@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. Returns a list of related topics up to the specified limit. """ logger.info(f"Tool: Getting related topics for: {title}") related = wikipedia_client.get_related_topics(title, limit=limit) return {"title": title, "related_topics": related}