get_related_pages
Retrieve related pages linked from a Grokipedia article by providing its slug. Optionally limit the number of results, up to 50.
Instructions
Get pages that are linked from the specified page.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| slug | Yes | Unique slug identifier of page to find related pages for | |
| limit | No | Maximum number of related pages to return (default: 10) |
Implementation Reference
- grokipedia_mcp/server.py:385-473 (handler)The async function that implements the get_related_pages tool. It fetches a page by slug using the API client, extracts linked pages, and returns them formatted as text output with structured content. Handles not-found, bad request, network, and API errors.
async def get_related_pages( slug: Annotated[str, Field(description="Unique slug identifier of page to find related pages for")], limit: Annotated[int, Field(description="Maximum number of related pages to return (default: 10)", ge=1, le=50)] = 10, ctx: Context[ServerSession, AppContext] | None = None, ) -> CallToolResult: """Get pages that are linked from the specified page.""" if ctx is None: raise ValueError("Context is required") await ctx.debug(f"Fetching related pages for: '{slug}' (limit={limit})") try: client = ctx.request_context.lifespan_context.client result = await client.get_page(slug=slug, include_content=False) if not result.found or result.page is None: await ctx.warning(f"Page not found: '{slug}'") raise ValueError(f"Page not found: {slug}") page = result.page linked_pages = page.linked_pages or [] total_count = len(linked_pages) related = linked_pages[:limit] if limit else linked_pages is_limited = limit and total_count > limit await ctx.info(f"Found {len(related)} of {total_count} related pages for: '{page.title}'") if not linked_pages: text_output = f"# {page.title}\n\nNo related pages found." structured = { "slug": page.slug, "title": page.title, "related_pages": [], "total_count": 0, "returned_count": 0, } else: header = f"# {page.title}\n\n" if is_limited: header += f"Showing {len(related)} of {total_count} related pages:\n\n" else: header += f"Found {total_count} related pages:\n\n" text_parts = [header] for i, rel_page in enumerate(related, 1): if isinstance(rel_page, dict): title = rel_page.get("title", "Unknown") slug_val = rel_page.get("slug", "") else: title = str(rel_page) slug_val = "" text_parts.append(f"{i}. {title}") if slug_val: text_parts.append(f" Slug: {slug_val}") text_parts.append("") if is_limited: text_parts.append(f"... and {total_count - len(related)} more") text_output = "\n".join(text_parts) structured = { "slug": page.slug, "title": page.title, "related_pages": related, "total_count": total_count, "returned_count": len(related), } if is_limited: structured["_limited"] = True return CallToolResult( content=[TextContent(type="text", text=text_output)], structuredContent=structured, ) except GrokipediaNotFoundError as e: await ctx.error(f"Page not found: {e}") raise ValueError(f"Page not found: {slug}") from e except GrokipediaBadRequestError as e: await ctx.error(f"Bad request: {e}") raise ValueError(f"Invalid page slug: {e}") from e except GrokipediaNetworkError as e: await ctx.error(f"Network error: {e}") raise RuntimeError(f"Failed to connect to Grokipedia API: {e}") from e except GrokipediaAPIError as e: await ctx.error(f"API error: {e}") raise RuntimeError(f"Grokipedia API error: {e}") from e - grokipedia_mcp/server.py:386-388 (schema)Input parameter definitions: slug (string required) and limit (int, default 10, range 1-50) with Pydantic Field annotations.
slug: Annotated[str, Field(description="Unique slug identifier of page to find related pages for")], limit: Annotated[int, Field(description="Maximum number of related pages to return (default: 10)", ge=1, le=50)] = 10, ctx: Context[ServerSession, AppContext] | None = None, - grokipedia_mcp/server.py:378-384 (registration)The @mcp.tool() decorator that registers get_related_pages as an MCP tool with readOnlyHint, destructiveHint=False, and idempotentHint=True annotations.
@mcp.tool( annotations=ToolAnnotations( readOnlyHint=True, destructiveHint=False, idempotentHint=True ) )