Update mcp/clinicaltrials.py
Browse files- mcp/clinicaltrials.py +30 -58
mcp/clinicaltrials.py
CHANGED
@@ -1,62 +1,34 @@
|
|
1 |
# mcp/clinicaltrials.py
|
2 |
|
3 |
-
import
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
"
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
)
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
r = await client.get(BASE_V2, params=params)
|
26 |
-
if r.status_code == 403:
|
27 |
-
return [] # API blocked or IP rate-limited
|
28 |
-
r.raise_for_status()
|
29 |
-
js = r.json()
|
30 |
-
# List of studies under ['studies']
|
31 |
-
return js.get("studies", [])
|
32 |
-
|
33 |
-
async def fetch_trials_v1(term: str, limit: int = 10) -> List[Dict]:
|
34 |
-
"""Fallback: Fetch trials using legacy v1 API."""
|
35 |
-
params = dict(
|
36 |
-
expr=term,
|
37 |
-
fields="NCTId,BriefTitle,Condition,InterventionName,Phase,OverallStatus,StartDate",
|
38 |
-
max_rnk=limit,
|
39 |
-
min_rnk=1,
|
40 |
-
fmt="json",
|
41 |
-
)
|
42 |
-
async with httpx.AsyncClient(timeout=20, headers=HEADERS) as client:
|
43 |
-
r = await client.get(BASE_V1, params=params)
|
44 |
-
if r.status_code == 403:
|
45 |
-
return []
|
46 |
-
r.raise_for_status()
|
47 |
-
js = r.json()
|
48 |
-
return js.get("StudyFieldsResponse", {}).get("StudyFields", [])
|
49 |
-
|
50 |
-
async def search_trials(term: str, max_studies: int = 10) -> List[Dict]:
|
51 |
-
"""Unified entry point. Try V2, fallback to V1 if needed."""
|
52 |
-
try:
|
53 |
-
trials = await fetch_trials_v2(term, max_studies)
|
54 |
-
if trials:
|
55 |
-
return trials
|
56 |
-
except Exception:
|
57 |
-
pass
|
58 |
-
try:
|
59 |
-
trials = await fetch_trials_v1(term, max_studies)
|
60 |
-
return trials
|
61 |
-
except Exception:
|
62 |
-
return []
|
|
|
1 |
# mcp/clinicaltrials.py
|
2 |
|
3 |
+
from pytrials.client import ClinicalTrials
|
4 |
+
import asyncio
|
5 |
|
6 |
+
async def fetch_clinical_trials(query: str, max_studies: int = 10) -> list[dict]:
|
7 |
+
"""
|
8 |
+
Pulls NCTId, Title, Phase, and Status for studies matching the given query.
|
9 |
+
Uses the pytrials wrapper over ClinicalTrials.gov.
|
10 |
+
"""
|
11 |
+
# pytrials expects a direct search expression, e.g. disease MeSH term or drug
|
12 |
+
client = ClinicalTrials()
|
13 |
+
# These are the fields you want from the API
|
14 |
+
study_fields = ["NCTId", "BriefTitle", "Phase", "OverallStatus"]
|
15 |
+
# Wrap in a ThreadPool so we can call it in asyncio
|
16 |
+
loop = asyncio.get_event_loop()
|
17 |
+
records = await loop.run_in_executor(
|
18 |
+
None,
|
19 |
+
lambda: client.get_study_fields(
|
20 |
+
search_expr=query,
|
21 |
+
fields=study_fields,
|
22 |
+
max_studies=max_studies
|
23 |
+
)
|
24 |
)
|
25 |
+
# Normalize the field names for your UI
|
26 |
+
return [
|
27 |
+
{
|
28 |
+
"nctId": rec.get("NCTId"),
|
29 |
+
"briefTitle": rec.get("BriefTitle"),
|
30 |
+
"phase": rec.get("Phase"),
|
31 |
+
"status": rec.get("OverallStatus"),
|
32 |
+
}
|
33 |
+
for rec in records
|
34 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|