-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
54 lines (46 loc) · 1.67 KB
/
main.py
File metadata and controls
54 lines (46 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from mcp.server.fastmcp import FastMCP
from Bio import Entrez
import time
import warnings
import asyncio
# Suppress warnings and set email for Entrez
warnings.filterwarnings('ignore')
Entrez.email = "your email"
# Initialize FastMCP server
mcp = FastMCP("pubmed")
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
@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."
if __name__ == "__main__":
mcp.run(transport='stdio')