MedConnect Layer by Layer

Telling an auditor "we take privacy seriously" is easy. Showing them exactly where, in the code, each protection lives is harder. MedConnect, the book's healthcare case study, is organised so that the second answer is short: go to the layer, and look. Here is that walk, from the bottom of the stack to the top.

The foundation: schemas and utilities

Schema (0.3) marks every field that counts as protected health information. In the book's approach, each such field carries a machine-readable tag with a category prefix, so that downstream layers can apply rules automatically. A sketch of the idea, with fields of my own choosing:

const Referral = z.object({
  referralId: z.string().uuid(),
  patientName: z.string().describe("PHI:name"),
  reason: z.string().describe("PHI:diagnosis"),
  clinicCode: z.string(),            // not PHI: no tag
});

This layer is the least volatile because the definition of PHI and the structure of FHIR resources change over multi-year regulatory cycles.

Utils (0.4) holds pure, stateless security primitives that act on those tags: masking rules for each category, authenticated field-level encryption that records which key version was used, and a minimum-necessary filter that maps each role to the fields it may see. Because these are pure functions, they can be tested exhaustively, and tool engineers call them rather than inventing their own.

The boundary: resources

Resources (0.6) is where the platform meets databases and outside systems. A patient repository checks consent before every read, filters the result to the permitted fields, and records every access, denial and modification. Storage is encrypted. The FHIR client connects over mutual TLS, logs each external call and validates the response against a schema before anything else sees it. The volatility is moderate because drivers, vendor APIs and third-party contracts change at a middling pace.

The decisions: tools

Tools (0.8) hold the clinical workflow. Each tool wraps its logic with audit logging, consent verification and access control, so that no clinical action happens without a traceable, authorised context. The book's prescribing tool also contains a safety gate: it checks drug interactions and, on a contraindication, records the decision and refuses to proceed. A smaller sketch of the same shape, in another workflow:

export async function releaseLabResult(input, ctx, deps) {
  await deps.audit.toolInvoked("releaseLabResult", ctx);
  const result = await deps.labRepo.findById(input.resultId, ctx);
  if (!result) throw new AccessDeniedError();   // L3 enforced consent and fields
  if (result.abnormal) await deps.notifier.alertProvider(result, ctx);
  await deps.audit.disclosure(result.resultId, ctx);
  return result;
}

Tools are volatile because clinical workflows, business rules and regulatory interpretations move with every guideline revision or formulary change.

The edge: API and UI

API (0.85) layers several authentication and authorisation mechanisms: token verification against an expected issuer, audience and age, scope enforcement, and a record of every request that notes whether PHI was touched and which fields. Endpoints, protocol updates and rate-limiting policy change often, which is why this sits above Tools.

UI (1.0), the clinician portal, is where data finally reaches a screen. The lower layers decide who may receive which fields, so the portal's job starts on arrival. One example from the book: the API sends sensitive identifiers already masked, so the browser never holds a full value only to hide it. Revealing one is a separate, audited request. The portal's rendering by role is described as a courtesy, not a control, because the filter below has already withheld the fields.

The takeaway

Read bottom to top, the stack tells a consistent story: tag the data, transform it safely, guard the boundary, make decisions accountable, authenticate the caller, and show only what the task needs. Each step is in a place a reviewer can find. For the bigger picture, see LVD for regulated systems, and for the Resources layer in depth, isolating an EHR vendor behind the Resources layer. The controls that cut across these layers are covered in security across the layers.

What the book covers

  • Full code for PHI annotations, masking rules and field encryption
  • The role-based minimum-necessary permission table
  • A consent-checking patient repository with audit calls
  • The prescribing tool with its drug-interaction safety gate
  • The API gateway's token, scope and request-logging middleware