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.73 kB
JavaScript
/**
* @fileoverview Handles the registration of the `docwriter_compile_latex_to_pdf` tool
* with an MCP server instance.
* @module src/mcp-server/tools/compileLatexToPdf/registration
*/
import { BaseErrorCode, McpError } from "../../../types-global/errors.js";
import { ErrorHandler, logger, requestContextService, } from "../../../utils/index.js";
import { compileLatexToPdfLogic, CompileLatexToPdfInputSchema, } from "./logic.js";
/**
* Registers the 'docwriter_compile_latex_to_pdf' 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 registerCompileLatexToPdfTool = async (server) => {
const toolName = "docwriter_compile_latex_to_pdf";
const toolDescription = "Compiles a specified .tex document into a PDF. This is the final step in the document creation process, producing the finished artifact. The tool intelligently handles complex documents that require multiple compilation passes (e.g., for table of contents, cross-references) and automatically runs Biber for bibliography processing if needed. It returns the path to the generated PDF and the full compilation log for debugging.";
const registrationContext = requestContextService.createRequestContext({
operation: "RegisterTool",
toolName: toolName,
});
logger.info(`Registering tool: '${toolName}'`, registrationContext);
await ErrorHandler.tryCatch(async () => {
server.tool(toolName, toolDescription, CompileLatexToPdfInputSchema.shape, async (params, mcpContext) => {
const handlerContext = requestContextService.createRequestContext({
parentRequestId: registrationContext.requestId,
operation: "HandleToolRequest",
toolName: toolName,
mcpToolContext: mcpContext,
input: params,
});
try {
const result = await compileLatexToPdfLogic(params, handlerContext);
return {
content: [
{ type: "text", text: JSON.stringify(result, null, 2) },
],
isError: false,
};
}
catch (error) {
const handledError = ErrorHandler.handleError(error, {
operation: "compileLatexToPdfToolHandler",
context: handlerContext,
input: params,
});
const mcpError = handledError instanceof McpError
? handledError
: new McpError(BaseErrorCode.INTERNAL_ERROR, "An unexpected error occurred while compiling the LaTeX document.", { 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,
});
};