updateMedicationRequest
Modify the status of an existing medication request using its unique ID. Supports updates to status fields like active, on-hold, cancelled, completed, and more.
Instructions
Updates an existing medication request. Requires the medication request ID and fields to update.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| medicationRequestId | Yes | The unique ID of the medication request to update. | |
| status | No | New status for the medication request. |
Implementation Reference
- The handler function that executes the updateMedicationRequest tool. It fetches the existing MedicationRequest, applies partial updates (treating null as delete), handles special fields like references and notes, and performs the update using Medplum.export async function updateMedicationRequest(medicationRequestId: string, updates: UpdateMedicationRequestArgs): Promise<MedicationRequest> { await ensureAuthenticated(); if (!medicationRequestId) { throw new Error('MedicationRequest ID is required to update a medication request.'); } if (!updates || Object.keys(updates).length === 0) { throw new Error('Updates object cannot be empty for updating a medication request.'); } const existingMedicationRequest = await medplum.readResource('MedicationRequest', medicationRequestId); if (!existingMedicationRequest) { throw new Error(`MedicationRequest with ID ${medicationRequestId} not found.`); } const { note: noteInput, identifier: identifierInput, subjectId: subjectIdInput, encounterId: encounterIdInput, requesterId: requesterIdInput, medicationCodeableConcept: medicationCodeableConceptInput, // medicationReference: medicationReferenceInput, // If added to UpdateMedicationRequestArgs ...restOfUpdates } = updates; const workingUpdates: Partial<MedicationRequest> = {}; // Handle fields from restOfUpdates, converting null to undefined for (const key in restOfUpdates) { if (Object.prototype.hasOwnProperty.call(restOfUpdates, key)) { const value = (restOfUpdates as any)[key]; if (value === null) { (workingUpdates as any)[key] = undefined; } else if (value !== undefined) { (workingUpdates as any)[key] = value; } } } // Handle specific conversions if (typeof noteInput === 'string') { workingUpdates.note = [{ text: noteInput }]; } else if (noteInput === null) { workingUpdates.note = undefined; } else if (noteInput !== undefined) { // If it was already Annotation[] workingUpdates.note = noteInput as any; } if (identifierInput && typeof identifierInput === 'object') { workingUpdates.identifier = [identifierInput as Identifier]; } else if (identifierInput === null) { workingUpdates.identifier = undefined; } if (typeof subjectIdInput === 'string') { workingUpdates.subject = { reference: `Patient/${subjectIdInput}` }; } else if (subjectIdInput === null) { workingUpdates.subject = undefined; } if (typeof encounterIdInput === 'string') { workingUpdates.encounter = { reference: `Encounter/${encounterIdInput}` }; } else if (encounterIdInput === null) { workingUpdates.encounter = undefined; } if (typeof requesterIdInput === 'string') { workingUpdates.requester = { reference: `Practitioner/${requesterIdInput}` }; } else if (requesterIdInput === null) { workingUpdates.requester = undefined; } if (medicationCodeableConceptInput === null) { workingUpdates.medicationCodeableConcept = undefined; } else if (medicationCodeableConceptInput !== undefined) { workingUpdates.medicationCodeableConcept = medicationCodeableConceptInput; // workingUpdates.medicationReference = undefined; // Ensure exclusivity if medicationReference is also handled } // Similar logic for medicationReference if it gets added // if (medicationReferenceInput === null) { // workingUpdates.medicationReference = undefined; // workingUpdates.medicationCodeableConcept = undefined; // Clear the other if one is nulled // } else if (medicationReferenceInput !== undefined) { // workingUpdates.medicationReference = medicationReferenceInput; // workingUpdates.medicationCodeableConcept = undefined; // Ensure exclusivity // } const updatedResource: MedicationRequest = { ...existingMedicationRequest, ...workingUpdates, resourceType: 'MedicationRequest', id: medicationRequestId, }; return medplum.updateResource(updatedResource); }
- TypeScript interface defining the expected input parameters for the updateMedicationRequest handler, including optional fields that can be null to indicate deletion.export interface UpdateMedicationRequestArgs { status?: MedicationRequest['status']; intent?: MedicationRequest['intent']; medicationCodeableConcept?: CodeableConcept | null; // medicationReference?: Reference | null; subjectId?: string | null; encounterId?: string | null; authoredOn?: string | null; requesterId?: string | null; dosageInstruction?: Dosage[] | null; note?: string | null; identifier?: { system?: string; value: string } | null; // Add other relevant fields from CreateMedicationRequestArgs as optional and nullable }
- src/index.ts:608-625 (schema)MCP tool schema definition, providing input validation schema for the updateMedicationRequest tool.{ name: "updateMedicationRequest", description: "Updates an existing medication request. Requires the medication request ID and fields to update.", inputSchema: { type: "object", properties: { medicationRequestId: { type: "string", description: "The unique ID of the medication request to update.", }, status: { type: "string", description: "New status for the medication request.", enum: ["active", "on-hold", "cancelled", "completed", "entered-in-error", "stopped", "draft", "unknown"], }, }, required: ["medicationRequestId"], },
- src/index.ts:972-975 (registration)Registration of the updateMedicationRequest handler function in the toolMapping object used by the MCP server to dispatch tool calls.createMedicationRequest, getMedicationRequestById, updateMedicationRequest, searchMedicationRequests,
- src/index.ts:45-49 (registration)Import statement registering the updateMedicationRequest function from the utils module into the main server file.createMedicationRequest, getMedicationRequestById, updateMedicationRequest, searchMedicationRequests, } from './tools/medicationRequestUtils.js';