UNPKG

clinicaltrialsgov-mcp-server

Version:

ClinicalTrials.gov Model Context Protocol (MCP) Server that provides a suite of tools for interacting with the official ClinicalTrials.gov v2 API. Enables AI agents and LLMs to programmatically search, retrieve, and analyze clinical trial data.

71 lines (70 loc) 2.77 kB
/** * @fileoverview Provides functions for cleaning and simplifying JSON responses * from the ClinicalTrials.gov API. * @module src/utils/clinicaltrials/jsonCleaner */ function cleanBrowseModules(study) { const cleanedStudy = JSON.parse(JSON.stringify(study)); const cleanModule = (module) => { if (module && module.browseLeaves && module.ancestors) { const leafTerms = new Set(); module.browseLeaves.forEach((leaf) => { if (leaf.term) leafTerms.add(leaf.term); if (leaf.name) leafTerms.add(leaf.name); }); module.ancestors = module.ancestors.filter((ancestor) => ancestor.term && !leafTerms.has(ancestor.term)); } }; cleanModule(cleanedStudy.derivedSection?.conditionBrowseModule); cleanModule(cleanedStudy.derivedSection?.interventionBrowseModule); return cleanedStudy; } /** * Filters browse leaves to only include those with high relevance. * @param study - The study object to clean. * @returns A new study object with filtered browse leaves. */ function filterBrowseLeavesByRelevance(study) { const cleanedStudy = JSON.parse(JSON.stringify(study)); const filterModule = (module) => { if (module && module.browseLeaves) { module.browseLeaves = module.browseLeaves.filter((leaf) => leaf.relevance !== "LOW"); } }; filterModule(cleanedStudy.derivedSection?.conditionBrowseModule); filterModule(cleanedStudy.derivedSection?.interventionBrowseModule); return cleanedStudy; } /** * Ensures that the `armNames` property in each intervention is an array. * This handles cases where the API might return a null or undefined value. * @param study - The study object to clean. * @returns A new study object with `armNames` guaranteed to be an array. */ function ensureArmNamesArray(study) { const cleanedStudy = JSON.parse(JSON.stringify(study)); const interventions = cleanedStudy.protocolSection?.armsInterventionsModule?.interventions; if (interventions) { interventions.forEach((intervention) => { if (!intervention.armNames) { intervention.armNames = []; } }); } return cleanedStudy; } /** * Cleans a single study object by applying all available cleaning functions. * @param study - The study object to clean. * @returns A cleaned study object. */ export function cleanStudy(study) { let cleanedStudy = study; cleanedStudy = cleanBrowseModules(cleanedStudy); cleanedStudy = filterBrowseLeavesByRelevance(cleanedStudy); cleanedStudy = ensureArmNamesArray(cleanedStudy); // Add other cleaning functions here as needed return cleanedStudy; }