@cyanheads/pubmed-mcp-server
Version:
A Model Context Protocol (MCP) server enabling AI agents to intelligently search, retrieve, and analyze biomedical literature from PubMed via NCBI E-utilities. Built on the mcp-ts-template for robust, production-ready performance.
72 lines (71 loc) • 3.29 kB
JavaScript
import { BaseErrorCode, McpError } from "../../../types-global/errors.js";
import { ErrorHandler, logger, requestContextService, } from "../../../utils/index.js";
import { GeneratePubMedChartInputSchema, generatePubMedChartLogic, } from "./logic.js";
export async function registerGeneratePubMedChartTool(server) {
const operation = "registerGeneratePubMedChartTool";
const toolName = "generate_pubmed_chart";
const toolDescription = "Generates a customizable chart (PNG) from structured data. " +
"Supports 'bar', 'line', 'scatter', 'pie', 'doughnut', 'bubble', 'radar', and 'polarArea' plots. " +
"Requires data values and field mappings for axes. " +
"Optional parameters allow for titles and dimensions. " +
"Internally uses Chart.js and chartjs-node-canvas to produce a Base64-encoded PNG image.";
const context = requestContextService.createRequestContext({ operation });
await ErrorHandler.tryCatch(async () => {
server.tool(toolName, toolDescription, GeneratePubMedChartInputSchema.shape, async (input, mcpProvidedContext) => {
const richContext = requestContextService.createRequestContext({
parentRequestId: context.requestId,
operation: "generatePubMedChartToolHandler",
mcpToolContext: mcpProvidedContext,
input,
});
try {
const result = await generatePubMedChartLogic(input, richContext);
return {
content: [
{
type: "image",
data: result.base64Data,
mimeType: "image/png",
},
],
isError: false,
};
}
catch (error) {
const handledError = ErrorHandler.handleError(error, {
operation: "generatePubMedChartToolHandler",
context: richContext,
input,
rethrow: false,
});
const mcpError = handledError instanceof McpError
? handledError
: new McpError(BaseErrorCode.INTERNAL_ERROR, "An unexpected error occurred while generating the chart.", {
originalErrorName: handledError.name,
originalErrorMessage: handledError.message,
});
return {
content: [
{
type: "text",
text: JSON.stringify({
error: {
code: mcpError.code,
message: mcpError.message,
details: mcpError.details,
},
}),
},
],
isError: true,
};
}
});
logger.notice(`Tool '${toolName}' registered.`, context);
}, {
operation,
context,
errorCode: BaseErrorCode.INITIALIZATION_FAILED,
critical: true,
});
}