search_pubmed
Search academic papers from PubMed to find relevant research articles using query terms and return paper metadata for analysis.
Instructions
Search academic papers from PubMed.
Args: query: Search query string (e.g., 'machine learning'). max_results: Maximum number of papers to return (default: 10). Returns: List of paper metadata in dictionary format.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | ||
| max_results | No |
Implementation Reference
- paper_search_mcp/server.py:372-382 (handler)The `search_pubmed` tool handler in the MCP server, which orchestrates the call to the PubMedSearcher instance.
async def search_pubmed(query: str, max_results: int = 10) -> List[Dict]: """Search academic papers from PubMed. Args: query: Search query string (e.g., 'machine learning'). max_results: Maximum number of papers to return (default: 10). Returns: List of paper metadata in dictionary format. """ papers = await async_search(pubmed_searcher, query, max_results) return papers if papers else [] - The `PubMedSearcher.search` method which performs the actual API calls to PubMed to fetch academic papers.
def search(self, query: str, max_results: int = 10) -> List[Paper]: search_params = { 'db': 'pubmed', 'term': query, 'retmax': max_results, 'retmode': 'xml' } search_response = requests.get(self.SEARCH_URL, params=search_params) search_root = ET.fromstring(search_response.content) ids = [id.text for id in search_root.findall('.//Id') if id.text] if not ids: return [] fetch_params = { 'db': 'pubmed', 'id': ','.join(ids), 'retmode': 'xml' } fetch_response = requests.get(self.FETCH_URL, params=fetch_params) fetch_root = ET.fromstring(fetch_response.content) papers = [] for article in fetch_root.findall('.//PubmedArticle'): try: pmid_elem = article.find('.//PMID') pmid = pmid_elem.text.strip() if pmid_elem is not None and pmid_elem.text else '' if not pmid: continue title_elem = article.find('.//ArticleTitle') title = ''.join(title_elem.itertext()).strip() if title_elem is not None else '' if not title: continue authors = [] for author in article.findall('.//Author'): last_name = author.find('LastName') initials = author.find('Initials') if last_name is not None and last_name.text: name = last_name.text.strip() if initials is not None and initials.text: name = f"{name} {initials.text.strip()}" authors.append(name) abstract_parts = [] for abstract_elem in article.findall('.//AbstractText'): text = ''.join(abstract_elem.itertext()).strip() if text: abstract_parts.append(text) abstract = ' '.join(abstract_parts) year_elem = article.find('.//PubDate/Year') pub_date = year_elem.text if year_elem is not None else None published = datetime.strptime(pub_date, '%Y') if pub_date else None doi_elem = article.find('.//ELocationID[@EIdType="doi"]') doi = doi_elem.text if doi_elem is not None else '' if not doi and abstract: doi = extract_doi(abstract) papers.append(Paper( paper_id=pmid, title=title, authors=authors, abstract=abstract, url=f"https://pubmed.ncbi.nlm.nih.gov/{pmid}/", pdf_url='', # PubMed 无直接 PDF published_date=published, updated_date=published, source='pubmed', categories=[], keywords=[], doi=doi )) except Exception: continue return papers - paper_search_mcp/server.py:43-43 (registration)Registration of the `pubmed_searcher` instance used by the handler.
pubmed_searcher = PubMedSearcher()