search_articles
Search for New York Times articles published within the last 30 days using a specific keyword to find relevant content quickly.
Instructions
Search NYTimes articles from the last 30 days based on a keyword
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| keyword | Yes | Keyword to search for in articles |
Implementation Reference
- src/index.ts:116-169 (handler)Executes the search_articles tool: validates input, queries NYTimes API for recent articles matching the keyword, maps response to simplified format, returns as JSON text content.async (request) => { if (request.params.name !== "search_articles") { throw new McpError( ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}` ); } if (!isValidSearchArticlesArgs(request.params.arguments)) { throw new McpError( ErrorCode.InvalidParams, "Invalid search arguments" ); } const keyword = request.params.arguments.keyword; try { const response = await this.axiosInstance.get<NYTimesApiResponse>(API_CONFIG.ENDPOINT, { params: { q: keyword, sort: 'newest', 'begin_date': this.getDateString(30), // 30 days ago 'end_date': this.getDateString(0) // today } }); const articles: ArticleSearchResult[] = response.data.response.docs.map(article => ({ title: article.headline.main, abstract: article.abstract, url: article.web_url, publishedDate: article.pub_date, author: article.byline.original || 'Unknown' })); return { content: [{ type: "text", text: JSON.stringify(articles, null, 2) }] }; } catch (error) { if (axios.isAxiosError(error)) { return { content: [{ type: "text", text: `NYTimes API error: ${error.response?.data.message ?? error.message}` }], isError: true, } } throw error; } }
- src/index.ts:94-112 (registration)Registers the 'search_articles' tool in the MCP ListToolsRequestSchema response, including name, description, and input schema.this.server.setRequestHandler( ListToolsRequestSchema, async () => ({ tools: [{ name: "search_articles", description: "Search NYTimes articles from the last 30 days based on a keyword", inputSchema: { type: "object", properties: { keyword: { type: "string", description: "Keyword to search for in articles" } }, required: ["keyword"] } }] }) );
- src/types.ts:64-66 (schema)TypeScript interface defining the input arguments for search_articles tool.export interface SearchArticlesArgs { keyword: string; }
- src/types.ts:69-76 (helper)Type guard function to validate search_articles arguments before execution.export function isValidSearchArticlesArgs(args: any): args is SearchArticlesArgs { return ( typeof args === "object" && args !== null && "keyword" in args && typeof args.keyword === "string" ); }
- src/index.ts:173-177 (helper)Helper function to format date for NYTimes API query (days ago).private getDateString(daysAgo: number): string { const date = new Date(); date.setDate(date.getDate() - daysAgo); return date.toISOString().split('T')[0].replace(/-/g, ''); }