search_pubmed
Retrieve relevant PubMed article abstracts by entering a search query and specifying the maximum number of results. Simplify biomedical research with structured, easy-to-read outputs.
Instructions
Search PubMed for articles matching the query.
Args:
query: The search term for PubMed.
max_results: Maximum number of articles to retrieve.
Returns:
A string containing the abstracts of found articles, separated by two newlines.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| max_results | No | ||
| query | No | endocarditis |
Implementation Reference
- pubmed-mcp-server/main.py:34-51 (handler)The main handler function for the 'search_pubmed' tool. It uses asyncio.to_thread to call the blocking fetch_pubmed_articles helper and formats the results as a string.@mcp.tool() async def search_pubmed(query: str = "endocarditis", max_results: int = 10) -> str: """ Search PubMed for articles matching the query. Args: query: The search term for PubMed. max_results: Maximum number of articles to retrieve. Returns: A string containing the abstracts of found articles, separated by two newlines. """ # Run the blocking function in a separate thread articles = await asyncio.to_thread(fetch_pubmed_articles, query, max_results) if articles: return "\n\n".join(articles) else: return "No articles found for the given query."
- pubmed-mcp-server/main.py:14-32 (helper)Helper function that performs the actual PubMed search using Bio.Entrez, fetching abstracts for the top results.def fetch_pubmed_articles(query: str = "endocarditis", max_results: int = 20) -> list[str]: """Fetch PubMed articles for the given query without saving to a file.""" handle = Entrez.esearch(db="pubmed", term=query, retmax=max_results) record = Entrez.read(handle) handle.close() ids = record.get('IdList', []) articles = [] for pmid in ids: time.sleep(0.5) # Delay to avoid overwhelming the API try: handle = Entrez.efetch(db="pubmed", id=pmid, rettype="abstract", retmode="text") abstract = handle.read() handle.close() if abstract: articles.append(abstract.strip()) except Exception: continue return articles
- pubmed-mcp-server/main.py:34-34 (registration)The @mcp.tool() decorator registers the search_pubmed function as an MCP tool.@mcp.tool()
- pubmed-mcp-server/main.py:35-45 (schema)Function signature and docstring define the input schema (query: str, max_results: int) and output (str).async def search_pubmed(query: str = "endocarditis", max_results: int = 10) -> str: """ Search PubMed for articles matching the query. Args: query: The search term for PubMed. max_results: Maximum number of articles to retrieve. Returns: A string containing the abstracts of found articles, separated by two newlines. """