# the-momentum/fhir-mcp-server architecture

> Architecture diagram of the GitHub repository the-momentum/fhir-mcp-server, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/the-momentum/fhir-mcp-server
- Repository: https://github.com/the-momentum/fhir-mcp-server

## Overview

#### 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: [`app/mcp/v1/mcp.py`](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/mcp/v1/mcp.py) (MCP server assembly), [`app/services/fhir/fhir_client.py`](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/fhir/fhir_client.py) (FHIR client), [`app/config.py`](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/config.py) (Configuration), [`app/services/rag/vector_store_service.py`](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/rag/vector_store_service.py) (Vector store service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_entry["Runtime entry points<br/>Python launchers<br/>[start.py]"]
  node_main["Application bootstrap<br/>app runtime<br/>[main.py]"]
end

subgraph group_mcp["MCP boundary"]
  node_mcp_server{{"MCP server assembly<br/>FastMCP server<br/>[mcp.py]"}}
  node_resource_tools["FHIR resource tools<br/>MCP adapters<br/>[patient.py]"]
  node_generic_tool["Generic FHIR tool<br/>MCP adapter<br/>[generic.py]"]
  node_contracts["Typed contracts<br/>schemas<br/>[fhir_schemas.py]"]
end

subgraph group_fhir["FHIR integration"]
  node_fhir_client["FHIR client<br/>backend client<br/>[fhir_client.py]"]
  node_oauth["OAuth token management<br/>auth client<br/>[token_manager.py]"]
  node_fhir_backend[("FHIR R4 backend<br/>external API")]
end

subgraph group_rag["Document RAG"]
  node_document_service["Document service<br/>RAG coordinator"]
  node_document_processor["Document processor<br/>parser and chunker"]
  node_embedder["Semantic embedder<br/>embedding service"]
  node_query_processor["Search query processor<br/>retrieval coordinator"]
  node_vector_store["Vector store service<br/>vector repository"]
  node_pinecone[("Pinecone<br/>external vector database")]
end

subgraph group_platform["Configuration &amp; externals"]
  node_loinc["LOINC client<br/>terminology client<br/>[loinc_client.py]"]
  node_loinc_api(("LOINC API<br/>external terminology API"))
  node_config["Configuration<br/>settings and secrets<br/>[config.py]"]
end

node_entry -->|"starts"| node_main
node_main -->|"boots"| node_mcp_server
node_mcp_server -->|"registers"| node_resource_tools
node_mcp_server -->|"registers"| node_generic_tool
node_resource_tools -->|"validates with"| node_contracts
node_generic_tool -->|"validates with"| node_contracts
node_resource_tools -->|"FHIR CRUD"| node_fhir_client
node_generic_tool -->|"FHIR CRUD"| node_fhir_client
node_fhir_client -->|"uses credentials"| node_oauth
node_oauth -->|"OAuth2"| node_fhir_backend
node_fhir_client -->|"FHIR requests"| node_fhir_backend
node_mcp_server -->|"document tools"| node_document_service
node_document_service -->|"ingests"| node_document_processor
node_document_processor -->|"chunks"| node_embedder
node_embedder -->|"vectors"| node_vector_store
node_document_service -->|"searches"| node_query_processor
node_query_processor -->|"embeds query"| node_embedder
node_query_processor -->|"retrieves"| node_vector_store
node_vector_store -->|"indexes and queries"| node_pinecone
node_mcp_server -->|"terminology lookup"| node_loinc
node_loinc -->|"search and validate"| node_loinc_api
node_config -.->|"endpoint settings"| node_fhir_client
node_config -.->|"client credentials"| node_oauth
node_config -.->|"API credentials"| node_loinc
node_config -.->|"Pinecone settings"| node_vector_store

click node_entry "https://github.com/the-momentum/fhir-mcp-server/blob/main/start.py"
click node_main "https://github.com/the-momentum/fhir-mcp-server/blob/main/app/main.py"
click node_mcp_server "https://github.com/the-momentum/fhir-mcp-server/blob/main/app/mcp/v1/mcp.py"
click node_resource_tools "https://github.com/the-momentum/fhir-mcp-server/blob/main/app/mcp/v1/tools/patient.py"
click node_generic_tool "https://github.com/the-momentum/fhir-mcp-server/blob/main/app/mcp/v1/tools/generic.py"
click node_contracts "https://github.com/the-momentum/fhir-mcp-server/blob/main/app/schemas/fhir_schemas.py"
click node_fhir_client "https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/fhir/fhir_client.py"
click node_oauth "https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/fhir/token_manager.py"
click node_document_service "https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/rag/document_service.py"
click node_document_processor "https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/rag/document_processor.py"
click node_embedder "https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/rag/semantic_embedder.py"
click node_query_processor "https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/rag/search_query_processor.py"
click node_vector_store "https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/rag/vector_store_service.py"
click node_loinc "https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/loinc_client.py"
click node_config "https://github.com/the-momentum/fhir-mcp-server/blob/main/app/config.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_main toneBlue
class node_mcp_server,node_resource_tools,node_generic_tool,node_contracts toneAmber
class node_fhir_client,node_oauth,node_fhir_backend toneMint
class node_document_service,node_document_processor,node_embedder,node_query_processor,node_vector_store,node_pinecone toneRose
class node_loinc,node_loinc_api,node_config toneIndigo
```

## Components

### Runtime

- [Runtime entry points](https://github.com/the-momentum/fhir-mcp-server/blob/main/start.py): Python launchers · Starts the server in stdio or HTTP/HTTPS modes. · `start.py`
- [Application bootstrap](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/main.py): app runtime · `app/main.py`

### MCP boundary

- [MCP server assembly](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/mcp/v1/mcp.py): FastMCP server · Public agent-facing tool registration and transport boundary. · `app/mcp/v1/mcp.py`
- [FHIR resource tools](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/mcp/v1/tools/patient.py): MCP adapters · Patient and other clinical resource CRUD tools. · `app/mcp/v1/tools/patient.py`
- [Generic FHIR tool](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/mcp/v1/tools/generic.py): MCP adapter · Handles FHIR resource types outside dedicated adapters. · `app/mcp/v1/tools/generic.py`
- [Typed contracts](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/schemas/fhir_schemas.py): schemas · Validates FHIR, document, vector-store, and error payloads. · `app/schemas/fhir_schemas.py`

### FHIR integration

- [FHIR client](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/fhir/fhir_client.py): backend client · Centralizes requests to the remote FHIR R4 backend. · `app/services/fhir/fhir_client.py`
- [OAuth token management](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/fhir/token_manager.py): auth client · Obtains and refreshes backend OAuth2 credentials. · `app/services/fhir/token_manager.py`
- **FHIR R4 backend**: external API

### Document RAG

- [Document service](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/rag/document_service.py): RAG coordinator · Coordinates document ingestion and semantic search. · `app/services/rag/document_service.py`
- [Document processor](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/rag/document_processor.py): parser and chunker · Processes TXT, CSV, JSON, and PDF documents. · `app/services/rag/document_processor.py`
- [Semantic embedder](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/rag/semantic_embedder.py): embedding service · Creates vectors with the configured Hugging Face model. · `app/services/rag/semantic_embedder.py`
- [Search query processor](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/rag/search_query_processor.py): retrieval coordinator · Builds embedding-aware semantic queries. · `app/services/rag/search_query_processor.py`
- [Vector store service](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/rag/vector_store_service.py): vector repository · Persists vectors and retrieves similarity matches. · `app/services/rag/vector_store_service.py`
- **Pinecone**: external vector database · Optional deployment dependency required for semantic search.

### Configuration & externals

- [LOINC client](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/services/loinc_client.py): terminology client · `app/services/loinc_client.py`
- **LOINC API**: external terminology API
- [Configuration](https://github.com/the-momentum/fhir-mcp-server/blob/main/app/config.py): settings and secrets · Loads environment-based deployment credentials and endpoints. · `app/config.py`

## Connections

- 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
