write_file
Write content to a file at a specified path for data storage, configuration management, or output generation.
Instructions
Write content to a file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | The content to write to the file | |
| path | Yes | The path to the file to write |
Implementation Reference
- server.js:232-246 (handler)The writeFile handler method executes the write_file tool logic: writes content to the specified file path using fs.writeFileSync equivalent (promises), handles errors, and returns MCP-formatted TextContent success message.async writeFile(filePath, content) { try { await fs.writeFile(filePath, content, 'utf-8'); return { content: [ { type: 'text', text: `Successfully wrote to ${filePath}`, }, ], }; } catch (error) { throw new Error(`Failed to write file: ${error.message}`); } }
- server.js:58-75 (schema)The input schema definition for the write_file tool, registered in the ListToolsRequestSchema handler, specifying path and content as required string parameters.{ name: 'write_file', description: 'Write content to a file', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'The path to the file to write', }, content: { type: 'string', description: 'The content to write to the file', }, }, required: ['path', 'content'], }, },
- server.py:208-217 (handler)The write_file handler method executes the tool logic: creates parent directories if needed, writes content using Path.write_text, returns MCP TextContent success or raises error.async def write_file(self, file_path: str, content: str) -> list[types.TextContent]: """Write content to file""" try: path = Path(file_path) path.parent.mkdir(parents=True, exist_ok=True) path.write_text(content, encoding="utf-8") return [types.TextContent(type="text", text=f"Successfully wrote to {file_path}")] except Exception as error: raise Exception(f"Failed to write file: {str(error)}")
- server.py:86-103 (schema)The Tool object definition for write_file including inputSchema, used in the list_tools handler for registration.types.Tool( name="write_file", description="Write content to a file", inputSchema={ "type": "object", "properties": { "path": { "type": "string", "description": "The path to the file to write", }, "content": { "type": "string", "description": "The content to write to the file", }, }, "required": ["path", "content"], }, ),
- chatgpt-proxy.js:225-227 (handler)Direct implementation of write_file in the OpenAI proxy's tool execution switch, using fs.writeFile and returning success object.case 'write_file': await fs.writeFile(args.path, args.content, 'utf-8'); return { success: true, message: `Successfully wrote to ${args.path}` };