write_file
Write text content to a specified file within secure directories, enabling file creation and modification with controlled access permissions.
Instructions
指定されたファイルにテキストを書き込みます(許可されたディレクトリのみ)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | Yes | 書き込むファイルのパス | |
| content | Yes | 書き込む内容 |
Implementation Reference
- src/index.ts:342-372 (handler)The core handler function for the 'write_file' tool. Validates the filepath using PathValidator, checks file extension, creates parent directory if necessary, writes content using fs.promises.writeFile, and returns a success CallToolResult.private async writeFile(filepath: string, content: string): Promise<CallToolResult> { try { const pathValidation = this.pathValidator.validatePath(filepath); if (!pathValidation.isValid) { throw new Error(pathValidation.error); } const extValidation = this.pathValidator.validateFileExtension(pathValidation.normalizedPath); if (!extValidation.isValid) { throw new Error(extValidation.error); } console.error(`Writing file: ${pathValidation.normalizedPath}`); // ディレクトリが存在しない場合は作成 const dir = path.dirname(pathValidation.normalizedPath); await fs.mkdir(dir, { recursive: true }); await fs.writeFile(pathValidation.normalizedPath, content, "utf-8"); return { content: [ { type: "text", text: `ファイル "${pathValidation.normalizedPath}" に正常に書き込みました`, }, ], isError: false, }; } catch (error) { throw new Error(`ファイルの書き込みに失敗: ${error}`); } }
- src/index.ts:129-146 (registration)Registration of the 'write_file' tool in the TOOLS constant array. Includes the tool name, description, and inputSchema for parameter validation (filepath: string, content: string). This is returned by ListToolsRequest.{ name: "write_file", description: "指定されたファイルにテキストを書き込みます(許可されたディレクトリのみ)", inputSchema: { type: "object", properties: { filepath: { type: "string", description: "書き込むファイルのパス", }, content: { type: "string", description: "書き込む内容", }, }, required: ["filepath", "content"], }, },
- src/index.ts:278-282 (handler)The dispatch case in the CallToolRequest handler switch statement that routes 'write_file' calls to the writeFile method, extracting filepath and content from arguments.case "write_file": return await this.writeFile( args.filepath as string, args.content as string );
- src/index.ts:132-145 (schema)The inputSchema definition for the 'write_file' tool, specifying required string parameters filepath and content with descriptions.inputSchema: { type: "object", properties: { filepath: { type: "string", description: "書き込むファイルのパス", }, content: { type: "string", description: "書き込む内容", }, }, required: ["filepath", "content"], },