@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.
73 lines (72 loc) • 3.82 kB
JavaScript
/**
* @fileoverview Registration for the pubmed_research_agent tool.
* @module pubmedResearchAgent/registration
*/
import { BaseErrorCode, McpError } from "../../../types-global/errors.js";
import { ErrorHandler, logger, requestContextService, } from "../../../utils/index.js";
import { pubmedResearchAgentLogic } from "./logic.js";
import { PubMedResearchAgentInputSchema, } from "./logic/index.js";
/**
* Registers the pubmed_research_agent tool with the MCP server.
* @param server - The McpServer instance.
*/
export async function registerPubMedResearchAgentTool(server) {
const operation = "registerPubMedResearchAgentTool";
const toolName = "pubmed_research_agent";
const toolDescription = "Generates a standardized JSON research plan outline from component details you provide. It accepts granular inputs for all research phases (conception, data collection, analysis, dissemination, cross-cutting concerns). If `include_detailed_prompts_for_agent` is true, the output plan will embed instructive prompts and detailed guidance notes to aid the research agent. The tool's primary function is to organize and structure your rough ideas into a formal, machine-readable plan. This plan is intended for further processing; as the research agent, you should then utilize your full suite of tools (e.g., file manipulation, `get_pubmed_article_connections` for literature/data search via PMID) to execute the outlined research, tailored to the user's request.";
const context = requestContextService.createRequestContext({ operation });
await ErrorHandler.tryCatch(async () => {
server.tool(toolName, toolDescription, PubMedResearchAgentInputSchema.shape, async (input, mcpProvidedContext) => {
const richContext = requestContextService.createRequestContext({
parentRequestId: context.requestId,
operation: "pubmedResearchAgentToolHandler",
mcpToolContext: mcpProvidedContext,
input,
});
try {
const result = await pubmedResearchAgentLogic(input, richContext);
return {
content: [
{ type: "text", text: JSON.stringify(result, null, 2) },
],
isError: false,
};
}
catch (error) {
const handledError = ErrorHandler.handleError(error, {
operation: "pubmedResearchAgentToolHandler",
context: richContext,
input,
rethrow: false,
});
const mcpError = handledError instanceof McpError
? handledError
: new McpError(BaseErrorCode.INTERNAL_ERROR, "An unexpected error occurred while generating the research plan.", {
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,
});
}