getCommentTree
Retrieve a structured comment thread for any Hacker News story by providing the story ID. Use this tool to access and analyze nested discussions within the Hacker News platform.
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)Main handler logic for the 'getCommentTree' tool: validates input using CommentTreeRequestSchema, fetches story with comments using algoliaApi, recursively formats the comment tree, and returns formatted text response.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/schemas/index.ts:58-60 (schema)Zod schema defining the input for getCommentTree: requires a positive integer storyId.export const CommentTreeRequestSchema = z.object({ storyId: z.number().int().positive(), });
- src/index.ts:139-149 (registration)Tool registration in ListTools handler, specifying name, description, and input schema matching CommentTreeRequestSchema.{ 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/api/algolia.ts:56-59 (helper)Helper function in AlgoliaAPI that fetches the full story item including comment tree from HN Algolia API.async getStoryWithComments(storyId: number): Promise<any> { const response = await fetch(`${API_BASE_URL}/items/${storyId}`); return response.json(); }
- src/index.ts:434-445 (helper)Local recursive helper function used in handler to format the comment tree with indentation based on depth.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; };