hn_top
Fetch top-ranked stories from Hacker News. Control the number of stories to retrieve with a limit parameter between 1 and 50, defaulting to 10.
Instructions
Get the top-ranked stories from Hacker News
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Number of stories to fetch (1-50, default: 10) |
Implementation Reference
- index.ts:162-176 (registration)Tool 'hn_top' is registered in the ListToolsRequestSchema handler with its name, description, and input schema.
{ name: "hn_top", description: "Get the top-ranked stories from Hacker News", inputSchema: { type: "object", properties: { limit: { type: "number", description: "Number of stories to fetch (1-50, default: 10)", minimum: 1, maximum: 50, default: 10 } } } - index.ts:165-176 (schema)Input schema for hn_top: optional 'limit' number (1-50, default 10).
inputSchema: { type: "object", properties: { limit: { type: "number", description: "Number of stories to fetch (1-50, default: 10)", minimum: 1, maximum: 50, default: 10 } } } - index.ts:257-278 (handler)Handler for 'hn_top' tool: calls api.getTopStories(limit), formats results, stores them in lastStoriesList, and returns text content.
if (name === "hn_top") { const limit = typeof args?.limit === 'number' ? args.limit : 10; const stories = await api.getTopStories(limit); const formattedStories = stories.map(story => ({ id: story.id, title: story.title, by: story.by, time: api.formatTime(story.time), url: story.url, score: story.score, commentsCount: story.kids?.length || 0 })); lastStoriesList = formattedStories; return { content: [ { type: "text", text: formatStoriesAsText(formattedStories) } ] }; } - index.ts:61-72 (helper)HackerNewsAPI.getTopStories() fetches top story IDs from /topstories.json, retrieves details, and filters for story type.
async getTopStories(limit = 50): Promise<Story[]> { try { const response = await axios.get(`${baseUrl}/topstories.json`); const storyIds = response.data || []; const storyPromises = storyIds.slice(0, limit).map((id: number) => this.getItemDetails(id)); const stories = await Promise.all(storyPromises); return stories.filter((story): story is Story => story !== null && story.type === 'story'); } catch (error) { console.error('Error fetching top stories:', error); return []; } }