file_writer
Write content to files with automatic directory creation and append options for managing generated API code and documentation.
Instructions
Write content to the specified file path, with support for automatic directory creation
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Complete path of the file | |
| content | Yes | Content to be written to the file | |
| createDirs | No | Whether to automatically create parent directories if they do not exist | |
| append | No | Whether to append to an existing file instead of overwriting it | |
| encoding | No | File encoding | utf8 |
Implementation Reference
- src/tools/file-writer-tool.ts:62-99 (handler)Core handler function that performs file writing: validates params, creates directories if needed, appends or overwrites content, retrieves stats, handles errors.async writeFile(params: FileWriterParams): Promise<object> { try { const { filePath, content, createDirs, append, encoding } = params; // 确保父目录存在 if (createDirs) { const dir = path.dirname(filePath); await fs.mkdir(dir, { recursive: true }); } // 写入文件 if (append) { await fs.appendFile(filePath, content, { encoding: encoding as BufferEncoding }); } else { await fs.writeFile(filePath, content, { encoding: encoding as BufferEncoding }); } // 获取文件信息 const stats = await fs.stat(filePath); return { success: true, filePath, size: stats.size, created: stats.birthtime, modified: stats.mtime, message: `文件已${append ? '追加' : '写入'}: ${filePath}`, }; } catch (error) { console.error('文件写入失败:', error); return { success: false, error: error instanceof Error ? error.message : String(error), filePath: params.filePath, }; } }
- src/tools/file-writer-tool.ts:11-17 (schema)Zod schema defining input parameters for the file_writer tool: filePath, content, createDirs, append, encoding.const fileWriterSchema = z.object({ filePath: z.string().min(1).describe('Complete path of the file'), content: z.string().describe('Content to be written to the file'), createDirs: z.boolean().optional().default(true).describe('Whether to automatically create parent directories if they do not exist'), append: z.boolean().optional().default(false).describe('Whether to append to an existing file instead of overwriting it'), encoding: z.string().optional().default('utf8').describe('File encoding'), });
- src/tools/file-writer-tool.ts:36-57 (registration)Class method `register` that registers the tool on the MCP server using server.tool with name, description, schema, and a wrapper async handler calling writeFile.register(server: McpServer): void { server.tool( this.name, this.description, this.schema.shape, async (params) => { // 验证参数 const result = await this.writeFile(params); // 返回符合MCP要求的格式 return { ...result, content: [ { type: "text", text: JSON.stringify(result, null, 2) } ] }; } ); }
- src/index.ts:63-63 (registration)Registers the FileWriterTool instance on the MCP server in the main index entry point.new FileWriterTool().register(server);
- src/mcp-tools-server.ts:69-81 (registration)Registers FileWriterTool (line 75) among other tools in the registerAllTools function used by the dedicated MCP tools server.function registerAllTools(server: McpServer): void { const tools = [ new SwaggerParserTool(), new OptimizedSwaggerParserTool(), new TypeScriptTypesGeneratorTool(), new ApiClientGeneratorTool(), new FileWriterTool(), ]; for (const tool of tools) { tool.register(server); console.log(`✅ 已注册工具: ${tool.name}`); }