mgbam commited on
Commit
682f510
·
verified ·
1 Parent(s): ee65d10

Update mcp/clinicaltrials.py

Browse files
Files changed (1) hide show
  1. mcp/clinicaltrials.py +26 -20
mcp/clinicaltrials.py CHANGED
@@ -1,23 +1,29 @@
1
- # mcp/clinicaltrials.py
2
- """
3
- ClinicalTrials.gov helper fetch recent trials for a keyword.
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[dict]:
12
- today = datetime.date.today().isoformat()
13
- params = {
14
- "expr": term,
15
- "fields": "NCTId,BriefTitle,Condition,InterventionName,Phase,OverallStatus,StartDate",
16
- "max_rnk": max_studies,
17
- "fmt": "json",
18
- "min_rnk": 1
19
- }
20
- async with httpx.AsyncClient(timeout=20) as client:
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", [])