write_file
Write text to specified files within secure directories using the Simple MCP Server, ensuring controlled and authorized file operations.
Instructions
指定されたファイルにテキストを書き込みます(許可されたディレクトリのみ)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | 書き込む内容 | |
| filepath | Yes | 書き込むファイルのパス |
Implementation Reference
- src/index.ts:342-372 (handler)The handler function for the 'write_file' tool. Validates the filepath using PathValidator, ensures allowed extension, creates directory if needed, writes content to file using fs.writeFile, and returns a success result.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 (schema)The input schema definition for the 'write_file' tool, specifying required 'filepath' and 'content' string parameters.{ name: "write_file", description: "指定されたファイルにテキストを書き込みます(許可されたディレクトリのみ)", inputSchema: { type: "object", properties: { filepath: { type: "string", description: "書き込むファイルのパス", }, content: { type: "string", description: "書き込む内容", }, }, required: ["filepath", "content"], }, },
- src/index.ts:278-282 (registration)The dispatch/registration case in the CallToolRequestSchema handler that routes 'write_file' calls to the writeFile method.case "write_file": return await this.writeFile( args.filepath as string, args.content as string );