How the-momentum/fhir-mcp-server works
Purpose and Runtime Entry Points
This is a Python Model Context Protocol (MCP) server that lets MCP clients/LLM agents perform CRUD operations against a FHIR R4 backend, resolve LOINC terminology, and ingest/search clinical documents through a RAG pipeline. The runtime entry paths are start.py, app/main.py, and the MCP server assembly in app/mcp/v1/mcp.py. It supports stdio and HTTP/HTTPS transport modes and is deployable through Dockerfile and docker-compose.yml.
MCP Tool Boundary
FastMCP-facing tools are grouped by clinical resource type. Resource-specific adapters expose Patient, Observation, Condition, Medication, Encounter, Immunization, AllergyIntolerance, FamilyMemberHistory, and DocumentReference operations; a generic tool covers other FHIR resources. Core paths: app/mcp/v1/mcp.py, app/mcp/v1/tools/patient.py, app/mcp/v1/tools/generic.py. These tools are the public agent-facing boundary and translate tool inputs into service calls and structured errors.
FHIR Integration and Authentication
FHIR communication is centralized behind a client layer rather than implemented in tools. app/services/fhir/fhir_client.py performs backend requests, while app/services/fhir/oauth_client.py obtains OAuth2 credentials and app/services/fhir/token_manager.py manages token lifecycle. Models and backend-specific error handling reside in app/services/fhir/models.py and app/services/fhir/errors.py. The external FHIR server is configured by host/base URL and OAuth client credentials.
Validation and Clinical Data Contracts
Schema modules define the application’s typed boundary for FHIR payloads, documents, vector-store operations, and errors. Relevant paths are app/schemas/fhir_schemas.py, app/schemas/document_schemas.py, and app/schemas/vector_store_schemas.py. MCP tools validate and constrain resource operations before requests cross to the remote FHIR API, protecting the backend from malformed agent-generated payloads.
RAG Document Pipeline
Document ingestion flows from MCP document operations into document parsing/chunking, embedding, and Pinecone indexing. app/services/rag/document_processor.py handles supported TXT, CSV, JSON, and PDF processing; app/services/rag/semantic_embedder.py creates embeddings using the configured Hugging Face model; app/services/rag/vector_store_service.py persists/retrieves vectors. Initialization and direct Pinecone integration are separated in app/services/rag/pinecone_initializer.py and app/services/rag/pinecone_client.py.
Semantic Retrieval Flow
A search request is processed into an embedding-aware query, sent to Pinecone for similarity retrieval, and returned as context suitable for an agent/RAG response. The coordinating service layer is app/services/rag/search_query_processor.py, app/services/rag/document_service.py, and app/services/rag/vector_store_service.py. Pinecone is optional at deployment level but required for semantic document search.
Terminology, Configuration, and Secret Boundary
app/services/loinc_client.py integrates with the external LOINC search API for code lookup/validation. Central configuration is loaded from app/config.py using environment settings represented by config/.env.example; encrypted secret support is implemented via scripts/cryptography/setup_encryption.py. Sensitive FHIR OAuth, LOINC, and Pinecone credentials are deployment-time concerns and should not flow through MCP tool payloads.
Main entry points: mcp.py MCP server assembly · fhir_client.py FHIR client · config.py Configuration · vector_store_service.py Vector store service
Components 18
Runtime
- Runtime entry points · Starts the server in stdio or HTTP/HTTPS modes.
start.py - Application bootstrap
app/main.py
MCP boundary
- MCP server assembly · Public agent-facing tool registration and transport boundary.
app/mcp/v1/mcp.py - FHIR resource tools · Patient and other clinical resource CRUD tools.
app/mcp/v1/tools/patient.py - Generic FHIR tool · Handles FHIR resource types outside dedicated adapters.
app/mcp/v1/tools/generic.py - Typed contracts · Validates FHIR, document, vector-store, and error payloads.
app/schemas/fhir_schemas.py
FHIR integration
- FHIR client · Centralizes requests to the remote FHIR R4 backend.
app/services/fhir/fhir_client.py - OAuth token management · Obtains and refreshes backend OAuth2 credentials.
app/services/fhir/token_manager.py - FHIR R4 backend
Document RAG
- Document service · Coordinates document ingestion and semantic search.
app/services/rag/document_service.py - Document processor · Processes TXT, CSV, JSON, and PDF documents.
app/services/rag/document_processor.py - Semantic embedder · Creates vectors with the configured Hugging Face model.
app/services/rag/semantic_embedder.py - Search query processor · Builds embedding-aware semantic queries.
app/services/rag/search_query_processor.py - Vector store service · Persists vectors and retrieves similarity matches.
app/services/rag/vector_store_service.py - Pinecone · Optional deployment dependency required for semantic search.
Configuration & externals
- LOINC client
app/services/loinc_client.py - LOINC API
- Configuration · Loads environment-based deployment credentials and endpoints.
app/config.py
Connections 25
- Runtime entry points → Application bootstrap: starts
- Application bootstrap → MCP server assembly: boots
- MCP server assembly → FHIR resource tools: registers
- MCP server assembly → Generic FHIR tool: registers
- FHIR resource tools → Typed contracts: validates with
- Generic FHIR tool → Typed contracts: validates with
- FHIR resource tools → FHIR client: FHIR CRUD
- Generic FHIR tool → FHIR client: FHIR CRUD
- FHIR client → OAuth token management: uses credentials
- OAuth token management → FHIR R4 backend: OAuth2
- FHIR client → FHIR R4 backend: FHIR requests
- MCP server assembly → Document service: document tools
- Document service → Document processor: ingests
- Document processor → Semantic embedder: chunks
- Semantic embedder → Vector store service: vectors
- Document service → Search query processor: searches
- Search query processor → Semantic embedder: embeds query
- Search query processor → Vector store service: retrieves
- Vector store service → Pinecone: indexes and queries
- MCP server assembly → LOINC client: terminology lookup
- LOINC client → LOINC API: search and validate
- Configuration → FHIR client: endpoint settings
- Configuration → OAuth token management: client credentials
- Configuration → LOINC client: API credentials
- Configuration → Vector store service: Pinecone settings