Wednesday, 17 September 2025

Extending our Patient Education Chatbot into a Patient-Aware Conversational Agent with MCP & FHIR

We are now extending our Patient Education Chatbot into a Patient-Aware Conversational Agent that uses FHIR data as personalized context using MCP (Model Context Protocol)

Please read previous articles for more context:

  1. Building a Voice-Based Health Education Assistant with React, AWS, and ChatGPT
  2. Extending a Patient Education Chatbot with RAG for Trusted Health Information


Healthcare chatbots are quickly moving beyond generic Q&A into patient-specific conversational agents.

Imagine a patient asking:

“What should I know about managing my diabetes based on my recent HbA1c test?”

A basic chatbot might explain what HbA1c means, but a patient-aware chatbot could personalize the answer:

  • Retrieve the patient’s lab results, diagnoses, allergies, and medications directly from their EHR (FHIR API).

  • Combine this patient context with trusted educational sources (Cleveland Clinic, Mayo Clinic, HealthDirect).

  • Generate a safe, tailored response via a Large Language Model (LLM).


This post explains how to extend a React + LangGraph + LangChain chatbot with an MCP server that fetches normalized FHIR data, while staying mindful of HIPAA and data security.


Why MCP + FHIR?

  • FHIR (Fast Healthcare Interoperability Resources): the industry standard for structured health data (diagnoses, labs, meds, allergies).

  • MCP (Medical Context Provider): a middleware layer between your chatbot backend and FHIR server. It:

    • Normalizes raw FHIR JSON into a clean format.

    • Masks or de-identifies PHI where needed.

    • Provides a consistent API for LangChain / LangGraph workflows.

This ensures your chatbot can reason with structured, normalized, patient-specific context instead of raw EHR data dumps.


High-Level Architecture

Here’s the flow:

  1. React App → Patient asks a question (voice (via AWS Transcribe) /text).

  2. LangGraph → Orchestrates workflow.

  3. MCP Server → Fetches & normalizes patient data from FHIR Server.

  4. LangChain RAG → Retrieves trusted medical content from Pinecone/OpenSearch.

  5. LLM (OpenAI) → Combines query + patient context + knowledge chunks.

  6. LangSmith → Observability/tracing.

  7. Polly (optional) → Converts answer back to speech.





Implementation Considerations

1. FHIR Normalization via MCP

  • Parse raw FHIR JSON into simple JSON:

    { "age": "52", "sex": "male", "diagnoses": ["Type 2 Diabetes", "Hypertension"], "labs": ["HbA1c: 9.2%", "LDL: 140 mg/dL"], "medications": ["Metformin", "Lisinopril"], "allergies": ["Penicillin"] }
  • Strip unnecessary identifiers (name, MRN, DOB) before sending downstream.

2. Retrieval-Augmented Generation (RAG)

  • Crawl and embed trusted health education websites (Cleveland, Mayo, HealthDirect).

  • Store embeddings in Pinecone

  • Retrieve top-N passages relevant to the patient’s query.

3. Prompt Construction

Example combined prompt:

You are a patient education assistant. Use ONLY the context below. Patient (de-identified): - Age: 52, Male - Diagnoses: Type 2 Diabetes, Hypertension - Labs: HbA1c 9.2% (poor control) - Medications: Metformin, Lisinopril - Allergies: Penicillin Trusted sources: [Excerpt 1: WHO guidelines on diabetes management...] [Excerpt 2: Mayo Clinic article on HbA1c...] Question: What should I know about my condition?

4. Compliance & Security

  • Keep PHI inside your VPC when possible.

  • Use TLS 1.2+, KMS for encryption, IAM least privilege.

  • Log patient consent before pulling FHIR data.

  • Add human-in-loop fallback for uncertain answers.


Benefits of This Approach

  • Personalized: Answers tailored to the patient’s actual health data.

  • Grounded: RAG ensures responses come from trusted sources.

  • Safer: De-identification + HIPAA-aligned architecture.

  • Traceable: LangSmith logs all steps for debugging & audit.


Closing Thoughts

By integrating MCP + FHIR context with your chatbot, you transform it from a generic health Q&A bot into a patient-aware digital assistant.

This hybrid model — combining patient context, trusted knowledge, and LLM reasoning — can empower patients with safe, reliable education while keeping sensitive data protected.

The future of digital health assistants isn’t just about answering questions. It’s about personalized, compliant, and context-aware guidance that patients (and clinicians) can trust.




Tuesday, 9 September 2025

Extending a Patient Education Chatbot with RAG for Trusted Health Information

In healthcare, trust is everything. While conversational AI is powerful for engaging patients, it also introduces a serious risk: hallucination. If a chatbot makes up information or provides unsafe medical advice, it can harm patients and erode trust.

To address this, we can extend a voice-enabled patient education chatbot that we discussed in previous article with Retrieval-Augmented Generation (RAG). Instead of letting the model freely generate answers, we ground it in trusted health education websites such as:

  • Mayo Clinic
  • Health Direct Australia
  • Cleveland Clinic

What is RAG (Retrieval-Augmented Generation)?

RAG is a pattern where:

  1. The patient asks a question.

  2. The system retrieves relevant passages from a knowledge base (vector database).

  3. The retrieved content is fed into the LLM prompt.

  4. The chatbot generates an answer grounded in those trusted sources.

This prevents hallucination and allows the system to cite sources in responses.


In this article we are only focusing on RAG implementation. We have already covered other steps in previous article.


Data Flow Explanation

  1. User (ReactApp)

    • Patient asks a health question via text/voice.

    • ReactApp sends the request to the backend.

  2. LangGraph (Workflow Orchestration Layer)

    • Orchestrates the flow of nodes (e.g., retrieval, validation, LLM).

    • Passes the query into the LangChain pipeline.

  3. LangChain (Core RAG Logic)

    • Handles embeddings, retrieval, and prompt construction.

    • Calls vector database (Pinecone which is already populated with indexed datasets from trusted sources) for context.

    • Prepares final prompt = {query + context} for LLM.

  4. LangSmith (Observability/Tracing)

    • Logs requests, responses, latency, errors.

    • Helps debug and monitor RAG performance.

  5. OpenAI (LLM API)

    • Receives grounded prompt.

    • Generates response.

    • Returns text back through LangChain → LangGraph → ReactApp.






For the purpose of this demo, I only extracted the following pages related to Diabetes from the trusted sources i.e. 
Mayo Clinic, HealthDirect, Cleveland Clinic. No Web Scrapping is done for this. We will only ask the questions related to diabetes to our chatbot.



Sample App Screenshot:



Sample LLM Question & Response:


Question:
What are the common health problems associated with diabetes, 
and what can I do to prevent them?

Response:
Common health problems associated with diabetes include 
heart disease, stroke, kidney disease, and nerve issues. 
To prevent these complications, focus on managing 
your blood glucose levels, maintaining a healthy diet, 
exercising regularly, and attending regular health check-ups. 
Additionally, avoid smoking and ensure proper dental 
and foot care to reduce risks.

Source(s):

URL: https://www.mayoclinic.org/diseases-conditions/diabetes/diagnosis-treatment/drc-20371451
Lines: 364 to 366

URL: https://www.healthdirect.gov.au/type-1-diabetes
Lines: 123 to 144



Validating Responses

To make sure the assistant does not hallucinate:

  • 🔗 Always cite sources in responses.

  • 🛑 Add a fallback: “I don’t know. Please consult your doctor.” if no relevant documents are found.

  • ✅ Add a semantic similarity check (Not implemented in this demo) to ensure the generated answer aligns with retrieved documents. 


Benefits of RAG in Healthcare

  • Accuracy: Answers come from trusted medical sources.

  • Transparency: Patients see citations.

  • Compliance: Reduces legal and ethical risks.

  • Scalability: Easily update embeddings when new health guidelines are published.


Monday, 1 September 2025

Building a Voice-Based Health Education Assistant with React, AWS, and ChatGPT

Imagine a patient speaking into a mobile app and instantly receiving a clear, natural-sounding explanation about their health concern. With advances in speech recognition, natural language processing, and text-to-speech, this is no longer futuristic— we will see a demo of this implementation.

In this post, we’ll explore how to architect and implement a voice AI chatbot for health education that:

  1. Listens to a user’s health question.

  2. Transcribes it into text.

  3. Queries an AI model (ChatGPT) for an answer.

  4. Converts the response back into natural speech.

  5. Plays it back to the patient.


Why Voice for Health Education?

  • Accessibility: Voice removes literacy barriers and makes health information more inclusive.

  • Convenience: Patients can interact hands-free.

  • Engagement: Natural conversations feel more intuitive than reading long articles.


High-Level Architecture

Here’s the flow of the system:

  1. User speaks into a React app.

  2. The app streams the audio via Socket.IO to AWS Transcribe.

  3. AWS Transcribe converts speech to text and returns it.

  4. The transcription is sent to ChatGPT API.

  5. ChatGPT generates a patient-friendly answer.

  6. The answer text is sent to AWS Polly.

  7. Polly generates natural voice audio.

  8. The React app plays the audio back to the user.






Demo Video





Note: This architecture is for demo purpose and not for production use.


Popular Posts