Medi — Clinical Pre-Visit Intelligence Agent
Inspiration
Clinicians spend 15-20 minutes per patient reviewing charts before every visit. They're scanning through conditions, medications, lab results, and uploaded clinical notes — trying to spot what matters. Meanwhile, medication conflicts slip through when different providers prescribe without seeing the full picture. A psychiatrist starts Sertraline while the PCP already has the patient on Escitalopram — same drug class, serotonin syndrome risk — and nobody catches it because the data lives in separate documents.
We asked: what if an AI agent could do in 30 seconds what takes a clinician 15 minutes — and catch what they'd miss?
What it does
Medi is a clinical pre-visit intelligence agent that connects to Prompt Opinion via the A2A protocol. Given a patient's FHIR record — including uploaded clinical documents — it delivers a prioritized clinical briefing covering:
- Critical Alerts — top 5 items a clinician would miss in a quick chart glance (medication conflicts, risk scores, care gaps)
- Medication Safety — dual-SSRI detection, NSAID/ACE-inhibitor interactions, therapeutic duplicates via RxNorm, FDA adverse event signals from the FAERS database in real-time
- Screening Interpretation — PHQ-2, GAD-7, DAST-10, PRAPARE scores translated into plain clinical language with severity bands and follow-up recommendations
- Vital Trend Detection — rising/falling/stable patterns across years of observations with threshold crossing alerts
- Care Gap Detection — overdue screenings and immunizations cross-referenced against USPSTF and CDC/ACIP guidelines
- Risk Stratification — multi-factor scoring for 30-day readmission, fall risk, diabetes complications, and hypertension progression
- Anti-Hallucination — zero fabrication tolerance; every clinical claim must cite a FHIR source or it gets dropped (93%+ citation rate)
The agent reads uploaded clinical documents (DocumentReferences) alongside structured FHIR data — catching medication issues that span across providers and documents that standard chart review misses.
How we built it
Architecture:
- Protocol: A2A (Agent-to-Agent) JSON-RPC — registered as an external agent in Prompt Opinion
- Framework: Mastra (
@mastra/core) for agent orchestration, tools, workflows, and MCP server - LLM: Google Gemini 2.5 Flash via Mastra model router
- Language: TypeScript, strict mode, ES modules, Node.js 20+
Components:
- 9 registered skills in the A2A Agent Card (visible in Prompt Opinion's marketplace)
- 9 clinical tools + 1 three-step workflow (fetch → analyze → synthesize)
- FHIR R4 client fetching 14 resource types in parallel (including DocumentReference with text content download)
- 6 analysis engines: trends, screenings, flags/medication safety, risk stratification, prior authorization, care narrative
- 2 real-time external APIs: FDA FAERS (adverse events) and RxNorm (ingredient-level duplicate detection)
- Hallucination guard with inline
[src:ResourceType/id]citation verification - Visit intent detection (adapts by specialty: cardiology, psychiatry, endocrinology, etc.)
- 11 indexed clinical skills with 5 reference guideline documents (USPSTF, CDC/ACIP, scoring guides) + BM25 search
- Streamable HTTP MCP server exposing all tools + workflow
- Log viewer (SSE-based) for A2A request tracing
Key Design Decisions:
- Fetch-all-then-synthesize pattern for predictable response times
toolChoice: 'none'in the workflow's synthesis step to prevent recursive tool calls- Document content fetched via URL with FHIR token authentication (not just inline base64)
documentContextfield injected into every specialized tool's output so the LLM reasons about both structured data and clinical notes
Challenges we ran into
A2A Protocol Compatibility — Prompt Opinion uses a proto3 variant of the A2A protocol while the
@a2a-js/sdkimplements the standard spec. We had to build a method-mapping layer (SendMessage→message/send) and a response transformer that wraps SDK output into proto3 task format.Document Authentication — Prompt Opinion stores uploaded documents behind a download URL that requires specific token-based auth. The FHIR token passed via A2A metadata authenticates this endpoint, but discovering this required debugging 401 errors and testing multiple auth strategies (Bearer header, query param, cookie).
Workflow-Agent Circular Dependency — The clinical pipeline workflow's synthesis step calls the same agent that might call the workflow. Solved with
toolChoice: 'none'which forces the LLM to generate text directly without re-invoking tools.Dual SSRI Detection Across Providers — The most compelling demo finding (Sertraline from psychiatrist + Escitalopram from PCP) only exists in uploaded clinical notes, not structured MedicationStatement resources. Required adding DocumentReference fetching, text content download, and injecting document context into every tool's output.
Hallucination in Empty Data — When a patient has no medications, the LLM would say "unable to retrieve data" instead of "no medications found." Fixed with explicit system prompt rules distinguishing "fetch failed" from "data doesn't exist."
Accomplishments that we're proud of
- 93% FHIR citation rate — 26/28 clinical claims in a typical response cite a specific FHIR source
- 5 medication safety issues detected from a single uploaded document — dual SSRI, NSAID/ACE-I interaction, iron/metformin timing, cannabis/SSRI potentiation, polypharmacy
- 7-year vital sign trend analysis — heart rate trajectory mapping to a trauma timeline (67→96→81 bpm correlating with IPV period)
- Real-time FDA pharmacovigilance — adverse event signals from the FAERS database, not a static drug reference
- Zero hallucination on every test — asked about non-existent data 9+ times across 5 patients, never fabricated
- Full workflow end-to-end in Mastra Studio — visible 3-step pipeline with timing metrics
- 9 skills registered in Prompt Opinion's Agent Card — discoverable by any orchestrator
What we learned
- The A2A protocol is powerful but implementations vary — proto3 vs JSON-RPC format differences require adapter layers
- Uploaded documents are where the real clinical gold lives — structured FHIR resources are clean but incomplete; clinical notes contain the cross-provider insights that save lives
- LLM grounding in healthcare requires active enforcement — citation requirements in the system prompt, hallucination guards post-generation, and explicit "data not found" vs "fetch failed" distinction
- Mastra's workspace + skills system (BM25-indexed SKILL.md files with reference documents) provides structured clinical knowledge that improves agent reasoning without fine-tuning
- The difference between "answering a question" and "clinical intelligence" is cross-referencing: medications × allergies × trends × social determinants × guidelines = insight that no single data source provides alone
What's next for Healthcare-AI
- Structured extraction from documents — parse medications, allergies, and diagnoses from clinical notes into proper FHIR resources (currently we pass raw text to the LLM; structured extraction would enable the specialized tools to reason about document data directly)
- Multi-patient batch analysis — Prompt Opinion supports patient groups; running the pipeline across an entire clinic day's patients and prioritizing by risk score
- Clinical Trials matching — Prompt Opinion has a Clinical Trials feature; our risk stratification and condition analysis could power eligibility screening
- Voice interface — Mastra supports TTS/STT; a clinician could ask "what should I know about my next patient?" hands-free while walking to the exam room
- Streaming responses — currently synchronous; streaming would show results progressively (vitals first, then screenings, then medications) for faster time-to-first-insight
- Marketplace publication — publish Medi to Prompt Opinion's agent marketplace so any workspace can install it
Built With
- fda
- google-adk
- mastra
- rxnorm
- typescript
Log in or sign up for Devpost to join the conversation.