createMedication
Generate medication resources in Medplum FHIR servers by specifying medication codes, names, and forms for accurate healthcare data management.
Instructions
Creates a new medication resource. Requires medication code or identifier.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | The code identifying the medication (e.g., RxNorm, SNOMED CT). | |
| display | No | The display name of the medication. | |
| form | No | The form of the medication (e.g., tablet, capsule, liquid). |
Implementation Reference
- src/tools/medicationUtils.ts:34-82 (handler)The primary handler function that executes the 'createMedication' tool logic. It takes CreateMedicationArgs, validates the code, builds a FHIR Medication resource, creates it using the Medplum client, and returns the created resource or an OperationOutcome on error.export async function createMedication( args: CreateMedicationArgs, client?: MedplumClient, // Restore optional client ): Promise<Medication | OperationOutcome> { const medplumClient = client || medplum; await ensureAuthenticated(); try { if (!args.code || !args.code.coding || args.code.coding.length === 0) { throw new Error('Medication code with at least one coding is required.'); } const medicationResource: Medication = { resourceType: 'Medication', code: args.code, status: args.status, manufacturer: args.manufacturer, form: args.form, identifier: args.identifier, }; Object.keys(medicationResource).forEach( (key) => (medicationResource as any)[key] === undefined && delete (medicationResource as any)[key], ); const createdMedication = (await medplumClient.createResource( medicationResource, )) as Medication; console.log('Medication created successfully:', createdMedication); return createdMedication; } catch (error: any) { console.error('Error creating Medication:', error); const outcome: OperationOutcome = { resourceType: 'OperationOutcome', issue: [ { severity: 'error', code: 'exception', diagnostics: `Error creating Medication: ${error.message || 'Unknown error'}`, }, ], }; if (error.outcome) { console.error('Server OperationOutcome:', JSON.stringify(error.outcome, null, 2)); return error.outcome as OperationOutcome; } return outcome; } }
- src/tools/medicationUtils.ts:15-21 (schema)TypeScript interface defining the input arguments for the createMedication handler, including required code and optional fields like status, manufacturer, form, and identifiers.export interface CreateMedicationArgs { code: CodeableConcept; status?: MedicationStatus; manufacturer?: Reference<Organization>; // Corrected to Reference<Organization> form?: CodeableConcept; identifier?: Identifier[]; }
- src/tools/toolSchemas.ts:458-516 (schema)Detailed JSON schema definition for the 'createMedication' tool input, providing structured properties for code, status, manufacturer, form, and identifiers to guide LLM usage.{ name: 'createMedication', description: 'Creates a new Medication resource. Requires at least a code system and code value.', input_schema: { type: 'object', properties: { code_coding_system: { type: 'string', description: "The system for the medication code (e.g., 'http://www.nlm.nih.gov/research/umls/rxnorm').", }, code_coding_code: { type: 'string', description: 'The value for the medication code (e.g., \'313782\').', }, code_coding_display: { type: 'string', description: 'Optional display text for the medication code (e.g., \'Amoxicillin 250mg Oral Tablet\').', }, code_text: { type: 'string', description: 'Optional fallback text for the overall medication code concept.', }, status: { type: 'string', description: "The status of the medication. Optional.", enum: ['active', 'inactive', 'entered-in-error'], }, manufacturer_reference: { type: 'string', description: 'Optional reference to the manufacturer Organization (e.g., \"Organization/123\").', }, form_coding_system: { type: 'string', description: "Optional: The system for the medication form code (e.g., 'http://snomed.info/sct').", }, form_coding_code: { type: 'string', description: "Optional: The value for the medication form code (e.g., \'385055001\').", }, form_coding_display: { type: 'string', description: "Optional: Display text for the medication form code (e.g., \'Oral tablet\').", }, form_text: { type: 'string', description: 'Optional: Fallback text for the overall medication form concept.', }, identifier_system: { type: 'string', description: "Optional: System for a medication identifier (e.g., 'http://hl7.org/fhir/sid/ndc').", }, identifier_value: { type: 'string', description: 'Optional: Value for the medication identifier.', }, }, required: ['code_coding_system', 'code_coding_code'], }, },
- src/index.ts:976-988 (registration)Tool registration in the MCP server: imported at line 51, defined in mcpTools array (lines 655-677 with inputSchema), and mapped in toolMapping object for execution handling.createMedication, getMedicationById, searchMedications, createEpisodeOfCare, getEpisodeOfCareById, updateEpisodeOfCare, searchEpisodesOfCare, createCondition, getConditionById, updateCondition, searchConditions, generalFhirSearch, };
- src/index.ts:655-677 (registration)MCP tool definition in mcpTools array, including name, description, and simplified inputSchema used by the MCP server for tool listing and validation.{ name: "createMedication", description: "Creates a new medication resource. Requires medication code or identifier.", inputSchema: { type: "object", properties: { code: { type: "string", description: "The code identifying the medication (e.g., RxNorm, SNOMED CT).", }, display: { type: "string", description: "The display name of the medication.", }, form: { type: "string", description: "The form of the medication (e.g., tablet, capsule, liquid).", }, }, required: ["code"], }, }, {