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_search_replace` tool
* with an MCP server instance.
* @module src/mcp-server/tools/searchAndReplace/registration
*/
import { BaseErrorCode, McpError } from "../../../types-global/errors.js";
import { ErrorHandler, logger, requestContextService, } from "../../../utils/index.js";
import { searchAndReplaceLogic, SearchAndReplaceInputSchema, } from "./logic.js";
/**
* Registers the 'docwriter_search_replace' 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 registerSearchAndReplaceTool = async (server) => {
const toolName = "docwriter_search_replace";
const toolDescription = "Performs a global search-and-replace operation on a specified LaTeX document. This tool is ideal for simple, non-structural text changes, like correcting a recurring typo or updating a name. It uses a regular expression for searching, and the replacement text is automatically sanitized to prevent security vulnerabilities. Use this for broad changes, but prefer `docwriter_update_document_block` for modifying structured content.";
const registrationContext = requestContextService.createRequestContext({
operation: "RegisterTool",
toolName: toolName,
});
logger.info(`Registering tool: '${toolName}'`, registrationContext);
await ErrorHandler.tryCatch(async () => {
server.tool(toolName, toolDescription, SearchAndReplaceInputSchema.shape, async (params, mcpContext) => {
const handlerContext = requestContextService.createRequestContext({
parentRequestId: registrationContext.requestId,
operation: "HandleToolRequest",
toolName: toolName,
mcpToolContext: mcpContext,
input: params,
});
try {
const result = await searchAndReplaceLogic(params, handlerContext);
return {
content: [
{ type: "text", text: JSON.stringify(result, null, 2) },
],
isError: false,
};
}
catch (error) {
const handledError = ErrorHandler.handleError(error, {
operation: "searchAndReplaceToolHandler",
context: handlerContext,
input: params,
});
const mcpError = handledError instanceof McpError
? handledError
: new McpError(BaseErrorCode.INTERNAL_ERROR, "An unexpected error occurred while performing the search and replace operation.", { 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,
});
};