getStoryWithComments
Retrieve Hacker News stories along with their associated comments using story IDs to analyze discussions and content in context.
Instructions
Get a story with its comments
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes | The ID of the story |
Implementation Reference
- src/index.ts:229-274 (handler)Main handler for the 'getStoryWithComments' tool. Fetches story data using algoliaApi and formats the story details with a recursive comment tree representation.case "getStoryWithComments": { const { id } = args as { id: number }; try { const data = await algoliaApi.getStoryWithComments(id); if (!data || !data.title) { throw new McpError( ErrorCode.InvalidParams, `Story with ID ${id} not found` ); } 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`; text += `${indent}Posted: ${comment.created_at}\n\n`; if (comment.children) { text += comment.children .map((child: any) => formatCommentTree(child, depth + 1)) .join(""); } return text; }; const text = `Story ID: ${data.id}\n` + `Title: ${data.title}\n` + `URL: ${data.url || "(text post)"}\n` + `Points: ${data.points} | Author: ${data.author}\n\n` + `Comments:\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 story: ${error.message}` ); } }
- src/index.ts:80-90 (registration)Tool registration in the ListTools handler, including name, description, and input schema.{ name: "getStoryWithComments", description: "Get a story with its comments", inputSchema: { type: "object", properties: { id: { type: "number", description: "The ID of the story" }, }, required: ["id"], }, },
- src/api/algolia.ts:56-59 (helper)Helper function in AlgoliaAPI that fetches the story item (including comments) from the Hacker News Algolia API endpoint.async getStoryWithComments(storyId: number): Promise<any> { const response = await fetch(`${API_BASE_URL}/items/${storyId}`); return response.json(); }
- src/index.ts:421-422 (helper)Usage of the helper in another tool handler 'getCommentTree'.const data = await algoliaApi.getStoryWithComments(storyId);