UNPKG

@cyanheads/filesystem-mcp-server

Version:

A Model Context Protocol (MCP) server for platform-agnostic file capabilities, including advanced search and replace, and directory tree traversal

45 lines (44 loc) 2.58 kB
import { BaseErrorCode } from '../../../types-global/errors.js'; import { ErrorHandler } from '../../../utils/internal/errorHandler.js'; import { logger } from '../../../utils/internal/logger.js'; import { requestContextService } from '../../../utils/internal/requestContext.js'; import { WriteFileInputSchema, writeFileLogic, } from './writeFileLogic.js'; /** * Registers the 'write_file' tool with the MCP server. * * @param {McpServer} server - The McpServer instance to register the tool with. * @returns {Promise<void>} A promise that resolves when the tool is registered. * @throws {McpError} Throws an error if registration fails. */ export const registerWriteFileTool = async (server) => { const registrationContext = requestContextService.createRequestContext({ operation: 'RegisterWriteFileTool' }); logger.info("Attempting to register 'write_file' tool", registrationContext); await ErrorHandler.tryCatch(async () => { server.tool('write_file', // Tool name 'Writes content to a specified file. Creates the file (and necessary directories) if it doesn\'t exist, or overwrites it if it does. Accepts relative or absolute paths (resolved like readFile).', // Description WriteFileInputSchema.shape, // Pass the schema shape async (params, extra) => { const typedParams = params; const callContext = requestContextService.createRequestContext({ operation: 'WriteFileToolExecution', parentId: registrationContext.requestId }); logger.info(`Executing 'write_file' tool for path: ${typedParams.path}`, callContext); // ErrorHandler will catch McpErrors thrown by the logic const result = await ErrorHandler.tryCatch(() => writeFileLogic(typedParams, callContext), { operation: 'writeFileLogic', context: callContext, input: { path: typedParams.path, content: '[CONTENT REDACTED]' }, // Redact content for logging errorCode: BaseErrorCode.INTERNAL_ERROR }); logger.info(`Successfully executed 'write_file' for path: ${result.writtenPath}`, callContext); // Format the successful response return { content: [{ type: 'text', text: result.message }], }; }); logger.info("'write_file' tool registered successfully", registrationContext); }, { operation: 'registerWriteFileTool', context: registrationContext, errorCode: BaseErrorCode.CONFIGURATION_ERROR, critical: true }); };