Create openfda.py
Browse files- mcp/openfda.py +24 -0
mcp/openfda.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mcp/openfda.py
|
| 2 |
+
|
| 3 |
+
import httpx
|
| 4 |
+
|
| 5 |
+
OPENFDA_DRUG_EVENT = "https://api.fda.gov/drug/event.json"
|
| 6 |
+
|
| 7 |
+
async def fetch_drug_safety(drug: str, max_results: int = 3):
|
| 8 |
+
"""Fetch recent adverse event reports for the drug from OpenFDA."""
|
| 9 |
+
params = {"search": f'patient.drug.medicinalproduct:"{drug}"', "limit": max_results}
|
| 10 |
+
async with httpx.AsyncClient() as client:
|
| 11 |
+
try:
|
| 12 |
+
resp = await client.get(OPENFDA_DRUG_EVENT, params=params)
|
| 13 |
+
results = resp.json().get("results", [])
|
| 14 |
+
output = []
|
| 15 |
+
for ev in results:
|
| 16 |
+
output.append({
|
| 17 |
+
"safety_report_id": ev.get("safetyreportid"),
|
| 18 |
+
"serious": ev.get("serious"),
|
| 19 |
+
"reactions": [r["reactionmeddrapt"] for r in ev.get("patient", {}).get("reaction", [])],
|
| 20 |
+
"receivedate": ev.get("receivedate"),
|
| 21 |
+
})
|
| 22 |
+
return output
|
| 23 |
+
except Exception:
|
| 24 |
+
return []
|