getComments
Fetch comments for Hacker News stories to analyze discussions and user feedback. Retrieve threaded conversations by providing a story ID, with configurable limits for focused research.
Instructions
Get comments for a story
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| storyId | Yes | The ID of the story | |
| limit | No | The maximum number of comments to fetch |
Implementation Reference
- src/index.ts:360-415 (handler)Handler for getComments tool: validates input, fetches story kids, retrieves top limit comments, formats and returns them as text.case "getComments": { const validatedArgs = validateInput(CommentsRequestSchema, args); const { storyId, limit = 30 } = validatedArgs; try { const story = await hnApi.getItem(storyId); if (!story || !story.kids || story.kids.length === 0) { return { content: [ { type: "text", text: `No comments found for story ID: ${storyId}`, }, ], }; } const commentIds = story.kids.slice(0, limit); const comments = await hnApi.getItems(commentIds); const formattedComments = comments .filter((item) => item && item.type === "comment") .map(formatComment); if (formattedComments.length === 0) { return { content: [ { type: "text", text: `No comments found for story ID: ${storyId}`, }, ], }; } const text = `Comments for Story ID: ${storyId}\n\n` + formattedComments .map( (comment, index) => `${index + 1}. Comment by ${comment.by} (ID: ${ comment.id }):\n` + ` ${comment.text}\n\n` ) .join(""); return { content: [{ type: "text", text: text.trim() }], }; } catch (err) { const error = err as Error; throw new McpError( ErrorCode.InternalError, `Failed to fetch comments: ${error.message}` ); } }
- src/schemas/index.ts:53-56 (schema)Input schema for getComments tool using Zod validation.export const CommentsRequestSchema = z.object({ storyId: z.number().int().positive(), limit: z.number().int().min(1).max(100).default(30), });
- src/index.ts:123-138 (registration)Tool registration in ListTools response, including name, description, and input schema.{ name: "getComments", description: "Get comments for a story", inputSchema: { type: "object", properties: { storyId: { type: "number", description: "The ID of the story" }, limit: { type: "number", description: "The maximum number of comments to fetch", default: 30, }, }, required: ["storyId"], }, },
- src/models/comment.ts:11-21 (helper)Helper function to format raw comment data into structured Comment object, used in getComments handler.export function formatComment(item: any): Comment { return { id: item.id, text: item.text || "", by: item.by || "deleted", time: item.time, parent: item.parent, kids: item.kids || [], type: "comment", }; }