Skip to main content
The clinician asks a patient-specific question. The agent emits a structured request naming the kinds of record it needs, yields, and answers from the records the client fetches back. If the answer depends on the current patient’s electronic health record (EHR), retrieve.
Retrieve flow: the clinician asks a patient question, the agent requests the relevant EHR data, the client fetches the records, and the agent answers grounded in the returned data.Retrieve flow: the clinician asks a patient question, the agent requests the relevant EHR data, the client fetches the records, and the agent answers grounded in the returned data.

A retrieve takes two runs: the agent requests data, the client fetches it, and the agent answers from what comes back.

This page builds on Build a schema tool, which explains the five fields of the connector body, additionalProperties: false, required, const, enum, transition, and how to attach a tool to your agent. What is new here: a requests array, anyOf branches selected by resourceType, per-branch search parameters that are all optional, and the input_required transition that hands the fetch to the client.

Build the retrieve tool

The target: the clinician asks “Does the patient have hypertension?” and the agent emits one request that names every resource type it needs, with any search parameters, then waits for the client to fetch the records.
1

Wrap the requests in an array

One question often needs several kinds of data, such as the problem list, lab values, and medication. So the schema’s only top-level field is a requests array, and the agent fills every request in one call:
With requests in required, minItems: 1 means the agent can never emit an empty request. The agent should also ask for each resource type at most once per call, but uniqueItems compares whole items, so two Condition requests with different search parameters would both pass it. That rule goes in the description instead: “Emit at least one request, at most one per resource type.”
2

Make each request one of several branches with anyOf

Resource types do not all take the same search parameters. Observations narrow by category and date, conditions narrow by category, and flags (alerts on the patient’s record) take no parameters at all. So items is an anyOf: a list of branches, each a complete object schema for a group of resource types that share the same search parameters.
A request is valid if it matches any one branch. Each branch is closed and requires resourceType, so a request always says what it asks for and carries only the search parameters its branch defines. This page groups 13 resource types into 7 branches by their search parameters.
3

Select the branch with the resource type

A branch that covers one resource type pins it with a const. A branch that covers several resource types with the same search parameters lists them in an enum:
No resource type appears in more than one branch, so resourceType acts as a discriminator: a request can match only one branch, and anyOf behaves like oneOf here. Grouping by search parameters keeps the body short, because the resource types in a branch share one definition.
4

Give each branch its own search parameters

Each branch lists its search parameters directly on the request, next to resourceType, and leaves them out of required. Every parameter is optional, and an omitted parameter means no restriction:
A branch whose resource types take no search parameters has only resourceType in its properties, so the agent cannot attach a parameter to a Flag request. Parameters whose accepted values your source system defines, such as type on DocumentReference, stay free-form strings until you know the full set:
A date range is one object: ge means on or after, and le means on or before. The client converts it to the query your EHR expects.
5

Yield with input_required

The agent cannot answer until the client has fetched the records, so the tool ends the run in input_required, not complete:
The framework enforces the transition. The task is not finished: the client’s next message on the same contextId resumes it, as described in Task lifecycle.

The full body

Retrieve needs no registry connector. Attach this one schema tool to your agent.
The right-hand column is the pattern: map each branch to the equivalent read surface in your EHR. An encounter is one visit or hospital stay. status: ["in-progress"] returns the current stay, and no status returns the stay history. Add _include: ["Encounter:diagnosis"] and the stay’s coded diagnoses and procedures come back with it. The Encounter lists each one with its use (such as admission or discharge diagnosis) and rank. Together they are the data a Diagnosis-Related Group (DRG) billing view reads.
* Differs slightly from FHIR R4, where a simpler field is easier for the agent to fill.

Point the system prompt at the tool

The schema tool defines what the agent can request. The system prompt routes each question to it. This is the minimal prompt for retrieve:
Add the rules every flow shares. Retrieve is the flow likeliest to break the rule against requesting data again once it has arrived: when the records come back, the agent must answer from them, not request the same resource types again.
The agent handles a retrieve in two runs: it emits a request, then answers from what comes back. The system prompt and the tool’s description produce this sequence.
  1. Clinician asks: “Does the patient have hypertension?”
  2. Emit request: the agent names one or more resource types and any search parameters, and ends the run in input_required.
  3. Client fetches EHR: the client resolves each request against the EHR’s read API.
  4. Data arrives: the client sends the records back as a new message on the same contextId, carrying a data part. See Using data parts for structured context for the data-part shape. The task is in input_required, not finished, so that message resumes it. See Task lifecycle.
  5. Answer: the agent grounds every claim in the returned data and answers in prose.
For “Does the patient have hypertension?”, the agent emits one request per resource type it needs, in a single call:
MedicationStatement has no search parameters, so the client returns all current medication. The Encounter request asks for the current stay and its coded diagnoses and procedures. Because the transition is input_required, this call ends the run.The apply-step is yours. Map each request to your EHR’s read API, scoped to the current patient, then fetch the records and return them in a new message on the same contextId. If answers come back hedged or empty, check what the client sends back as the data part before adding prompt rules.

Adapt it

  • Keep one retrieve connector for all your resource types. One connector with every branch is easier for the agent to route to and more token-efficient than several.
  • Remove the resource types and search parameters your EHR does not support, using the “What yours would be” column in the branch table.
  • Replace the free-form category and type strings on DocumentReference, and the questionnaire strings, with your own enum once you know your system’s full set. A closed enum makes the schema a complete menu of what the agent may emit.
  • If a new resource type takes the same search parameters as an existing branch, add it to that branch’s resourceType enum. If it takes different parameters, add a new branch to the anyOf.
  • The transition stays input_required, because the agent always waits for the client to fetch the data.