Overview
Problem Statement
Access to care is slow and fragmented — waiting rooms, scattered paper records, and no easy way to get quick guidance between visits.
Solution
Mediflow connects patients and doctors for virtual care: on-demand video consultations, an AI assistant for general guidance, automatic prescription digitization, and a single dashboard to track health records. Doctors get a verified onboarding flow, a scheduling dashboard, and a clinical AI assistant that can pull a patient's real medical history live during a conversation — with the visit automatically transcribed so nothing gets lost.
Architecture Overview
Mediflow is composed of seven independently deployable services sharing one Firebase project as the single source of truth. Every service verifies caller identity independently via the Firebase Admin SDK — there is no central session store, and no service trusts another service's prior check. Firestore's real-time listeners double as the event/pub-sub layer, coordinating call status and cross-service state without a message broker.
Features
Search doctors by specialty with live appointment slot availability
One-click video/audio consultations, no downloads
"Medora" AI health assistant with safety guardrails and emergency escalation
Prescription upload with automatic client-side OCR digitization
Fitness and recovery tracking (workouts, diet, macros)
Centralized health records dashboard
Doctor onboarding with document upload and admin approval
Doctor-facing Medora AI assistant with MCP tool-calling into real patient records
Automatic call transcription in free or high-accuracy Whisper mode, visible only to the doctor
Architecture Diagrams
System Architecture
- Patient and Provider frontends (Next.js / React+Vite) talk to five independently deployable backend services over REST, each authenticated with a Firebase ID token.
- Patient Backend (:5000), Provider Backend (:5001), Call Service (:4000), AI Gateway (:5003), and MCP Server (:5002) all read and write through one shared Firestore instance — the single source of truth.
- Cloudinary handles media storage, LiveKit Cloud powers video calls, and Firebase Cloud Messaging delivers push notifications as supporting infrastructure around the core services.
Authentication Flow
- The client signs in via Firebase Auth and attaches the resulting ID token to every API request.
- Each backend service independently verifies that token with the Firebase Admin SDK before processing the request.
- No central session store, and no service trusts another service's prior check — invalid or expired tokens are rejected at every boundary, not just the first one.
Video Call Sequence
- The doctor starts the call from the appointment dashboard; the Call Service verifies ownership of that appointment before doing anything else.
- A LiveKit room scoped to the appointment is created (capped at 2 participants), and Firestore's call status flips to active in real time.
- The patient's frontend picks up the status change instantly via a Firestore listener and joins with its own scoped access token — no polling involved.
AI Gateway Internals
- One AI Gateway service fronts three interchangeable LLM providers — Claude, Gemini, and OpenRouter/GPT — selected per deployment via environment variable.
- Patient-facing chat is intentionally limited to general guidance, with safety guardrails that detect emergency language and escalate.
- Doctor-facing chat gets access to real data tools via MCP — the same Gateway code path branches based on who's asking.
MCP Tool-Calling Flow
- When the doctor's AI assistant decides it needs patient data (e.g. current medications), the AI Gateway calls the MCP Server with the doctor's own ID token attached.
- The MCP Server independently re-verifies that token — even though the Gateway already checked it — before touching Firestore.
- Tool results flow back through the same provider adapter and into the model's final answer, streamed to the doctor in real time.
Call Transcript Flow
- Free mode: each participant's browser transcribes its own audio locally via the Web Speech API, and segments are merged by timestamp after the call.
- Accurate mode: the doctor's browser mixes local and remote audio into a single stream, uploads it, and the AI Gateway transcribes it with OpenAI Whisper.
- Either way, the finished transcript is saved to the patient's chart and is visible only to the doctor.
Target Architecture at 1M Users
- At roughly 1M registered users (~10K peak concurrent), a CDN handles static assets/media and a new API Gateway / Load Balancer sits in front of every autoscaled service.
- A new Redis cache absorbs hot reads (directories, appointment slots), and a new pub/sub message queue offloads transcript-merge and AI-analysis jobs to async workers instead of running them inline.
- Firestore moves to sharded counters and event-based writes, LiveKit Cloud auto-scales its SFU media servers, and the MCP Server gets automatic failover across Claude, Gemini, and OpenRouter.
Implementation Workflows
Technology Stack
Patient Backend
Patient Frontend
Provider Backend
Provider Frontend
Call Service
AI Gateway
MCP Server
Shared Infrastructure
CI/CD Pipeline
- Lint & type-check on every push and PR
- Build (next build / vite build / plain Node per service)
- Run each service's test suite
- Deploy a PR preview environment for review
- On merge to main: frontends to Vercel, backends to Render/Cloud Run, Firestore rules/indexes via Firebase CLI
- Isolated dev/staging/production Firebase projects
Case Studies
The Patient With No Time to Wait
Sarah feels unwell but can't get a same-day GP appointment. She opens Mediflow, describes her symptoms to Medora, the AI health assistant, and gets general guidance plus a recommendation to see a doctor. She books a same-day video consultation with an available specialist in under a minute. During the call, her doctor pulls up her medication history instantly via the MCP-powered clinical assistant instead of asking her to repeat it.
The Doctor Who Never Loses Context
Dr. Ahmed sees a returning patient. Before the call even starts, he asks the doctor-facing AI assistant for a quick summary of the patient's diagnoses and current medications — the assistant queries real Firestore data live via MCP tools, not a stale cached summary. After the visit, the call is automatically transcribed and saved to the patient's chart.
Digitizing a Decade of Paper Prescriptions
A patient with years of paper prescriptions photographs each one. Mediflow's client-side OCR extracts medicine names and dosages directly in the browser, turning unreadable handwriting photos into structured, searchable records without ever uploading the raw image for that first pass.
Engineering Highlights
- Multi-LLM provider abstraction — Claude, Gemini, and OpenRouter are interchangeable behind one adapter layer; adding a new LLM provider doesn't touch business logic.
- Hard patient/doctor AI boundary — the patient chat has zero data-tool access; the doctor chat does, via MCP. This is an architectural boundary, not a prompt-level rule.
- Defense-in-depth authentication — every service re-verifies the caller's identity independently, including the MCP server re-checking a token the Gateway already validated.
- Firestore as the event layer — real-time listeners handle call status and cross-service coordination without a message broker.
- Dual-mode transcription — a free, fully client-side mode and a higher-accuracy server-side Whisper mode, selectable per call.
- Deliberately simple today, scalable by design — no premature infrastructure (cache, queue, gateway), with a documented path to add each once real load justifies it.
