getCommentTree
Retrieve nested comment threads for Hacker News stories to analyze discussions and follow conversations.
Instructions
Get a comment tree for a story
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| storyId | Yes | The ID of the story |
Implementation Reference
- src/index.ts:417-463 (handler)The handler for the 'getCommentTree' tool. Validates the input using CommentTreeRequestSchema, fetches the story with comments using algoliaApi.getStoryWithComments, recursively formats the comment tree into a textual representation with indentation, and returns it as text content.case "getCommentTree": { const validatedArgs = validateInput(CommentTreeRequestSchema, args); const { storyId } = validatedArgs; try { const data = await algoliaApi.getStoryWithComments(storyId); if (!data || !data.children || data.children.length === 0) { return { content: [ { type: "text", text: `No comments found for story ID: ${storyId}`, }, ], }; } const formatCommentTree = (comment: any, depth = 0): string => { const indent = " ".repeat(depth); let text = `${indent}Comment by ${comment.author} (ID: ${comment.id}):\n`; text += `${indent}${comment.text}\n\n`; if (comment.children) { text += comment.children .map((child: any) => formatCommentTree(child, depth + 1)) .join(""); } return text; }; const text = `Comment tree for Story ID: ${storyId}\n\n` + data.children .map((comment: any) => formatCommentTree(comment)) .join(""); return { content: [{ type: "text", text: text.trim() }], }; } catch (err) { const error = err as Error; throw new McpError( ErrorCode.InternalError, `Failed to fetch comment tree: ${error.message}` ); } }
- src/index.ts:139-149 (registration)Tool registration in the ListTools response, defining name, description, and input schema for getCommentTree.{ name: "getCommentTree", description: "Get a comment tree for a story", inputSchema: { type: "object", properties: { storyId: { type: "number", description: "The ID of the story" }, }, required: ["storyId"], }, },
- src/schemas/index.ts:58-60 (schema)Zod schema used for input validation of the getCommentTree tool, requiring a positive integer storyId.export const CommentTreeRequestSchema = z.object({ storyId: z.number().int().positive(), });