hn_top
Fetch the top-ranked stories from Hacker News with customizable limits, ensuring you stay updated on trending tech and development topics efficiently.
Instructions
Get the top-ranked stories from Hacker News
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Number of stories to fetch (1-50, default: 10) |
Implementation Reference
- index.ts:257-277 (handler)Handler logic for the 'hn_top' tool: parses limit argument, fetches top stories via HackerNewsAPI, formats them, stores in lastStoriesList, and returns formatted text response.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:162-177 (schema)Input schema and metadata registration for the 'hn_top' tool within the listTools response.{ 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:61-72 (helper)Core helper method in HackerNewsAPI that fetches top story IDs from HN API and resolves full story details.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 []; } }
- index.ts:391-406 (helper)Helper function to format the list of stories into a numbered, readable text output.function formatStoriesAsText(stories: FormattedStory[]): string { if (!stories || stories.length === 0) { return "No stories found."; } return stories.map((story, index) => { return `${index + 1}. ${story.title} ID: ${story.id} By: ${story.by} Published: ${story.time} Score: ${story.score} Comments: ${story.commentsCount} URL: ${story.url || 'N/A'} ------------------------------`; }).join('\n\n'); }