File size: 1,063 Bytes
682f510
 
 
b3da24c
 
682f510
 
 
 
 
 
 
 
 
 
 
 
 
b3da24c
682f510
 
 
 
 
 
 
 
 
 
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
# mcp/clinicaltrials.py  –  403-proof CPU-only helper
import httpx, asyncio, datetime
from typing import List, Dict

BASE = "https://clinicaltrials.gov/api/query/study_fields"
UA   = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
        "AppleWebKit/537.36 (KHTML, like Gecko) "
        "Chrome/124.0 Safari/537.36")  # real browser UA

_HEADERS = {"User-Agent": UA}

async def _fetch(url: str, params: Dict) -> Dict:
    async with httpx.AsyncClient(timeout=20, headers=_HEADERS) as c:
        r = await c.get(url, params=params)
        # If still 4xx/5xx, return empty dict – keep app alive
        if r.status_code != 200:
            return {}
        return r.json()

async def search_trials(term: str, max_studies: int = 10) -> List[Dict]:
    params = dict(
        expr=term,
        fields="NCTId,BriefTitle,Condition,InterventionName,Phase,OverallStatus,StartDate",
        max_rnk=max_studies,
        min_rnk=1,
        fmt="json",
    )
    data = await _fetch(BASE, params)
    return data.get("StudyFieldsResponse", {}).get("StudyFields", [])