Skip to main content
Glama

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

Implementation Reference

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

Other Tools

Related Tools

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