Skip to main content
Glama
README.mdโ€ข10.4 kB
# FHIR + GraphRAG Medical Chat System Complete implementation of MCP-based medical search with interactive chat interface. ## ๐ŸŽฏ Quick Start ### 1. Run MCP Server The MCP server exposes 6 medical search tools via stdio transport: ```bash cd /Users/tdyar/ws/FHIR-AI-Hackathon-Kit/mcp-server python3 fhir_graphrag_mcp_server.py ``` ### 2. Run Streamlit Chat Interface Interactive chat UI for medical search: ```bash cd /Users/tdyar/ws/FHIR-AI-Hackathon-Kit/mcp-server streamlit run streamlit_medical_chat.py ``` Access at: http://localhost:8501 ### 3. Connect to Claude Desktop Add to `~/Library/Application Support/Claude/claude_desktop_config.json`: ```json { "mcpServers": { "fhir-graphrag": { "command": "python3", "args": ["/Users/tdyar/ws/FHIR-AI-Hackathon-Kit/mcp-server/fhir_graphrag_mcp_server.py"] } } } ``` ## ๐Ÿ“Š System Architecture ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Frontend Options: โ”‚ โ”‚ โ€ข Streamlit Chat UI โ”‚ โ”‚ โ€ข Claude Desktop โ”‚ โ”‚ โ€ข Continue (VSCode) โ”‚ โ”‚ โ€ข Custom MCP clients โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ MCP Protocol (JSON-RPC 2.0) โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ MCP Server (fhir_graphrag_mcp_server.py) โ”‚ โ”‚ โ€ข 6 medical search tools โ”‚ โ”‚ โ€ข stdio transport โ”‚ โ”‚ โ€ข 563 lines Python โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ IRIS Native API (TCP) โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ AWS IRIS (3.84.250.46:1972) โ”‚ โ”‚ โ€ข SQLUser.FHIRDocuments (51 docs) โ”‚ โ”‚ โ€ข SQLUser.Entities (83) โ”‚ โ”‚ โ€ข SQLUser.EntityRelationships (540)โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` ## ๐Ÿ”ง MCP Tools ### 1. search_fhir_documents Full-text search of FHIR clinical notes **Parameters**: - `query` (string): Search terms - `limit` (integer): Max results (default: 10) **Example**: ```python result = await call_tool("search_fhir_documents", { "query": "chest pain", "limit": 5 }) ``` **Returns**: ```json { "query": "chest pain", "results_count": 2, "documents": [ { "fhir_id": "1474", "preview": "Otitis Media Evaluation...", "relevance": "matches: pain" } ] } ``` ### 2. search_knowledge_graph Search medical entities in knowledge graph **Parameters**: - `query` (string): Entity search terms - `limit` (integer): Max entities (default: 5) **Example**: ```python result = await call_tool("search_knowledge_graph", { "query": "fever vomiting", "limit": 3 }) ``` **Returns**: ```json { "query": "fever vomiting", "entities_found": 2, "entities": [ { "id": 42, "text": "fever", "type": "SYMPTOM", "confidence": 0.75 }, { "id": 58, "text": "vomiting", "type": "SYMPTOM", "confidence": 0.85 } ], "documents_found": 3, "documents": [...] } ``` ### 3. hybrid_search Combined FHIR + GraphRAG with RRF fusion **Parameters**: - `query` (string): Medical search query - `top_k` (integer): Number of results (default: 5) **Example**: ```python result = await call_tool("hybrid_search", { "query": "respiratory infection", "top_k": 5 }) ``` **Returns**: ```json { "query": "respiratory infection", "fhir_results": 5, "graphrag_results": 1, "fused_results": 5, "top_documents": [ { "fhir_id": "1474", "rrf_score": 0.0328, "sources": ["fhir", "graphrag"] } ] } ``` ### 4. get_entity_relationships Multi-hop graph traversal **Parameters**: - `entity_text` (string): Starting entity - `max_depth` (integer): Max depth 1-3 (default: 2) ### 5. get_document_details Retrieve full FHIR document **Parameters**: - `fhir_id` (string): Document ID ### 6. get_entity_statistics Knowledge graph statistics **Parameters**: None **Returns**: ```json { "total_entities": 83, "total_relationships": 540, "entity_distribution": [ {"type": "TEMPORAL", "count": 43}, {"type": "SYMPTOM", "count": 21}, {"type": "BODY_PART", "count": 10}, {"type": "CONDITION", "count": 6}, {"type": "MEDICATION", "count": 2}, {"type": "PROCEDURE", "count": 1} ] } ``` ## ๐Ÿ’ฌ Streamlit Chat Interface ### Features - **Intelligent Tool Selection**: Automatically chooses the right tool based on query - **Interactive UI**: Chat-style interface with example queries - **Real-time Stats**: Live knowledge graph statistics - **Tool Results**: View raw JSON responses - **Entity Visualization**: Color-coded entity type badges - **Document Previews**: Clinical note snippets ### Example Queries - "Search for patients with chest pain" - "Find entities related to fever" - "What respiratory conditions are in the knowledge graph?" - "Show me document 1474" - "Use hybrid search for respiratory infection" ### Query Patterns The chat interface intelligently routes queries: - Contains "hybrid" or "combined" โ†’ `hybrid_search` - Contains "entity" or "knowledge graph" โ†’ `search_knowledge_graph` - Contains "document" + numbers โ†’ `get_document_details` - Default โ†’ `search_fhir_documents` ## ๐Ÿ“ฆ Installation ### Prerequisites ```bash # Python 3.11+ python3 --version # Required packages pip install intersystems-iris streamlit mcp ``` ### Database Access The system connects to AWS IRIS: - **Host**: 3.84.250.46 - **Port**: 1972 - **Namespace**: USER - **Credentials**: _SYSTEM / SYS ## ๐Ÿ“š Data Contents ### FHIR Documents (51 total) - Synthea-generated clinical notes - Hex-encoded in DocumentReference resources - Various medical encounters and conditions ### Knowledge Graph (83 entities, 540 relationships) - **TEMPORAL** (43): Dates and time references - **SYMPTOM** (21): fever, vomiting, diarrhea, pain, fatigue - **BODY_PART** (10): respiratory, abdomen, chest - **CONDITION** (6): infections, diseases - **MEDICATION** (2): prescriptions - **PROCEDURE** (1): medical procedures ### High-Confidence Entities - discomfort, tenderness, vomiting, diarrhea - respiratory, abdomen - Various temporal markers ## ๐Ÿงช Testing ### Test Tool Discovery ```bash python3 test_mcp_server_tools.py ``` Expected output: ``` Found 6 tools: ------------------------------------------------------------ โ€ข search_fhir_documents โ€ข search_knowledge_graph โ€ข hybrid_search โ€ข get_entity_relationships โ€ข get_document_details โ€ข get_entity_statistics โœ“ All expected tools found! ``` ### Test Tool Execution ```bash python3 test_mcp_tool_execution.py ``` Expected output: ``` Testing MCP Tool Execution... ============================================================ 1. Testing get_entity_statistics... โœ“ Total entities: 83 โœ“ Total relationships: 540 2. Testing search_knowledge_graph... โœ“ Found 1 entities โœ“ Related to 1 documents 3. Testing search_fhir_documents... โœ“ Found 2 documents 4. Testing hybrid_search... โœ“ FHIR results: 5 โœ“ GraphRAG results: 1 โœ“ Fused results: 5 โœ“ All tool execution tests PASSED! ``` ## ๐Ÿ” Technical Details ### Reciprocal Rank Fusion (RRF) Hybrid search combines rankings using RRF: ```python k = 60 # RRF constant rrf_score = 0.0 if fhir_rank: rrf_score += 1.0 / (k + fhir_rank) if graph_rank: rrf_score += 1.0 / (k + graph_rank) ``` Documents appearing in both FHIR and GraphRAG results get boosted scores. ### Hex-Encoded Clinical Notes FHIR DocumentReference stores notes as hex strings: ```python encoded_data = resource_json['content'][0]['attachment']['data'] clinical_note = bytes.fromhex(encoded_data).decode('utf-8') ``` ### Fuzzy Entity Matching Entity search uses SQL LIKE for flexible matching: ```sql WHERE LOWER(EntityText) LIKE '%fever%' ``` ## ๐Ÿ“ Files ``` mcp-server/ โ”œโ”€โ”€ fhir_graphrag_mcp_server.py # Main MCP server (563 lines) โ”œโ”€โ”€ streamlit_medical_chat.py # Chat interface โ”œโ”€โ”€ test_mcp_server_tools.py # Tool discovery test โ”œโ”€โ”€ test_mcp_tool_execution.py # Integration test โ”œโ”€โ”€ debug_mcp_response.py # Debug helper โ””โ”€โ”€ README.md # This file /Users/tdyar/ws/FHIR-AI-Hackathon-Kit/ โ”œโ”€โ”€ MCP_SERVER_COMPLETE.md # Detailed documentation โ”œโ”€โ”€ STATUS.md # Project status โ”œโ”€โ”€ PROGRESS.md # Development log โ””โ”€โ”€ TODO.md # Task list ``` ## ๐Ÿš€ Next Steps ### 1. Medical Image Integration Add MIMIC-CXR image support: ```python # Query images sql = """ SELECT ImageID, StudyID, SubjectID, ViewPosition, ImagePath FROM SQLUser.MIMICImages WHERE StudyID = ? """ # Return image thumbnails in tool responses { "fhir_id": "1474", "clinical_note": "...", "images": [ { "image_id": "abc123", "view": "PA", "thumbnail_url": "/images/abc123_thumb.jpg" } ] } ``` ### 2. Knowledge Graph Visualization Add interactive graph visualization using: - Cytoscape.js - vis.js - D3.js force-directed graphs ### 3. Advanced Search - Temporal queries (date ranges) - Medication interactions - Patient cohort identification - Condition co-occurrence analysis ## ๐Ÿ“ License Part of FHIR-AI-Hackathon-Kit - InterSystems demo project ## ๐Ÿค Contributing This is a demo/prototype system. For production use: 1. Add authentication/authorization 2. Implement rate limiting 3. Add comprehensive error handling 4. Set up monitoring and logging 5. Deploy with proper security configurations ## ๐Ÿ“ž Support For issues or questions: - Check MCP_SERVER_COMPLETE.md for detailed documentation - Review test scripts for usage examples - Inspect tool responses with debug_mcp_response.py --- **Status**: โœ… MCP server operational, Streamlit UI ready, 6 tools tested **Last Updated**: 2025-11-18

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/isc-tdyar/medical-graphrag-assistant'

If you have feedback or need assistance with the MCP directory API, please join our Discord server