File size: 10,917 Bytes
56e185e ab1e76c 56e185e ab1e76c 56e185e ab1e76c 56e185e ab1e76c 56e185e ab1e76c 56e185e 98f00cc 56e185e 98f00cc 56e185e 98f00cc 56e185e 98f00cc 56e185e 98f00cc 56e185e 98f00cc 56e185e 98f00cc 56e185e 98f00cc 56e185e ab1e76c 56e185e |
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
import os
import uuid
import json
import asyncio
from dotenv import load_dotenv
import gradio as gr
from mcp.server.fastmcp import FastMCP, Context
# ---------------------------------------------------------------------------
# Environment & MCP initialisation
# ---------------------------------------------------------------------------
load_dotenv()
mcp = FastMCP(
name="healthcare-mcp",
version="1.0.0",
description="Healthcare MCP server for medical information access (Gradio edition)"
)
# ---------------------------------------------------------------------------
# Internal tools & services
# ---------------------------------------------------------------------------
from src.tools.fda_tool import FDATool
from src.tools.pubmed_tool import PubMedTool
from src.tools.healthfinder_tool import HealthFinderTool
from src.tools.clinical_trials_tool import ClinicalTrialsTool
from src.tools.medical_terminology_tool import MedicalTerminologyTool
from src.services.usage_service import UsageService
fda_tool = FDATool()
pubmed_tool = PubMedTool()
healthfinder_tool = HealthFinderTool()
clinical_trials_tool = ClinicalTrialsTool()
medical_terminology_tool = MedicalTerminologyTool()
usage_service = UsageService(db_path="healthcare_usage.db")
# Unique session – ensures all usage is grouped for the current interface user
session_id = str(uuid.uuid4())
# ---------------------------------------------------------------------------
# FastMCP tool definitions – these can be reached programmatically by MCP
# clients (and are also reused by the Gradio UI wrappers below).
# ---------------------------------------------------------------------------
@mcp.tool()
async def fda_drug_lookup(ctx: Context, drug_name: str, search_type: str = "general"):
"""Look up drug information from the FDA database."""
usage_service.record_usage(session_id, "fda_drug_lookup")
return await fda_tool.lookup_drug(drug_name, search_type)
@mcp.tool()
async def pubmed_search(ctx: Context, query: str, max_results: int = 5, date_range: str = ""):
"""Search for medical literature in the PubMed database."""
usage_service.record_usage(session_id, "pubmed_search")
return await pubmed_tool.search_literature(query, max_results, date_range)
@mcp.tool()
async def health_topics(ctx: Context, topic: str, language: str = "en"):
"""Retrieve evidence‑based information for a specific health topic."""
usage_service.record_usage(session_id, "health_topics")
return await healthfinder_tool.get_health_topics(topic, language)
@mcp.tool()
async def clinical_trials_search(
ctx: Context,
condition: str,
status: str = "recruiting",
max_results: int = 10,
):
"""Search for clinical trials by condition and status."""
usage_service.record_usage(session_id, "clinical_trials_search")
return await clinical_trials_tool.search_trials(condition, status, max_results)
@mcp.tool()
async def lookup_icd_code(
ctx: Context,
code: str | None = None,
description: str | None = None,
max_results: int = 10,
):
"""Look up ICD‑10 codes by code or text description."""
usage_service.record_usage(session_id, "lookup_icd_code")
return await medical_terminology_tool.lookup_icd_code(code, description, max_results)
@mcp.tool()
async def get_usage_stats(ctx: Context):
"""Return per‑session usage statistics."""
return usage_service.get_monthly_usage(session_id)
@mcp.tool()
async def get_all_usage_stats(ctx: Context):
"""Return aggregate usage statistics across all sessions."""
return usage_service.get_usage_stats()
# ---------------------------------------------------------------------------
# Helper – convert coroutine results to nicely‑formatted JSON for UI display
# ---------------------------------------------------------------------------
def pretty_json(data):
try:
return json.dumps(data, indent=2, ensure_ascii=False)
except TypeError:
# Fallback if the object isn’t serialisable
return str(data)
# ---------------------------------------------------------------------------
# Gradio UI definitions
# ---------------------------------------------------------------------------
async def fda_drug_lookup_gr(drug_name: str, search_type: str):
"""Gradio wrapper for :pyfunc:`fda_drug_lookup`.
Parameters
----------
drug_name : str
Drug identifier to pass through to :pyfunc:`FDATool.lookup_drug`.
search_type : str
See :pydata:`search_type` in :pyfunc:`fda_drug_lookup`.
Returns
-------
str
JSON‑formatted response suitable for a ``gr.Textbox``.
"""
usage_service.record_usage(session_id, "fda_drug_lookup")
res = await fda_tool.lookup_drug(drug_name, search_type)
return pretty_json(res)
async def pubmed_search_gr(query: str, max_results: int, date_range: str):
"""Gradio wrapper for PubMed literature search.
Parameters
----------
query : str
PubMed query string.
max_results : int
Maximum number of articles requested from the backend tool.
date_range : str
If non‑empty, restricts search to the last *N* years.
Returns
-------
str
Pretty‑printed JSON with article details.
"""
usage_service.record_usage(session_id, "pubmed_search")
res = await pubmed_tool.search_literature(query, int(max_results), str(date_range or ""))
return pretty_json(res)
async def health_topics_gr(topic: str, language: str):
"""Gradio wrapper for HealthFinder topic retrieval.
Parameters
----------
topic : str
Health topic slug or phrase.
language : str
Output language (``"en"`` / ``"es"``).
Returns
-------
str
JSON‑formatted HealthFinder result.
"""
usage_service.record_usage(session_id, "health_topics")
res = await healthfinder_tool.get_health_topics(topic, language)
return pretty_json(res)
async def clinical_trials_search_gr(condition: str, status: str, max_results: int):
"""Gradio wrapper for :pyfunc:`clinical_trials_search`.
Parameters
----------
condition : str
Condition keyword, e.g. "melanoma".
status : str
Recruitment status filter.
max_results : int
Cap on trial records returned.
Returns
-------
str
Pretty‑printed JSON list of trials.
"""
usage_service.record_usage(session_id, "clinical_trials_search")
res = await clinical_trials_tool.search_trials(condition, status, int(max_results))
return pretty_json(res)
async def lookup_icd_code_gr(code: str, description: str, max_results: int):
"""Gradio wrapper for ICD‑10 code search.
The user may supply *either* an ICD‑10 code fragment *or* a free‑text
description. Empty strings are converted to ``None`` so the underlying tool
can decide which parameter to use.
Parameters
----------
code : str
ICD‑10 code or prefix (optional).
description : str
Natural‑language description (optional).
max_results : int
Maximum number of matches.
Returns
-------
str
JSON‑formatted list of matching codes.
"""
usage_service.record_usage(session_id, "lookup_icd_code")
code = code or None
description = description or None
res = await medical_terminology_tool.lookup_icd_code(code, description, int(max_results))
return pretty_json(res)
# ---------------------------------------------------------------------------
# Usage‑statistics helpers (synchronous; no external I/O)
# ---------------------------------------------------------------------------
def get_usage_stats_gr() -> str:
"""Return **session‑specific** usage statistics as pretty JSON.
Returns
-------
str
JSON rendition of the current session’s monthly usage counts.
"""
return pretty_json(usage_service.get_monthly_usage(session_id))
def get_all_usage_stats_gr() -> str:
"""Return **global** usage statistics across *all* sessions.
Returns
-------
str
JSON rendition of cumulative endpoint invocations.
"""
return pretty_json(usage_service.get_usage_stats())
# ---------------------------------------------------------------------------
# Build the Gradio Blocks interface
# ---------------------------------------------------------------------------
with gr.Blocks(title="Healthcare MCP Explorer") as demo:
gr.Markdown("""# 🩺 Healthcare MCP Dashboard\nInteractively query FDA, PubMed, HealthFinder, Clinical‑Trials, and ICD‑10 data — all from one screen.""")
# ---------------- Drug lookup ----------------
with gr.Group():
gr.Markdown("## 💊 FDA Drug Lookup")
with gr.Row():
drug_name_tb = gr.Textbox(label="Drug name", placeholder="e.g. atorvastatin")
search_type_dd = gr.Dropdown(
label="Search type",
choices=["general", "label", "adverse_events"],
value="general",
)
fda_button = gr.Button("Search")
fda_output = gr.Textbox(label="Result", lines=12)
# ---------------- PubMed search -------------
with gr.Group():
gr.Markdown("## 🔍 PubMed Literature Search")
query_tb = gr.Textbox(label="Query", placeholder="e.g. type 2 diabetes glycemic control")
with gr.Row():
max_results_slider = gr.Slider(1, 20, value=5, step=1, label="Max articles")
date_range_tb = gr.Textbox(label="Published within last N years (optional)")
pubmed_button = gr.Button("Search PubMed")
pubmed_output = gr.Textbox(label="Result", lines=12)
# ---------------- Health topics -------------
with gr.Group():
gr.Markdown("## 📚 HealthFinder Topics")
topic_tb = gr.Textbox(label="Health topic", placeholder="e.g. hypertension")
language_dd = gr.Dropdown(label="Language", choices=["en", "es"], value="en")
hf_button = gr.Button("Get Topic Info")
hf_output = gr.Textbox(label="Result", lines=12)
# -------------------------------------------------------------------
# Wire‑up callbacks
# -------------------------------------------------------------------
fda_button.click(fda_drug_lookup_gr, inputs=[drug_name_tb, search_type_dd], outputs=fda_output)
pubmed_button.click(pubmed_search_gr, inputs=[query_tb, max_results_slider, date_range_tb], outputs=pubmed_output)
hf_button.click(health_topics_gr, inputs=[topic_tb, language_dd], outputs=hf_output)
# ---------------------------------------------------------------------------
# Entrypoint – run Gradio and MCP in one process
# ---------------------------------------------------------------------------
if __name__ == "__main__":
demo.launch(mcp_server=True, show_error=True)
|