Authoring Carefold Connectors
Carefold connectors integrate external patient devices, calendars, and health record portals into the local-first runtime while preserving strict egress isolation, clinical provenance, and prompt-injection taint boundaries.
1. Architecture Overview
Every connector in libs/connectors/ consists of two parts:
- Metadata-Only Declarative Manifest (
*.yaml): Validated byConnectorManifest(connectors.manifest). Declares allowed outbound hosts, schemes, ports, authentication mechanism (oauth2with PKCE orbasic), tool risk classifications, and data provenance trust level (trustedvsuntrusted). - Protocol Implementation (
Connector): A Python class implementingmanifest,streams(),fetch(ctx, stream, cursor, http),map(page), andtools().
2. Declarative Manifest (connector.yaml)
Manifests are strictly declarative and never contain executable code or credentials:
id: withings
version: 0.1.0
name: Withings Health Mate
description: Ingests smart scale weight, blood pressure, heart rate, and SpO2 readings.
categories:
- vitals
entrypoint: connectors.starters.withings:WithingsConnector
data_flows:
allowed_hosts:
- wbsapi.withings.net
- account.withings.com
allowed_schemes:
- https
allowed_ports:
- 443
egress_direction: egress_only
pii_types_transferred:
- vitals
auth:
type: oauth2
pkce: true
client_source: byo
token_rotation: true
rotating_refresh: true
scopes:
- user.metrics
trust:
publisher: Spectrayan
verified: true
sandbox_level: strict
trust_level: trusted
terms:
license: Apache-2.0
ai_use_allowed: true
Authentication Modes
- OAuth 2.0 with PKCE (
type: oauth2): Usespkce: true(S256 verification),client_source: byo(user-supplied client credentials stored encrypted at rest), and optionaltoken_rotation: truefor providers like Withings that rotate refresh tokens on every exchange. - Basic / App-Specific Password (
type: basic): Used for standards-based protocols such as CalDAV (caldav.icloud.com,caldav.fastmail.com, or self-hostedlocalhost).
3. Implementing the Connector Protocol
A connector class must satisfy the @runtime_checkable Connector protocol in connectors.base:
class Connector(Protocol):
@property
def manifest(self) -> ConnectorManifest: ...
def streams(self) -> List[StreamSpec]: ...
async def fetch(self, ctx: ToolContext, stream: str, cursor: Optional[str], http: ScopedHttp) -> Page: ...
def map(self, page: Page) -> List[NormalisedRecord]: ...
def tools(self) -> List[ToolSpec]: ...
Sandboxed Network Egress (ScopedHttp)
Connectors never instantiate raw httpx or urllib clients directly. Instead, fetch(...) receives a pre-configured ScopedHttp instance constructed via ScopedHttp.from_data_flow(manifest.data_flows). Any request or redirect targeting a hostname outside data_flows.allowed_hosts (or a cloud metadata/link-local IP) immediately raises EgressBlockedError.
4. Open-Source FHIR R4 Validation (fhir.resources)
Connectors that emit clinical records (WithingsConnector, EpicSmartConnector) validate and normalize all resources using fhir.resources.R4B Pydantic v2 models before emitting NormalisedRecord items:
- Withings (
AU-54): Maps device measure groups into LOINC-coded FHIR R4Observationresources (29463-7Body weight,8302-2Body height,85354-9Blood pressure panel with8480-6Systolic and8462-4Diastolic components,8867-4Heart rate, and2708-6SpO2). - Epic SMART on FHIR (
AU-56): Validates USCDI R4 resources (Condition,MedicationRequest,AllergyIntolerance,Immunization,Observation,Encounter,DocumentReference) against their respectivefhir.resources.R4Bmodels.
5. Provenance & Taint Rules (trusted vs untrusted)
Carefold enforces strict provenance and taint tracking across all connectors:
| Connector Type | trust_level |
Rationale |
|---|---|---|
Structured device telemetry (withings) |
trusted |
Numeric sensor measurements mapped deterministically to LOINC codes. |
Structured calendar availability (google-calendar, caldav) |
trusted |
Busy windows and PHI-stripped appointment slots (sanitize_calendar_event_title). |
External EHR clinical narratives (epic-smart) |
untrusted |
Free-text clinical notes and external narratives carry prompt-injection risk and require From Epic SMART, <YYYY-MM-DD> provenance attribution (E10.7). |
External write tools (such as calendar_create_event) must declare risk_tier="write_external" so the policy engine gates execution behind explicit human approval in A0/A1 autonomy modes.