getComments
Fetch and display comments for a specific Hacker News story by providing the story ID and optional comment limit. Integrates with Hacker News MCP Server for enhanced functionality.
Instructions
Get comments for a story
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | The maximum number of comments to fetch | |
| storyId | Yes | The ID of the story |
Input Schema (JSON Schema)
{
"properties": {
"limit": {
"default": 30,
"description": "The maximum number of comments to fetch",
"type": "number"
},
"storyId": {
"description": "The ID of the story",
"type": "number"
}
},
"required": [
"storyId"
],
"type": "object"
}
Implementation Reference
- src/index.ts:360-415 (handler)The handler function for the 'getComments' tool. Validates input, fetches the story to get top-level comment IDs, retrieves the comments via HN API, formats them, and returns a formatted text response listing the comments.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)Zod schema for validating the input parameters of the getComments tool: storyId (required) and optional limit.export const CommentsRequestSchema = z.object({ storyId: z.number().int().positive(), limit: z.number().int().min(1).max(100).default(30), });
- src/index.ts:124-138 (registration)Tool registration in the ListTools response, including name, description, and input schema matching the validation 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 HN comment data into a standardized Comment interface, used in the 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", }; }