-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlab-imaging.ts
More file actions
167 lines (162 loc) · 5.76 KB
/
Copy pathlab-imaging.ts
File metadata and controls
167 lines (162 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Lab results, vitals, diagnostic reports & imaging documents.
* Wraps FHIR Observation, DiagnosticReport, DocumentReference.
*/
import { z } from "zod";
import { FHIRError } from "../clients/fhir-client.ts";
import {
bundleNextLink,
bundleToResources,
bundleTotal,
diagnosticReportSummary,
documentReferenceSummary,
observationSummary,
} from "../fhir-utils.ts";
import type { McpServer } from "../mcp/server.ts";
import { checkFhirContext, fhirClientForCurrentContext, resolvePatientId } from "./_helpers.ts";
const clamp = (n: number, lo: number, hi: number) => Math.min(Math.max(n, lo), hi);
export function registerLabImagingTools(server: McpServer): void {
server.tool(
"lab_get_results",
"Return laboratory results (FHIR Observation category=laboratory).",
z.object({
patient_id: z.string().optional(),
code: z.string().optional().describe("LOINC or other code filter, e.g. 2339-0"),
date: z.string().optional().describe("FHIR date filter, e.g. ge2024-01-01"),
count: z.number().int().min(1).max(250).default(50),
abnormal_only: z.boolean().default(false),
}),
async ({ patient_id, code, date, count, abnormal_only }) => {
const err = checkFhirContext({ requirePatient: true, patientId: patient_id });
if (err) return err;
const pid = resolvePatientId(patient_id) ?? "";
try {
const fhir = fhirClientForCurrentContext();
const bundle = await fhir.getObservations(pid, {
category: "laboratory",
code,
date,
count: clamp(count, 1, 250),
});
let labs = bundleToResources(bundle).map(observationSummary);
if (abnormal_only) labs = labs.filter((l) => Boolean(l.abnormal));
return {
labs,
total_count: bundleTotal(bundle),
returned: labs.length,
has_more: bundleNextLink(bundle) !== null,
abnormal_count: labs.reduce((n, l) => n + (l.abnormal ? 1 : 0), 0),
};
} catch (e) {
if (e instanceof FHIRError) return e.toToolResponse();
throw e;
}
},
);
server.tool(
"lab_get_vital_signs",
"Return vital sign observations (Observation category=vital-signs), grouped by test name.",
z.object({
patient_id: z.string().optional(),
date: z.string().optional(),
count: z.number().int().min(1).max(250).default(100),
}),
async ({ patient_id, date, count }) => {
const err = checkFhirContext({ requirePatient: true, patientId: patient_id });
if (err) return err;
const pid = resolvePatientId(patient_id) ?? "";
try {
const fhir = fhirClientForCurrentContext();
const bundle = await fhir.getObservations(pid, {
category: "vital-signs",
date,
count: clamp(count, 1, 250),
});
const vitals = bundleToResources(bundle).map(observationSummary);
const grouped: Record<string, Record<string, unknown>[]> = {};
for (const v of vitals) {
const key = (v.test as string) || "Unknown";
grouped[key] ??= [];
grouped[key].push(v);
}
return {
vitals,
by_type: grouped,
types: Object.keys(grouped),
total_count: bundleTotal(bundle),
returned: vitals.length,
has_more: bundleNextLink(bundle) !== null,
};
} catch (e) {
if (e instanceof FHIRError) return e.toToolResponse();
throw e;
}
},
);
server.tool(
"lab_get_diagnostic_reports",
"Return DiagnosticReport resources for the patient.",
z.object({
patient_id: z.string().optional(),
category: z.string().optional().describe("LAB, RAD, PAT, CT, CG, ..."),
date: z.string().optional(),
count: z.number().int().min(1).max(250).default(25),
}),
async ({ patient_id, category, date, count }) => {
const err = checkFhirContext({ requirePatient: true, patientId: patient_id });
if (err) return err;
const pid = resolvePatientId(patient_id) ?? "";
try {
const fhir = fhirClientForCurrentContext();
const bundle = await fhir.getDiagnosticReports(pid, {
category,
date,
count: clamp(count, 1, 250),
});
const reports = bundleToResources(bundle).map(diagnosticReportSummary);
return {
reports,
total_count: bundleTotal(bundle),
returned: reports.length,
has_more: bundleNextLink(bundle) !== null,
};
} catch (e) {
if (e instanceof FHIRError) return e.toToolResponse();
throw e;
}
},
);
server.tool(
"imaging_get_documents",
"Return DocumentReference resources (clinical notes, imaging, scans).",
z.object({
patient_id: z.string().optional(),
category: z.string().optional(),
type_code: z.string().optional().describe("e.g. 18748-4 for diagnostic imaging"),
count: z.number().int().min(1).max(250).default(25),
}),
async ({ patient_id, category, type_code, count }) => {
const err = checkFhirContext({ requirePatient: true, patientId: patient_id });
if (err) return err;
const pid = resolvePatientId(patient_id) ?? "";
try {
const fhir = fhirClientForCurrentContext();
const bundle = await fhir.getDocumentReferences(pid, {
category,
type: type_code,
count: clamp(count, 1, 250),
});
const documents = bundleToResources(bundle).map(documentReferenceSummary);
return {
documents,
total_count: bundleTotal(bundle),
returned: documents.length,
has_more: bundleNextLink(bundle) !== null,
};
} catch (e) {
if (e instanceof FHIRError) return e.toToolResponse();
throw e;
}
},
);
}