diff --git a/src/filesystem/README.md b/src/filesystem/README.md index 3d3fb48556..762db2dfd8 100644 --- a/src/filesystem/README.md +++ b/src/filesystem/README.md @@ -36,6 +36,13 @@ Node.js server implementing Model Context Protocol (MCP) for filesystem operatio - `path` (string): File location - `content` (string): File content +- **append_file** + - Append content to an existing file without overwriting previous content + - Inputs: + - `path` (string): File location + - `content` (string): Content to append + - Useful for adding content to log files or continuing content from previous operations + - **edit_file** - Make selective edits using advanced pattern matching and formatting - Features: diff --git a/src/filesystem/index.ts b/src/filesystem/index.ts index c544ff2571..df5a231cd5 100644 --- a/src/filesystem/index.ts +++ b/src/filesystem/index.ts @@ -108,6 +108,11 @@ const WriteFileArgsSchema = z.object({ content: z.string(), }); +const AppendFileArgsSchema = z.object({ + path: z.string(), + content: z.string(), +}); + const EditOperation = z.object({ oldText: z.string().describe('Text to search for - must match exactly'), newText: z.string().describe('Text to replace with') @@ -343,6 +348,15 @@ server.setRequestHandler(ListToolsRequestSchema, async () => { "the contents of a single file. Only works within allowed directories.", inputSchema: zodToJsonSchema(ReadFileArgsSchema) as ToolInput, }, + { + name: "append_file", + description: + "Append content to an existing file or create a new file if it doesn't exist. " + + "This is safer than overwriting when you need to add content incrementally. " + + "Preserves existing content and adds new content at the end of the file. " + + "Handles text content with proper encoding. Only works within allowed directories.", + inputSchema: zodToJsonSchema(AppendFileArgsSchema) as ToolInput, + }, { name: "read_multiple_files", description: @@ -366,6 +380,8 @@ server.setRequestHandler(ListToolsRequestSchema, async () => { description: "Make line-based edits to a text file. Each edit replaces exact line sequences " + "with new content. Returns a git-style diff showing the changes made. " + + "When modifying existing files, prefer using this tool for partial changes (adding/modifying/removing specific parts) " + + "rather than rewriting the entire or substantial portions of the file. " + "Only works within allowed directories.", inputSchema: zodToJsonSchema(EditFileArgsSchema) as ToolInput, }, @@ -491,6 +507,18 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => { }; } + case "append_file": { + const parsed = AppendFileArgsSchema.safeParse(args); + if (!parsed.success) { + throw new Error(`Invalid arguments for append_file: ${parsed.error}`); + } + const validPath = await validatePath(parsed.data.path); + await fs.appendFile(validPath, parsed.data.content, "utf-8"); + return { + content: [{ type: "text", text: `Successfully appended to ${parsed.data.path}` }], + }; + } + case "edit_file": { const parsed = EditFileArgsSchema.safeParse(args); if (!parsed.success) {