Skip to main content
Glama

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
NameRequiredDescriptionDefault
filepathYes書き込むファイルのパス
contentYes書き込む内容

Implementation Reference

  • 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"], }, },
  • 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 );
  • 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"], },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/p-united/mcpSample'

If you have feedback or need assistance with the MCP directory API, please join our Discord server