mgbam commited on
Commit
de2e45e
·
verified ·
1 Parent(s): ee63964

Create pubmed.py

Browse files
Files changed (1) hide show
  1. mcp/pubmed.py +46 -0
mcp/pubmed.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # mcp/pubmed.py
2
+
3
+ import httpx
4
+ import xmltodict
5
+ import os
6
+
7
+ PUBMED_ESEARCH = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
8
+ PUBMED_EFETCH = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi"
9
+ PUB_KEY = os.environ.get("PUB_KEY")
10
+
11
+ async def fetch_pubmed(query: str, max_results: int = 5):
12
+ """Fetch latest PubMed articles for the query."""
13
+ async with httpx.AsyncClient() as client:
14
+ params = {
15
+ "db": "pubmed",
16
+ "term": query,
17
+ "retmax": max_results,
18
+ "retmode": "json",
19
+ "api_key": PUB_KEY
20
+ }
21
+ resp = await client.get(PUBMED_ESEARCH, params=params)
22
+ ids = resp.json()["esearchresult"]["idlist"]
23
+ if not ids:
24
+ return []
25
+ efetch_params = {
26
+ "db": "pubmed",
27
+ "id": ",".join(ids),
28
+ "retmode": "xml",
29
+ "api_key": PUB_KEY
30
+ }
31
+ efetch_resp = await client.get(PUBMED_EFETCH, params=efetch_params)
32
+ articles = xmltodict.parse(efetch_resp.text)["PubmedArticleSet"].get("PubmedArticle", [])
33
+ if not isinstance(articles, list): # Single article
34
+ articles = [articles]
35
+ results = []
36
+ for a in articles:
37
+ art = a["MedlineCitation"]["Article"]
38
+ results.append({
39
+ "title": art["ArticleTitle"],
40
+ "authors": ", ".join([a["LastName"] + " " + a["ForeName"] for a in art.get("AuthorList", {}).get("Author", []) if "LastName" in a and "ForeName" in a]),
41
+ "summary": art.get("Abstract", {}).get("AbstractText", [""])[0],
42
+ "link": f"https://pubmed.ncbi.nlm.nih.gov/{a['MedlineCitation']['PMID']['#text']}/",
43
+ "published": art.get("ArticleDate", [{}])[0].get("Year", ""),
44
+ "source": "PubMed"
45
+ })
46
+ return results