Skip to main content
Glama
rkirkendall

Medplum MCP Server

by rkirkendall

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
NameRequiredDescriptionDefault
medicationRequestIdYesThe unique ID of the medication request to update.
statusNoNew 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
    }
  • 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';
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden for behavioral disclosure. It states this is an update operation (implying mutation) and mentions the required ID, but doesn't disclose critical behavioral traits like permission requirements, whether changes are reversible, side effects on related resources, rate limits, or what the response contains. For a mutation tool with zero annotation coverage, this is insufficient.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that gets straight to the point without unnecessary words. It's appropriately sized for a simple update operation, though it could potentially be more front-loaded with key behavioral information given the lack of annotations.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a mutation tool with no annotations and no output schema, the description is incomplete. It doesn't explain what happens during the update, what values can be changed beyond status, error conditions, or what the tool returns. Given the complexity of healthcare data updates and the lack of structured behavioral information, more context is needed.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, with both parameters well-documented in the schema. The description mentions 'medication request ID and fields to update' which aligns with the schema but adds no additional semantic context beyond what's already in the structured fields. The baseline of 3 is appropriate when the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Updates') and resource ('an existing medication request'), making the purpose evident. It distinguishes from sibling 'createMedicationRequest' by specifying 'existing', but doesn't explicitly differentiate from other update tools like 'updateCondition' or 'updatePatient' beyond the resource type.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites beyond requiring the ID, nor does it specify scenarios where this tool is appropriate compared to other update tools or create/delete operations. No explicit when/when-not statements are present.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/rkirkendall/medplum-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server