Update mcp/clinicaltrials.py
Browse files- mcp/clinicaltrials.py +26 -20
mcp/clinicaltrials.py
CHANGED
@@ -1,23 +1,29 @@
|
|
1 |
-
# mcp/clinicaltrials.py
|
2 |
-
|
3 |
-
|
4 |
-
"""
|
5 |
-
|
6 |
-
import httpx, datetime
|
7 |
-
from typing import List
|
8 |
|
9 |
BASE = "https://clinicaltrials.gov/api/query/study_fields"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
async def search_trials(term: str, max_studies: int = 10) -> List[
|
12 |
-
|
13 |
-
|
14 |
-
"
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
r = await client.get(BASE, params=params)
|
22 |
-
r.raise_for_status()
|
23 |
-
return r.json()["StudyFieldsResponse"]["StudyFields"]
|
|
|
1 |
+
# mcp/clinicaltrials.py – 403-proof CPU-only helper
|
2 |
+
import httpx, asyncio, datetime
|
3 |
+
from typing import List, Dict
|
|
|
|
|
|
|
|
|
4 |
|
5 |
BASE = "https://clinicaltrials.gov/api/query/study_fields"
|
6 |
+
UA = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
7 |
+
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
8 |
+
"Chrome/124.0 Safari/537.36") # real browser UA
|
9 |
+
|
10 |
+
_HEADERS = {"User-Agent": UA}
|
11 |
+
|
12 |
+
async def _fetch(url: str, params: Dict) -> Dict:
|
13 |
+
async with httpx.AsyncClient(timeout=20, headers=_HEADERS) as c:
|
14 |
+
r = await c.get(url, params=params)
|
15 |
+
# If still 4xx/5xx, return empty dict – keep app alive
|
16 |
+
if r.status_code != 200:
|
17 |
+
return {}
|
18 |
+
return r.json()
|
19 |
|
20 |
+
async def search_trials(term: str, max_studies: int = 10) -> List[Dict]:
|
21 |
+
params = dict(
|
22 |
+
expr=term,
|
23 |
+
fields="NCTId,BriefTitle,Condition,InterventionName,Phase,OverallStatus,StartDate",
|
24 |
+
max_rnk=max_studies,
|
25 |
+
min_rnk=1,
|
26 |
+
fmt="json",
|
27 |
+
)
|
28 |
+
data = await _fetch(BASE, params)
|
29 |
+
return data.get("StudyFieldsResponse", {}).get("StudyFields", [])
|
|
|
|
|
|