Skip to content

Agent protocol

An Epic.direct Agent is a worker that runs connector drivers outside the platform: for a system that needs a long-lived session, or one that can only be reached from inside a network the hospital has approved. The agent calls the platform; the platform never calls the agent.

Where this stands#

The protocol on this page is implemented and tested on the platform, and the worker in the repository speaks it. What an agent can do with it today is limited by the catalogue:

Hosting#

Hosted by Epic.directHosted by the workspace
Registered byA platform administrator, in the admin consoleA member with connections.manage, on the workspace’s Agents page
RunsOn Epic.direct's infrastructureOn a host inside a network the hospital has approved
Suited toA system reachable from the internet, where the hospital has authorised that accessA system reachable only from the hospital's network, or where access must come from a known machine

Either way the agent makes outbound HTTPS requests only and listens on no port. An agent sees work only for the connections assigned to it, and a workspace’s administrator makes that assignment on the connection.

Address and authentication#

Every operation is a POST with a JSON body to the platform’s HTTP address:

Base address
https://tame-oyster-517.convex.site/agent/v1/
  • Authenticate with the agent’s token: Authorization: Bearer eda_…. It is shown once at registration and stored only as a SHA-256 hash.
  • Revoking an agent unassigns its connections and takes effect on its next request.
  • A request body may be up to 4,000,000 characters.
StatusCodeWhen
401invalid_agent_tokenThe token is missing, unknown or revoked.
400invalid_jsonThe body is not JSON.
400invalid_requestA required field is missing.
404not_foundNo such operation, or no such connection for this agent.
413too_largeThe body is over the size limit.

Operations#

OperationPurpose
helloAnnounce the agent and learn which connections are assigned to it.
claimTake the next queued job, if there is one.
heartbeatKeep the lease on a running job.
finishReport the outcome of a job.
mfa/requestAsk a person for a sign-in code or approval.
mfa/collectCollect that person's answer.

hello

Call it at start-up. It marks the agent as seen, and records the three optional fields so they can be shown on the Agents page.

POST /agent/v1/hello
{ "version": "0.1.0", "hostname": "reading-room-02", "drivers": ["selftest"] }
200
{
  "data": {
    "agent": { "id": "…", "name": "Main campus on-site agent" },
    "connections": [
      { "id": "…", "typeId": "epic-hyperspace-citrix", "name": "…", "status": "draft", "capabilities": ["connection.test"] }
    ]
  }
}

claim

Send an empty body. The answer is either { "data": { "job": null } } or one job. Claiming is a single transaction, so two workers sharing a token are never given the same job.

200, with a job
{
  "data": {
    "job": {
      "id": "n17q3w5e7r9t1y3u5i7o9p2a4s",
      "attempt": 1,
      "lease_until": 1795787584000,
      "kind": "protocols.submit",
      "connection": {
        "id": "kd72m4n6b8v0c1x3z5a7s9d2f4",
        "typeId": "epic-hyperspace-citrix",
        "config": { "portalUrl": "https://…" },
        "capabilities": ["connection.test", "requisitions.list", "protocols.submit"]
      },
      "secrets": { "username": "…", "password": "…" },
      "submission": {
        "externalId": "…",
        "protocolCode": "CT-AP-IV",
        "notes": "…",
        "approvedBy": "…"
      }
    }
  }
}
FieldMeaning
idThe job. Send it back as job_id.
attemptWhich attempt this is. Send it back unchanged: it is how the platform knows a report belongs to the current claim.
lease_untilWhen the lease runs out, in Unix milliseconds, unless a heartbeat extends it.
kindThe job kind.
connectionThe connection's id, connector type, settings and capabilities.
secretsThe connection's credentials, unsealed for this job.
submissionFor protocols.submit only: the order’s id in the source system, the approved protocol code, the radiologist’s notes and who approved it, with locator (the row to find first) and, where the hospital takes a form, form (the buttons to press, in order). See Protocol forms. Otherwise null.

heartbeat

Call it while a job runs, well inside the lease. Each call extends the lease by the job kind’s lease time.

POST /agent/v1/heartbeat
{ "job_id": "…", "attempt": 1 }
200
{ "data": { "held": true } }

held: false means the claim is no longer yours: the lease ran out and the platform has already retried or failed the job. Stop the work and report nothing.

A heartbeat renews the job, not the agent’s presence. Only hello and claim mark the agent as seen, and an agent not seen for three minutes is shown as offline. A worker on a long job should call hello about once a minute.

finish

Report success with the driver’s output, or failure with a kind.

Success
{ "job_id": "…", "attempt": 1, "ok": true, "output": { "message": "Signed in" } }
Failure
{
  "job_id": "…", "attempt": 1, "ok": false,
  "error": "The system did not answer",
  "error_kind": "unreachable",
  "retryable": true
}
200
{ "data": { "settled": true } }
  • error_kind is one of auth, unreachable, rejected, unsupported, invalid, internal. Anything else is recorded as internal.
  • error is kept to 500 characters, stored with the job and shown to operators. It must not contain patient details or a credential.
  • retryable is taken as true only when it is exactly true.
  • settled: false means the report was not accepted, because the claim had already passed to someone else.

What the platform reads from a successful output:

Job kindOutputEffect
connection.testmessage, up to 300 charactersRecorded as the result of the test.
protocols.options.syncoptions: protocol optionsReplaces what is known of the hospital's protocol list from this source.
requisitions.syncrequisitions, and optionally optionsRequisitions are created, updated or closed to match the source.
protocols.submitconfirmed: true, and optionally referenceThe requisition is marked submitted. Without confirmed: true the job is settled as rejected and not retried.
studies.syncstudies: an array of study recordsStudies are added to the reading worklist or updated in place, as they are for a connector the platform runs.
  • Records must have the shapes in @epicdirect/core: RequisitionRecord and ProtocolOptionRecord. Output that does not match is settled as invalid, is not retried, and is not echoed back.
  • confirmed: true may only be sent after the protocol was written and read back as saved. This is the one rule the whole submission path rests on.

mfa/request and mfa/collect

Some sign-ins need a person: a code sent by text, a code read from an authenticator app, or an approval in one. A driver asks for it through the platform and waits.

POST /agent/v1/mfa/request
{ "connection_id": "…", "kind": "sms_code" }
200
{ "data": { "challenge_id": "…" } }
  • kind is sms_code (the default), authenticator_code or authenticator_number. With authenticator_code a person types the 6-digit code their authenticator app shows, and collect returns it like a texted code. With authenticator_number nothing is typed: prompt can carry the number to choose, up to 20 characters.
  • The request appears in the workspace, and the workspace’s notification numbers are sent a text if it has any.
  • A member with credentials.manage answers in the workspace, or a code is sent back by text reply. Either way the answer is written to the audit trail, without the code.
  • A new request replaces any earlier one still waiting for the same connection.
POST /agent/v1/mfa/collect
{ "challenge_id": "…" }
200
{ "data": { "status": "answered", "code": "…" } }

status is waiting, answered or expired. A request expires after five minutes. The code is handed over once and then erased, so a second collect returns answered without it.

The worker loop#

  1. hello. If the platform cannot be reached, wait and try again, doubling the wait to a ceiling.
  2. claim. On null, wait a few seconds and claim again.
  3. Run the job with the driver for connection.typeId. With no driver for it, finish as unsupported, not retryable.
  4. heartbeat on an interval while it runs. On held: false, abandon the job.
  5. finish, then clear the credentials from memory.
  6. On a stop signal, stop claiming. Give a running job a short time to complete, then finish it as a retryable failure so the queue does not wait out the lease.
  7. On 401, exit. The token was revoked and retrying cannot help.

The worker in the repository#

services/agent implements that loop on Node 22 with no dependencies. It reads EPICDIRECT_URL and EPICDIRECT_AGENT_TOKEN from the environment, writes structured logs that cannot hold a credential or patient data, and ships a container definition.

Shell
# Check the installation. Needs no network, no token and no clinical system.
node src/index.ts selftest

# Run the worker.
export EPICDIRECT_URL="https://tame-oyster-517.convex.site"
export EPICDIRECT_AGENT_TOKEN="eda_…"
node src/index.ts

With both variables set, selftest also calls hello, which proves the address, the token and the route out of the network. It never claims a job. To add a driver, see Adding a connector.