Skip to main content
Glama
ncukondo

PubMed MCP Server

by ncukondo

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
NameRequiredDescriptionDefault
queryYesSearch query for PubMed
searchOptionsNoOptional search parameters

Implementation Reference

  • 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'}`, }, ], }; } }
  • 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'}`, }, ], }; } } );
  • 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 })); } }; }
  • 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); };

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ncukondo/pubmed-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server