/**Search Hacker News for stories about "machine learning"
* search_by_date MCP Tool
* Search Hacker News content sorted by date
*/
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
import { HNError } from '../lib/errors.js';
import type { HNClient } from '../lib/hn-client.js';
import { createChildLogger } from '../lib/logger.js';
export function registerSearchByDate(server: McpServer, hnClient: HNClient): void {
server.registerTool(
'search_by_date',
{
title: 'Search by Date',
description:
'Search Hacker News content sorted by date (most recent first). Useful for finding latest stories, comments, or posts by specific authors.',
inputSchema: {
query: z.string().describe('Search query text. Can be empty to get all content matching tags/filters.'),
tags: z
.string()
.optional()
.describe(
"Optional filter tags. Examples: 'story', 'comment', 'show_hn', 'ask_hn', 'author_USERNAME', 'story_ID'"
),
numericFilters: z
.string()
.optional()
.describe('Optional numeric filters for date ranges, points, comments count'),
page: z.number().int().min(0).optional().default(0).describe('Page number (0-indexed)'),
hitsPerPage: z.number().int().min(1).max(100).optional().default(20).describe('Results per page'),
},
outputSchema: {
hits: z.array(z.any()),
nbHits: z.number(),
nbPages: z.number(),
page: z.number(),
hitsPerPage: z.number(),
},
},
async ({ query, tags, numericFilters, page = 0, hitsPerPage = 20 }) => {
const correlationId = crypto.randomUUID();
const toolLogger = createChildLogger({ correlationId, tool: 'search_by_date' });
try {
toolLogger.info({ query, tags, numericFilters, page, hitsPerPage }, 'Searching by date');
const result = await hnClient.searchByDate({
query,
...(tags && { tags }),
...(numericFilters && { numericFilters }),
page,
hitsPerPage,
});
toolLogger.info({ nbHits: result.nbHits, nbPages: result.nbPages }, 'Search completed');
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
structuredContent: result as unknown as Record<string, unknown>,
};
} catch (error) {
toolLogger.error({ error }, 'Search failed');
if (error instanceof HNError) {
const errorContent = { error: error.message };
return {
content: [{ type: 'text', text: JSON.stringify(errorContent) }],
isError: true,
};
}
throw error;
}
}
);
}