Skip to main content
Glama
devabdultech

Hacker News MCP Server

getStory

Retrieve a specific Hacker News story by its unique ID to access content, comments, and details through the Model Context Protocol.

Instructions

Get a single story by ID

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
idYesThe ID of the story

Implementation Reference

  • Main handler logic for the 'getStory' tool: fetches story data using hnApi.getItem, formats it with formatStory, constructs a textual response.
    case "getStory": {
      const { id } = args as { id: number };
      const item = await hnApi.getItem(id);
      if (!item || item.type !== "story") {
        throw new McpError(
          ErrorCode.InvalidParams,
          `Story with ID ${id} not found`
        );
      }
      const story = formatStory(item);
      const text =
        `Story ID: ${story.id}\n` +
        `Title: ${story.title}\n` +
        `URL: ${story.url || "(text post)"}\n` +
        `Points: ${story.score} | Author: ${story.by} | Comments: ${story.descendants}\n` +
        (story.text ? `\nContent:\n${story.text}\n` : "");
    
      return {
        content: [{ type: "text", text: text.trim() }],
      };
    }
  • src/index.ts:69-79 (registration)
    Tool registration in ListTools handler, defining name, description, and input schema for 'getStory'.
    {
      name: "getStory",
      description: "Get a single story by ID",
      inputSchema: {
        type: "object",
        properties: {
          id: { type: "number", description: "The ID of the story" },
        },
        required: ["id"],
      },
    },
  • hnApi.getItem method: fetches a single Hacker News item by ID from the official Firebase API.
    async getItem(id: number): Promise<any> {
      const response = await fetch(`${API_BASE_URL}/item/${id}.json`);
      return response.json();
    }
  • formatStory function: transforms raw HN item data into a structured Story object used in the handler.
    export function formatStory(item: any): Story {
      return {
        id: item.id,
        title: item.title,
        url: item.url,
        text: item.text,
        by: item.by,
        score: item.score || 0,
        time: item.time,
        descendants: item.descendants || 0,
        kids: item.kids || [],
        type: "story",
      };
    }
  • Input schema definition for the 'getStory' tool, specifying the required 'id' parameter.
    inputSchema: {
      type: "object",
      properties: {
        id: { type: "number", description: "The ID of the story" },
      },
      required: ["id"],
    },

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