Skip to main content
Glama

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

Implementation Reference

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

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