Skip to main content
Glama
fhir-mcp-oauth2-demo

fhir-mcp-oauth2-demo

by budityw23

FHIR + MCP: OAuth2 Client Credentials and Scope Enforcement

A small, runnable demo of how an AI agent's MCP server gets a token for a FHIR server, and how the token (not the prompt) limits what the agent can read.

Demo: valid scope returns 200, wrong scope returns 403, token refreshes before expiry

Three containers: Keycloak (authorization server), HAPI FHIR, and a small Python gateway that validates JWTs against Keycloak's JWKS and checks SMART-style system/ scopes.

Prerequisites

  • Docker and Docker Compose

  • Python 3.12+

Related MCP server: Secure Clinical LLM Access via MCP

Run

# 1. Python dependencies
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

# 2. Start the stack and wait for Keycloak to be healthy (~30-60s)
docker compose up -d
docker compose ps        # keycloak should show "healthy"

# 3. Seed HAPI with synthetic data (direct to HAPI on :8084, no auth)
python seed.py

# 4. Run the three cases
python demo.py

What the demo shows

  1. Valid token: client credentials grant with system-Observation.rs scope, Observation search returns 200.

  2. Wrong scope: the same client searches Patients, the gateway returns 403.

  3. Token near expiry: the token lives 60s; TokenClient refreshes it inside a 30s cache margin, before it expires, and the next call returns 200.

demo.py drives TokenClient and the gateway directly. mcp_server.py exposes the same calls as an MCP tool (search_observations) over STDIO.

Scope naming

Keycloak does not allow / in client scope names. The demo uses system-Observation.rs (dash) in Keycloak. The gateway normalizes this to system/Observation.rs when checking. A SMART-native FHIR server would use the canonical / format directly.

Ports

Service

Port

Purpose

Gateway

8083

FHIR requests (scope-enforced)

HAPI

8084

Direct FHIR (no auth, for seed)

Keycloak

8085

Token endpoint, JWKS (admin/admin)

Credentials are demo-only

The client secret demo-secret-not-for-production and the Keycloak admin/admin login exist only for this local stack. All ports bind to 127.0.0.1. Never reuse these values, and in a real deployment load credentials from the environment or a secret manager.

Recording

recording/record-video.sh records a live run with asciinema and renders a GIF with agg. See recording/README.md.

  • LangCare FHIR MCP server, generic OAuth2 client credentials: PR #10 (under review)

Cleanup

docker compose down -v

License

MIT

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables AI applications to query and retrieve healthcare data (patients, conditions, observations, medications) from a public FHIR R4 server via MCP tools.
      MIT
    • A
      license
      A
      quality
      C
      maintenance
      Enables MCP clients to search FHIR R4 patients and observations, access patient data as resources, and generate clinical summaries through prompts, using a public HAPI FHIR sandbox.
      2
      MIT
    • F
      license
      Not graded
      quality
      A
      maintenance
      Enables LLM-based agents and MCP-compatible clients to query and manipulate clinical data on a FHIR-compliant backend through CRUD resource tools, document ingestion, semantic search, and terminology lookup.
      -