wikipedia_get_related_topics
Find related topics from a Wikipedia article using its links and categories. Specify a title and optional limit to get relevant results.
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
| Name | Required | Description | Default |
|---|---|---|---|
| title | Yes | ||
| limit | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| title | Yes | ||
| related_topics | Yes |
Implementation Reference
- wikipedia_mcp/server.py:321-330 (handler)The handler function for the 'get_related_topics' tool. It delegates to wikipedia_client.get_related_topics and returns the title with related topics.
@register_tool("get_related_topics", model_output_schema(RelatedTopicsResponse)) def get_related_topics(title: str, limit: int = 10): """ 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("Tool: Getting related topics for: %s", title) related = wikipedia_client.get_related_topics(title, limit=limit) return {"title": title, "related_topics": related} - wikipedia_mcp/schemas.py:79-81 (schema)Pydantic schema 'RelatedTopicsResponse' defining the output type for the get_related_topics tool (title + list of related topics dicts).
class RelatedTopicsResponse(MCPBaseModel): title: str related_topics: list[dict[str, Any]] - wikipedia_mcp/server.py:160-175 (registration)The register_tool decorator that registers the tool under both the base name ('get_related_topics') and the prefixed name ('wikipedia_get_related_topics').
def register_tool(name: str, output_schema: dict[str, Any]): def decorator(func): server.tool( func, name=name, annotations=_READ_ONLY_TOOL_ANNOTATIONS, output_schema=output_schema, ) server.tool( func, name=f"wikipedia_{name}", annotations=_READ_ONLY_TOOL_ANNOTATIONS, output_schema=output_schema, ) return func - Core helper function that retrieves related topics by fetching page links and categories from the Wikipedia API, with support for a limit parameter.
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 - LRU cache wrapping applied to get_related_topics method for performance optimization.
self.get_related_topics = functools.lru_cache(maxsize=128)( # type: ignore[method-assign] self.get_related_topics )