@cyanheads/pubmed-mcp-server
Version:
Production-ready PubMed Model Context Protocol (MCP) server that empowers AI agents and research tools with comprehensive access to PubMed's article database. Enables advanced, automated LLM workflows for searching, retrieving, analyzing, and visualizing
70 lines • 3.51 kB
JavaScript
/**
* @fileoverview Registration for the pubmed_fetch_contents MCP tool.
* @module src/mcp-server/tools/pubmedFetchContents/registration
*/
import { BaseErrorCode, McpError } from "../../../types-global/errors.js";
import { ErrorHandler, logger, requestContextService, } from "../../../utils/index.js";
import { PubMedFetchContentsInputSchema, pubMedFetchContentsLogic, } from "./logic.js";
/**
* Registers the pubmed_fetch_contents tool with the MCP server.
* @param server - The McpServer instance.
*/
export async function registerPubMedFetchContentsTool(server) {
const operation = "registerPubMedFetchContentsTool";
const toolName = "pubmed_fetch_contents";
const toolDescription = "Fetches detailed information from PubMed using NCBI EFetch. Can be used with a direct list of PMIDs or with queryKey/webEnv from an ESearch history entry. Supports pagination (retstart, retmax) when using history. Available 'detailLevel' options: 'abstract_plus' (parsed details), 'full_xml' (raw PubMedArticle XML), 'medline_text' (MEDLINE format), or 'citation_data' (minimal citation data). Returns a JSON object containing results, any PMIDs not found (if applicable), and EFetch details.";
const context = requestContextService.createRequestContext({ operation });
await ErrorHandler.tryCatch(async () => {
server.tool(toolName, toolDescription, PubMedFetchContentsInputSchema._def.schema.shape, async (input, toolContext) => {
const richContext = requestContextService.createRequestContext({
parentRequestId: context.requestId,
operation: "pubMedFetchContentsToolHandler",
mcpToolContext: toolContext,
input,
});
try {
const result = await pubMedFetchContentsLogic(input, richContext);
return {
content: [{ type: "text", text: result.content }],
isError: false,
};
}
catch (error) {
const handledError = ErrorHandler.handleError(error, {
operation: "pubMedFetchContentsToolHandler",
context: richContext,
input,
rethrow: false,
});
const mcpError = handledError instanceof McpError
? handledError
: new McpError(BaseErrorCode.INTERNAL_ERROR, "An unexpected error occurred while fetching PubMed content.", {
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,
});
}
//# sourceMappingURL=registration.js.map