write_file
Write or overwrite text files in Obsidian iCloud vaults, ensuring proper encoding and handling specific paths under the Documents/my-vault directory.
Instructions
Your task is to write file to an appropriate path under /Users/username/Library/Mobile Documents/iCloud~md~obsidian/Documents/my-vault. The path you'll write should follow user's instruction and make sure it hasn't been occupied.Create a new file or completely overwrite an existing file with new content. Use with caution as it will overwrite existing files without warning. Handles text content with proper encoding. Only works within allowed directories.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | ||
| path | Yes |
Implementation Reference
- src/file-system.ts:237-249 (handler)The core handler function that implements the write_file tool. It validates input using WriteFileArgsSchema, writes the file content to the specified path using fs.writeFile, and returns a success message.export async function writeFile(args?: Record<string, unknown>) { const parsed = WriteFileArgsSchema.safeParse(args) if (!parsed.success) { throw new Error(`Invalid arguments for write_file: ${parsed.error}`) } await fs.writeFile(parsed.data.path, parsed.data.content, 'utf-8') return { content: [ { type: 'text', text: `Successfully wrote to ${parsed.data.path}` } ] } }
- src/schemas.ts:12-15 (schema)Zod schema defining the input parameters for the write_file tool: path (string) and content (string). Used for validation in the handler.export const WriteFileArgsSchema = z.object({ path: z.string(), content: z.string() })
- src/index.ts:110-114 (registration)Tool registration in the ListToolsRequestHandler response. Specifies the tool name 'write_file', a dynamic description generated by writeFilePrompt, and the input schema converted to JSON schema.{ name: 'write_file', description: writeFilePrompt(args), inputSchema: zodToJsonSchema(WriteFileArgsSchema) as ToolInput },
- src/index.ts:179-181 (registration)Dispatch logic in the CallToolRequestHandler switch statement that routes 'write_file' calls to the writeFile handler function.case 'write_file': { return writeFile(args) }
- src/prompts.ts:15-20 (helper)Helper function that generates the tool description/prompt for write_file, incorporating the allowed root paths for safety.export const writeFilePrompt = (rootPaths: string[]) => `Your task is to write file to an appropriate path under ${rootPaths.join(', ')}. ` + "The path you'll write should follow user's instruction and make sure it hasn't been occupied." + 'Create a new file or completely overwrite an existing file with new content. ' + 'Use with caution as it will overwrite existing files without warning. ' + 'Handles text content with proper encoding. Only works within allowed directories.'