search_pubmed
Search PubMed for scientific articles using queries, date filters, and sorting options to find relevant research publications.
Instructions
Search PubMed for scientific articles.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query for PubMed | |
| searchOptions | No | Optional search parameters |
Implementation Reference
- src/index.ts:142-174 (handler)Inline asynchronous handler function for the 'search_pubmed' tool. Processes input parameters, handles date range logic, invokes searchHandler.search, and formats the response as MCP content.async ({ query, searchOptions }) => { try { const options = { ...(searchOptions || {}) }; // If dateFrom is provided but dateTo is missing, set dateTo to today's date if (options.dateFrom && !options.dateTo) { const now = new Date(); const yyyy = now.getFullYear(); const mm = String(now.getMonth() + 1).padStart(2, '0'); const dd = String(now.getDate()).padStart(2, '0'); options.dateTo = `${yyyy}/${mm}/${dd}`; } const results = await searchHandler.search(query, options); return { content: [ { type: 'text', text: JSON.stringify(results, null, 2), }, ], }; } catch (error) { return { content: [ { type: 'text', text: `Error searching PubMed: ${error instanceof Error ? error.message : 'Unknown error'}`, }, ], }; } }
- src/index.ts:125-140 (schema)Zod input schema definition for the 'search_pubmed' tool, defining 'query' as required string and optional 'searchOptions' object with retMax, retStart, sort, dateFrom, dateTo.inputSchema: { query: z.string().describe('Search query for PubMed'), searchOptions: z .object({ retMax: z.number().optional().describe('Maximum number of results to return'), retStart: z.number().optional().describe('Starting index for results'), sort: z .enum(['relevance', 'pub_date', 'author', 'journal']) .optional() .describe('Sort order for results'), dateFrom: z.string().optional().describe('Start date filter (YYYY/MM/DD format)'), dateTo: z.string().optional().describe('End date filter (YYYY/MM/DD format)(If dateFrom is provided but dateTo is missing, set dateTo to today\'s date)'), }) .optional() .describe('Optional search parameters'), },
- src/index.ts:120-175 (registration)Complete registration of the 'search_pubmed' MCP tool using server.registerTool, including metadata, Zod input schema, and the primary handler function.server.registerTool( 'search_pubmed', { title: 'PubMed Search', description: 'Search PubMed for scientific articles.', inputSchema: { query: z.string().describe('Search query for PubMed'), searchOptions: z .object({ retMax: z.number().optional().describe('Maximum number of results to return'), retStart: z.number().optional().describe('Starting index for results'), sort: z .enum(['relevance', 'pub_date', 'author', 'journal']) .optional() .describe('Sort order for results'), dateFrom: z.string().optional().describe('Start date filter (YYYY/MM/DD format)'), dateTo: z.string().optional().describe('End date filter (YYYY/MM/DD format)(If dateFrom is provided but dateTo is missing, set dateTo to today\'s date)'), }) .optional() .describe('Optional search parameters'), }, }, async ({ query, searchOptions }) => { try { const options = { ...(searchOptions || {}) }; // If dateFrom is provided but dateTo is missing, set dateTo to today's date if (options.dateFrom && !options.dateTo) { const now = new Date(); const yyyy = now.getFullYear(); const mm = String(now.getMonth() + 1).padStart(2, '0'); const dd = String(now.getDate()).padStart(2, '0'); options.dateTo = `${yyyy}/${mm}/${dd}`; } const results = await searchHandler.search(query, options); return { content: [ { type: 'text', text: JSON.stringify(results, null, 2), }, ], }; } catch (error) { return { content: [ { type: 'text', text: `Error searching PubMed: ${error instanceof Error ? error.message : 'Unknown error'}`, }, ], }; } } );
- src/handlers/search.ts:13-27 (helper)Factory function createSearchHandler that initializes PubMed API client and returns a search handler object. The search method calls pubmedApi.searchAndFetch and maps results to {pmid, title, pubDate}.export function createSearchHandler(pubmedOptions: PubMedOptions): SearchHandler { const pubmedApi = createPubMedAPI(pubmedOptions); return { async search(query: string, searchOptions?: SearchOptions): Promise<SearchResultItem[]> { const articles = await pubmedApi.searchAndFetch(query, searchOptions); return articles.map(article => ({ pmid: article.pmid, title: article.title, pubDate: article.pubDate })); } }; }
- src/pubmed-api.ts:568-572 (helper)Core searchAndFetch method in PubMedAPI implementation. Performs ESearch for PMIDs then fetches article details via EFetch, implementing the actual PubMed API interaction for search_pubmed tool.const searchAndFetch = async (query: string, options: SearchAndFetchOptions = {}): Promise<Article[]> => { const { maxResults = 20, ...searchOptions } = options; const searchResult = await search(query, { ...searchOptions, retMax: maxResults }); return fetchArticles(searchResult.idList); };