getStoryWithComments
Retrieve a Hacker News story along with its associated comments by providing the story ID, enabling enhanced content review and discussion analysis.
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)The primary handler for the 'getStoryWithComments' tool within the CallToolRequest switch statement. It extracts the story ID, fetches data via algoliaApi, formats the story details and recursively formats the comment tree, then returns the response as formatted text content.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 ListToolsRequest handler, defining the tool name, description, and input schema (object with required 'id' number).{ 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 method in AlgoliaAPI class that fetches the story data including comments from the Algolia Hacker News API endpoint.async getStoryWithComments(storyId: number): Promise<any> { const response = await fetch(`${API_BASE_URL}/items/${storyId}`); return response.json(); }