getComment
Retrieve a specific Hacker News comment by entering its unique ID. Enables direct access to individual discussion points within the platform, facilitating focused analysis or reference.
Instructions
Get a single comment by ID
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes | The ID of the comment |
Input Schema (JSON Schema)
{
"properties": {
"id": {
"description": "The ID of the comment",
"type": "number"
}
},
"required": [
"id"
],
"type": "object"
}
Implementation Reference
- src/index.ts:338-358 (handler)The main handler for the 'getComment' tool. Validates input using CommentRequestSchema, fetches the comment item via hnApi.getItem(id), checks if it's a comment, formats it with formatComment, constructs a text response, and returns it.case "getComment": { const validatedArgs = validateInput(CommentRequestSchema, args); const { id } = validatedArgs; const item = await hnApi.getItem(id); if (!item || item.type !== "comment") { throw new McpError( ErrorCode.InvalidParams, `Comment with ID ${id} not found` ); } const comment = formatComment(item); const text = `Comment ID: ${comment.id}\n` + `Comment by ${comment.by}:\n` + `${comment.text}\n` + `Parent ID: ${comment.parent}\n`; return { content: [{ type: "text", text: text.trim() }], }; }
- src/schemas/index.ts:49-51 (schema)Zod schema for validating the input parameters of the getComment tool (requires positive integer ID). Used in the handler via validateInput.export const CommentRequestSchema = z.object({ id: z.number().int().positive(), });
- src/index.ts:112-122 (registration)Registration of the 'getComment' tool in the ListTools response, including name, description, and JSON schema for input validation.{ name: "getComment", description: "Get a single comment by ID", inputSchema: { type: "object", properties: { id: { type: "number", description: "The ID of the comment" }, }, required: ["id"], }, },
- src/models/comment.ts:11-21 (helper)Helper function that formats a raw Hacker News item object into a structured Comment interface, used in the getComment handler.export function formatComment(item: any): Comment { return { id: item.id, text: item.text || "", by: item.by || "deleted", time: item.time, parent: item.parent, kids: item.kids || [], type: "comment", }; }
- src/api/hn.ts:9-15 (helper)Core API helper in HackerNewsAPI class that fetches a single item (story or comment) by ID from the Hacker News Firebase API endpoint. Called by the getComment handler./** * Fetch an item by ID */ async getItem(id: number): Promise<any> { const response = await fetch(`${API_BASE_URL}/item/${id}.json`); return response.json(); }