file_writer
Writes content to a specified file path, optionally creating directories and appending data, with configurable encoding for file handling.
Instructions
Write content to the specified file path, with support for automatic directory creation
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| append | No | Whether to append to an existing file instead of overwriting it | |
| content | Yes | Content to be written to the file | |
| createDirs | No | Whether to automatically create parent directories if they do not exist | |
| encoding | No | File encoding | utf8 |
| filePath | Yes | Complete path of the file |
Implementation Reference
- src/tools/file-writer-tool.ts:62-99 (handler)Core handler function that executes the file_writer tool logic: creates directories if needed, writes or appends content to file, 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 validation schema for file_writer tool input parameters.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)Registration method of FileWriterTool class that registers the 'file_writer' tool with the MCP server using server.tool(), providing the MCP-compliant handler.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:61-64 (registration)Registration of FileWriterTool (among others) in the main MCP server creation in index.ts.new TypeScriptTypesGeneratorTool().register(server); new ApiClientGeneratorTool().register(server); new FileWriterTool().register(server); new TemplateManagerTool().register(server);
- src/mcp-tools-server.ts:78-81 (registration)Loop that registers FileWriterTool (included in tools array at line 75) with the MCP server in mcp-tools-server.ts.for (const tool of tools) { tool.register(server); console.log(`✅ 已注册工具: ${tool.name}`); }