Update mcp/clinicaltrials.py
Browse files- mcp/clinicaltrials.py +43 -30
mcp/clinicaltrials.py
CHANGED
@@ -1,34 +1,47 @@
|
|
1 |
-
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
import
|
5 |
|
6 |
-
async def fetch_clinical_trials(
|
|
|
|
|
|
|
7 |
"""
|
8 |
-
|
9 |
-
|
10 |
"""
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
"""
|
3 |
+
Fetch clinical trials from ClinicalTrials.gov without external clients.
|
4 |
+
"""
|
5 |
|
6 |
+
import httpx
|
7 |
+
import urllib.parse
|
8 |
|
9 |
+
async def fetch_clinical_trials(
|
10 |
+
query: str,
|
11 |
+
max_studies: int = 10
|
12 |
+
) -> list[dict]:
|
13 |
"""
|
14 |
+
Query ClinicalTrials.gov study_fields API for NCTId, Title, Phase, Status.
|
15 |
+
Returns up to `max_studies` results.
|
16 |
"""
|
17 |
+
# Build the API URL
|
18 |
+
base = "https://clinicaltrials.gov/api/query/study_fields"
|
19 |
+
params = {
|
20 |
+
"expr": query,
|
21 |
+
"fields": ",".join(["NCTId", "BriefTitle", "Phase", "OverallStatus"]),
|
22 |
+
"max_rnk": max_studies,
|
23 |
+
"fmt": "json",
|
24 |
+
}
|
25 |
+
url = f"{base}?{urllib.parse.urlencode(params)}"
|
26 |
+
|
27 |
+
try:
|
28 |
+
async with httpx.AsyncClient(timeout=10) as client:
|
29 |
+
resp = await client.get(url)
|
30 |
+
resp.raise_for_status()
|
31 |
+
data = resp.json()
|
32 |
+
except Exception:
|
33 |
+
# In case of any network / parse error
|
34 |
+
return []
|
35 |
+
|
36 |
+
# Navigate JSON response
|
37 |
+
# API docs: https://clinicaltrials.gov/api/gui/ref/study_fields
|
38 |
+
studies = data.get("StudyFieldsResponse", {}).get("StudyFields", [])
|
39 |
+
output = []
|
40 |
+
for s in studies:
|
41 |
+
output.append({
|
42 |
+
"nctId": s.get("NCTId", [""])[0],
|
43 |
+
"briefTitle": s.get("BriefTitle", [""])[0],
|
44 |
+
"phase": s.get("Phase", [""])[0],
|
45 |
+
"status": s.get("OverallStatus", [""])[0],
|
46 |
+
})
|
47 |
+
return output
|