mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
97 lines
3.8 KiB
TypeScript
97 lines
3.8 KiB
TypeScript
import { z } from "zod/v4";
|
|
|
|
import { resolveMarkerForRead } from "@/lib/labs/biomarker-resolve";
|
|
import { getLabHistory, LAB_HISTORY_MAX_LIMIT } from "@/lib/mcp/rich-reads";
|
|
import { decodeOffsetCursor } from "@/lib/mcp/pagination";
|
|
import type { McpAuthContext } from "../auth";
|
|
import { runCoachTool, whenModuleOn } from "./shared";
|
|
import { READ_ONLY_ANNOTATIONS, type McpToolDefinition } from "./types";
|
|
|
|
/** Output schema for `get_labs` — latest-per-biomarker OR a paginated history. */
|
|
const getLabsOutput: z.ZodRawShape = {
|
|
present: z.boolean(),
|
|
reason: z.string().optional(),
|
|
// Latest-mode payload (the Coach labs section, carried opaque).
|
|
data: z.unknown().optional(),
|
|
grounding: z.string().optional(),
|
|
// History-mode payload (per-analyte trajectory + pagination).
|
|
analyte: z.string().optional(),
|
|
readings: z
|
|
.array(
|
|
z.object({
|
|
value: z.number().nullable(),
|
|
valueText: z.string().nullable(),
|
|
unit: z.string(),
|
|
referenceLow: z.number().nullable(),
|
|
referenceHigh: z.number().nullable(),
|
|
rangeStatus: z.enum(["in-range", "below", "above", "unknown"]),
|
|
takenAt: z.string(),
|
|
}),
|
|
)
|
|
.optional(),
|
|
nextCursor: z.string().optional(),
|
|
};
|
|
|
|
/** `get_labs` behind its module gate: latest-per-biomarker or a history page. */
|
|
function readLabs(
|
|
ctx: McpAuthContext,
|
|
args: Record<string, unknown>,
|
|
): Promise<unknown> {
|
|
// History mode: one analyte's paginated trajectory over the labs read
|
|
// path. Requires an analyte — there is no whole-record history dump.
|
|
if (args.history === true) {
|
|
const analyte = typeof args.analyte === "string" ? args.analyte : "";
|
|
if (!analyte.trim()) {
|
|
return Promise.resolve({
|
|
present: false,
|
|
reason: "analyte_required_for_history",
|
|
});
|
|
}
|
|
return getLabHistory(ctx.userId, {
|
|
analyte,
|
|
offset: decodeOffsetCursor(args.cursor),
|
|
limit: LAB_HISTORY_MAX_LIMIT,
|
|
});
|
|
}
|
|
// Default: latest reading per biomarker via the Coach labs read. A name that
|
|
// resolves to one of the user's markers (an alias or a catalog abbreviation
|
|
// such as "GPT" for "ALT") is read as that marker's own name (v1.43).
|
|
return (async () => {
|
|
const asked = typeof args.analyte === "string" ? args.analyte : null;
|
|
const marker = asked?.trim()
|
|
? await resolveMarkerForRead(ctx.userId, asked)
|
|
: null;
|
|
return runCoachTool(ctx, "get_labs", {
|
|
...(asked !== null ? { analyte: marker?.name ?? asked } : {}),
|
|
});
|
|
})();
|
|
}
|
|
|
|
export const getLabsTool: McpToolDefinition = {
|
|
name: "get_labs",
|
|
title: "Get lab results",
|
|
description:
|
|
"Fetch the user's lab results. By default returns the latest reading per biomarker over the whole record. Filtered to one named analyte, each matching marker also carries a summarised history (the newest earlier values in full, older ones as one mean / min / max / count per year). Pass history:true with an analyte to return that analyte's reading TRAJECTORY (newest first, paginated). Each reading carries its value, unit, reference range, and in-range/below/above status. Returns { present: false } when no labs match, and { present: false, reason: \"module_disabled\" } when the labs module is switched off.",
|
|
inputShape: {
|
|
analyte: z.string().min(1).max(80).optional(),
|
|
history: z
|
|
.boolean()
|
|
.optional()
|
|
.describe(
|
|
"When true (requires `analyte`), return that analyte's full reading trajectory (newest first, paginated) instead of the latest-per-biomarker snapshot.",
|
|
),
|
|
cursor: z
|
|
.string()
|
|
.max(256)
|
|
.optional()
|
|
.describe(
|
|
"Opaque pagination cursor from a previous history response's nextCursor. Treat as a black box.",
|
|
),
|
|
},
|
|
annotations: READ_ONLY_ANNOTATIONS,
|
|
outputShape: getLabsOutput,
|
|
run(ctx, args) {
|
|
return whenModuleOn(ctx, "labs", "get_labs", () => readLabs(ctx, args));
|
|
},
|
|
};
|