Skip to content

feat(filesystem): add append_file #2093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/filesystem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ Node.js server implementing Model Context Protocol (MCP) for filesystem operatio
- Returns detailed diff and match information for dry runs, otherwise applies changes
- Best Practice: Always use dryRun first to preview changes before applying them

- **append_file**
- Append content to existing file or create new file if it doesn't exist
- Inputs:
- `path` (string): File location
- `content` (string): Content to append
- Features:
- Creates file if it doesn't exist
- Appends content to the end of the file

- **create_directory**
- Create new directory or ensure it exists
- Input: `path` (string)
Expand Down
25 changes: 25 additions & 0 deletions src/filesystem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ const EditFileArgsSchema = z.object({
dryRun: z.boolean().default(false).describe('Preview changes using git-style diff format')
});

const AppendFileArgsSchema = z.object({
path: z.string(),
content: z.string(),
});

const CreateDirectoryArgsSchema = z.object({
path: z.string(),
});
Expand Down Expand Up @@ -369,6 +374,13 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
"Only works within allowed directories.",
inputSchema: zodToJsonSchema(EditFileArgsSchema) as ToolInput,
},
{
name: "append_file",
description:
"Appends content to a file. If the file does not exist, it will be created. " +
"Only works within allowed directories.",
inputSchema: zodToJsonSchema(AppendFileArgsSchema) as ToolInput,
},
{
name: "create_directory",
description:
Expand Down Expand Up @@ -503,6 +515,19 @@ 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 "create_directory": {
const parsed = CreateDirectoryArgsSchema.safeParse(args);
if (!parsed.success) {
Expand Down