docwriter-mcp-server
Version:
A Model Context Protocol (MCP) server for programmatic creation, modification, and compilation of structured LaTeX documents. Enables AI agents and automated workflows to generate reports, articles, and papers from templates, with secure, structured conte
75 lines (74 loc) • 3.62 kB
JavaScript
/**
* @fileoverview Handles the registration of the `docwriter_list_latex_documents` tool
* with an MCP server instance.
* @module src/mcp-server/tools/listLatexDocuments/registration
*/
import { BaseErrorCode, McpError } from "../../../types-global/errors.js";
import { ErrorHandler, logger, requestContextService, } from "../../../utils/index.js";
import { listLatexDocumentsLogic, ListLatexDocumentsInputSchema, } from "./logic.js";
/**
* Registers the 'docwriter_list_latex_documents' tool and its handler with the MCP server.
*
* @param {McpServer} server - The MCP server instance to register the tool with.
* @returns {Promise<void>} A promise that resolves when tool registration is complete.
*/
export const registerListLatexDocumentsTool = async (server) => {
const toolName = "docwriter_list_latex_documents";
const toolDescription = "Retrieves a list of all available LaTeX documents by scanning the server's data directory. This tool is useful for discovering existing `documentId`s that can be used with other tools like `docwriter_update_document_block` or `docwriter_compile_latex_to_pdf`. It returns a simple list of document identifiers.";
const registrationContext = requestContextService.createRequestContext({
operation: "RegisterTool",
toolName: toolName,
});
logger.info(`Registering tool: '${toolName}'`, registrationContext);
await ErrorHandler.tryCatch(async () => {
server.tool(toolName, toolDescription, ListLatexDocumentsInputSchema.shape, async (params, mcpContext) => {
const handlerContext = requestContextService.createRequestContext({
parentRequestId: registrationContext.requestId,
operation: "HandleToolRequest",
toolName: toolName,
mcpToolContext: mcpContext,
input: params,
});
try {
const result = await listLatexDocumentsLogic(params, handlerContext);
return {
content: [
{ type: "text", text: JSON.stringify(result, null, 2) },
],
isError: false,
};
}
catch (error) {
const handledError = ErrorHandler.handleError(error, {
operation: "listLatexDocumentsToolHandler",
context: handlerContext,
input: params,
});
const mcpError = handledError instanceof McpError
? handledError
: new McpError(BaseErrorCode.INTERNAL_ERROR, "An unexpected error occurred while listing the LaTeX documents.", { originalErrorName: handledError.name });
return {
content: [
{
type: "text",
text: JSON.stringify({
error: {
code: mcpError.code,
message: mcpError.message,
details: mcpError.details,
},
}),
},
],
isError: true,
};
}
});
logger.info(`Tool '${toolName}' registered successfully.`, registrationContext);
}, {
operation: `RegisteringTool_${toolName}`,
context: registrationContext,
errorCode: BaseErrorCode.INITIALIZATION_FAILED,
critical: true,
});
};