Skip to main content
Glama

write_content

Write or append content to multiple files in specific paths, creating necessary directories automatically. Ideal for managing and updating file content efficiently.

Instructions

Write or append content to multiple specified files (creating directories if needed).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
itemsYesArray of {path, content, append?} objects.

Implementation Reference

  • Main handler function `handleWriteContentFunc` that validates arguments, processes multiple file write/append operations concurrently using Promise.allSettled, sorts results by original order, and returns JSON-formatted output.
    export const handleWriteContentFunc = async ( // Added export deps: WriteContentDependencies, args: unknown, ): Promise<McpToolResponse> => { const { items: filesToWrite } = parseAndValidateArgs(args); const writePromises = filesToWrite.map((file) => processSingleWriteOperation(file, deps)); const settledResults = await Promise.allSettled(writePromises); const outputResults = processSettledResults(settledResults, filesToWrite); // Sort results based on the original order const originalIndexMap = new Map(filesToWrite.map((f, i) => [f.path.replaceAll('\\', '/'), i])); outputResults.sort((a, b) => { const indexA = originalIndexMap.get(a.path) ?? Infinity; const indexB = originalIndexMap.get(b.path) ?? Infinity; return indexA - indexB; }); return { content: [{ type: 'text', text: JSON.stringify(outputResults, null, 2) }], }; };
  • Zod schemas: `WriteItemSchema` for individual file items and `WriteContentArgsSchema` for the tool input (array of items).
    export const WriteItemSchema = z .object({ path: z.string().describe('Relative path for the file.'), content: z.string().describe('Content to write.'), append: z .boolean() .optional() .default(false) .describe('Append content instead of overwriting.'), }) .strict(); export const WriteContentArgsSchema = z .object({ items: z .array(WriteItemSchema) .min(1, { message: 'Items array cannot be empty' }) .describe('Array of {path, content, append?} objects.'), }) .strict();
  • Tool definition object `writeContentToolDefinition` with name 'write_content', description, inputSchema, and handler function that initializes dependencies and calls the main handler.
    export const writeContentToolDefinition = { name: 'write_content', description: "Write or append content to multiple specified files (creating directories if needed). NOTE: For modifying existing files, prefer using 'edit_file' or 'replace_content' for better performance, especially with large files. Use 'write_content' primarily for creating new files or complete overwrites.", inputSchema: WriteContentArgsSchema, handler: (args: unknown): Promise<McpToolResponse> => { const deps: WriteContentDependencies = { writeFile: fs.writeFile, mkdir: fs.mkdir, stat: fs.stat, appendFile: fs.appendFile, resolvePath: resolvePath, PROJECT_ROOT: PROJECT_ROOT, pathDirname: path.dirname.bind(path), }; return handleWriteContentFunc(deps, args); }, };
  • Inclusion of `writeContentToolDefinition` in the `allToolDefinitions` array exported from handlers index.
    writeContentToolDefinition,
  • Helper function `parseAndValidateArgs` for parsing and validating tool input using the schema, throwing McpError on failure.
    function parseAndValidateArgs(args: unknown): WriteContentArgs { try { return WriteContentArgsSchema.parse(args); } catch (error) { if (error instanceof z.ZodError) { throw new McpError( ErrorCode.InvalidParams, `Invalid arguments: ${error.errors.map((e) => `${e.path.join('.')} (${e.message})`).join(', ')}`, ); } throw new McpError(ErrorCode.InvalidParams, 'Argument validation failed'); } }

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/SylphxAI/filesystem-mcp'

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