Skip to main content
Glama
devabdultech

Hacker News MCP Server

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
NameRequiredDescriptionDefault
storyIdYesThe ID of the story

Implementation Reference

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

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