Skip to main content
Glama

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

Implementation Reference

  • 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"], }, },
  • 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(); }
  • Usage of the helper in another tool handler 'getCommentTree'.
    const data = await algoliaApi.getStoryWithComments(storyId);

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