Skip to main content
Glama

Filesystem MCP Server

Official

write_file

Create or overwrite files with specified content in allowed directories using the Filesystem MCP Server. Ensures proper text encoding for accurate file handling.

Instructions

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

NameRequiredDescriptionDefault
contentYes
pathYes

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "content": { "type": "string" }, "path": { "type": "string" } }, "required": [ "path", "content" ], "type": "object" }

Implementation Reference

  • The handler function for the 'write_file' tool that validates the path, writes the content using writeFileContent, and returns a success message.
    async (args: z.infer<typeof WriteFileArgsSchema>) => { const validPath = await validatePath(args.path); await writeFileContent(validPath, args.content); const text = `Successfully wrote to ${args.path}`; return { content: [{ type: "text" as const, text }], structuredContent: { content: text } }; }
  • Zod schema defining the input arguments for the write_file tool: path and content.
    const WriteFileArgsSchema = z.object({ path: z.string(), content: z.string(), });
  • Registration of the 'write_file' tool with the MCP server, specifying title, description, input/output schemas, annotations, and handler function.
    server.registerTool( "write_file", { title: "Write File", description: "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.", inputSchema: { path: z.string(), content: z.string() }, outputSchema: { content: z.string() }, annotations: { readOnlyHint: false, idempotentHint: true, destructiveHint: true } }, async (args: z.infer<typeof WriteFileArgsSchema>) => { const validPath = await validatePath(args.path); await writeFileContent(validPath, args.content); const text = `Successfully wrote to ${args.path}`; return { content: [{ type: "text" as const, text }], structuredContent: { content: text } }; } );
  • Helper function implementing secure atomic file writing, handling new files and overwrites with symlink protection and race condition prevention.
    export async function writeFileContent(filePath: string, content: string): Promise<void> { try { // Security: 'wx' flag ensures exclusive creation - fails if file/symlink exists, // preventing writes through pre-existing symlinks await fs.writeFile(filePath, content, { encoding: "utf-8", flag: 'wx' }); } catch (error) { if ((error as NodeJS.ErrnoException).code === 'EEXIST') { // Security: Use atomic rename to prevent race conditions where symlinks // could be created between validation and write. Rename operations // replace the target file atomically and don't follow symlinks. const tempPath = `${filePath}.${randomBytes(16).toString('hex')}.tmp`; try { await fs.writeFile(tempPath, content, 'utf-8'); await fs.rename(tempPath, filePath); } catch (renameError) { try { await fs.unlink(tempPath); } catch {} throw renameError; } } else { throw error; } } }

Other Tools

Related Tools

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/modelcontextprotocol/filesystem'

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