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.

60 lines (59 loc) 2.77 kB
import { ErrorHandler, logger, requestContextService, } from "../../../utils/index.js"; import { AnalyzeTrendsInputSchema, analyzeTrendsLogic, AnalyzeTrendsOutputSchema, } from "./logic.js"; /** * Registers the 'clinicaltrials_analyze_trends' tool with the MCP server. * @param server - The MCP server instance. */ export const registerAnalyzeTrendsTool = async (server) => { const toolName = "clinicaltrials_analyze_trends"; const toolDescription = "Performs a statistical analysis on a set of clinical trials, aggregating data by status, country, sponsor, or phase. Use specific query parameters to refine the analysis and filter the studies included in the analysis. The tool can handle up to 5000 studies per analysis."; server.registerTool(toolName, { title: "Analyze Clinical Trial Trends", description: toolDescription, inputSchema: AnalyzeTrendsInputSchema.shape, outputSchema: AnalyzeTrendsOutputSchema.shape, annotations: { readOnlyHint: true }, }, async (params, callContext) => { const handlerContext = requestContextService.createRequestContext({ toolName, parentContext: callContext, }); try { const result = await analyzeTrendsLogic(params, handlerContext); const summaryLines = []; result.analysis.forEach((analysisResult) => { const resultsSummary = Object.entries(analysisResult.results) .map(([key, value]) => ` - ${key}: ${value}`) .join("\n"); summaryLines.push(`Successfully analyzed ${analysisResult.totalStudies} studies for trend '${analysisResult.analysisType}'.\n\n` + `Analysis Results:\n${resultsSummary}`); }); const summaryText = summaryLines.join("\n\n---\n\n"); return { structuredContent: result, content: [{ type: "text", text: summaryText }], }; } catch (error) { logger.error(`Error in ${toolName} handler`, { error, ...handlerContext, }); const mcpError = ErrorHandler.handleError(error, { operation: toolName, context: handlerContext, input: params, }); return { isError: true, content: [{ type: "text", text: mcpError.message }], structuredContent: { code: mcpError.code, message: mcpError.message, details: mcpError.details, }, }; } }); logger.info(`Tool '${toolName}' registered successfully.`); };