Get top stories
get_top_storiesRetrieve current top stories from Hacker News to stay informed about trending technology and programming topics.
Instructions
Get the current top Hacker News stories.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| stories | Yes | ||
| returned | Yes |
Implementation Reference
- servers/hackernews/src/index.ts:314-321 (handler)The handler implementation for the `get_top_stories` tool in `HackerNewsServer`, which calls the client's `getTopStories` method.
handler: async ({ limit }, context) => { await context.log("info", "Fetching top Hacker News stories"); const stories = await this.client.getTopStories(limit); return { stories: [...stories], returned: stories.length, }; }, - servers/hackernews/src/index.ts:302-323 (registration)Registration of the `get_top_stories` tool within the `HackerNewsServer` class.
this.registerTool( defineTool({ name: "get_top_stories", title: "Get top stories", description: "Get the current top Hacker News stories.", inputSchema: { limit: z.number().int().min(1).max(30).default(10), }, outputSchema: { stories: z.array(storySummarySchema), returned: z.number().int(), }, handler: async ({ limit }, context) => { await context.log("info", "Fetching top Hacker News stories"); const stories = await this.client.getTopStories(limit); return { stories: [...stories], returned: stories.length, }; }, renderText: ({ stories }) => stories.map((story) => `${story.title} (${story.score ?? 0} points)`).join("\n"), }), - The `getTopStories` method on `FetchHackerNewsClient` that actually performs the API request to fetch top stories.
public async getTopStories(limit: number): Promise<ReadonlyArray<HackerNewsStorySummary>> { const ids = await this.fetchJson(new URL(`${this.apiBaseUrl}/topstories.json`), z.array(z.number().int().nonnegative())); const items = await Promise.all(ids.slice(0, limit).map((id) => this.fetchItem(id))); return items .filter((item): item is z.infer<typeof hnItemSchema> => isHnItem(item) && item.type === "story") .map((item) => this.toStorySummary(item)); }