Skip to main content
Glama
devabdultech

Hacker News MCP Server

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
NameRequiredDescriptionDefault
storyIdYesThe ID of the story
limitNoThe maximum number of comments to fetch

Implementation Reference

  • 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}`
        );
      }
    }
  • 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"],
      },
    },
  • 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",
      };
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/devabdultech/hn-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server