Update mcp/pubmed.py
Browse files- mcp/pubmed.py +40 -5
mcp/pubmed.py
CHANGED
@@ -30,17 +30,52 @@ async def fetch_pubmed(query: str, max_results: int = 5):
|
|
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":
|
41 |
-
"summary":
|
42 |
-
"link": f"https://pubmed.ncbi.nlm.nih.gov/{
|
43 |
-
"published":
|
44 |
"source": "PubMed"
|
45 |
})
|
46 |
return results
|
|
|
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 edge case
|
34 |
articles = [articles]
|
35 |
results = []
|
36 |
for a in articles:
|
37 |
art = a["MedlineCitation"]["Article"]
|
38 |
+
|
39 |
+
# Robustly extract publication year or date
|
40 |
+
published = ""
|
41 |
+
article_date = art.get("ArticleDate")
|
42 |
+
if isinstance(article_date, list) and article_date:
|
43 |
+
published = article_date[0].get("Year", "")
|
44 |
+
elif isinstance(article_date, dict):
|
45 |
+
published = article_date.get("Year", "")
|
46 |
+
else:
|
47 |
+
# Fallback to PubDate in Journal > JournalIssue > PubDate
|
48 |
+
pubdate = art.get("Journal", {}).get("JournalIssue", {}).get("PubDate", {})
|
49 |
+
published = pubdate.get("Year", "") or pubdate.get("MedlineDate", "")
|
50 |
+
|
51 |
+
# Robustly extract authors
|
52 |
+
authors_raw = art.get("AuthorList", {}).get("Author", [])
|
53 |
+
if isinstance(authors_raw, dict):
|
54 |
+
authors_raw = [authors_raw]
|
55 |
+
authors = ", ".join([
|
56 |
+
f"{a.get('LastName', '')} {a.get('ForeName', '')}".strip()
|
57 |
+
for a in authors_raw if a.get("LastName") and a.get("ForeName")
|
58 |
+
]) if authors_raw else "Unknown"
|
59 |
+
|
60 |
+
# Robustly extract summary/abstract
|
61 |
+
abstract = art.get("Abstract", {}).get("AbstractText", "")
|
62 |
+
if isinstance(abstract, list):
|
63 |
+
summary = " ".join(abstract)
|
64 |
+
elif isinstance(abstract, dict):
|
65 |
+
summary = abstract.get("#text", "")
|
66 |
+
else:
|
67 |
+
summary = abstract or ""
|
68 |
+
|
69 |
+
pmid = a["MedlineCitation"]["PMID"]
|
70 |
+
if isinstance(pmid, dict):
|
71 |
+
pmid = pmid.get("#text", "")
|
72 |
+
|
73 |
results.append({
|
74 |
"title": art["ArticleTitle"],
|
75 |
+
"authors": authors,
|
76 |
+
"summary": summary,
|
77 |
+
"link": f"https://pubmed.ncbi.nlm.nih.gov/{pmid}/",
|
78 |
+
"published": published,
|
79 |
"source": "PubMed"
|
80 |
})
|
81 |
return results
|