mgbam commited on
Commit
ac3658e
·
verified ·
1 Parent(s): 6fe33f5

Update mcp/clinicaltrials.py

Browse files
Files changed (1) hide show
  1. mcp/clinicaltrials.py +30 -58
mcp/clinicaltrials.py CHANGED
@@ -1,62 +1,34 @@
1
  # mcp/clinicaltrials.py
2
 
3
- import httpx
4
- from typing import List, Dict
5
 
6
- BASE_V2 = "https://clinicaltrials.gov/api/v2/studies"
7
- BASE_V1 = "https://clinicaltrials.gov/api/query/study_fields"
8
-
9
- HEADERS = {
10
- "User-Agent": (
11
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
12
- "AppleWebKit/537.36 (KHTML, like Gecko) "
13
- "Chrome/124.0 Safari/537.36"
 
 
 
 
 
 
 
 
 
 
14
  )
15
- }
16
-
17
- async def fetch_trials_v2(term: str, limit: int = 10) -> List[Dict]:
18
- """Fetch clinical trials using v2 API."""
19
- params = {
20
- "query": term,
21
- "pageSize": limit,
22
- "fields": "nctId,briefTitle,phase,status,startDate,conditions,interventions"
23
- }
24
- async with httpx.AsyncClient(timeout=20, headers=HEADERS) as client:
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
+ ]