The VS Code MCP Server provides two primary tools for modifying files in your workspace. Choosing the right tool depends on the size of the change and whether you have the exact current content for validation.
create_file_code
When to use:
- Creating new files.
- Large modifications (>10 lines).
- Complete file rewrites.
Key Options:
overwrite: Set to true to replace an existing file.ignoreIfExists: Set to true to skip the operation if the file already exists.
replace_lines_code
When to use:
- Small edits (≤10 lines) where you have the exact original text.
- Inserting content of any size.
CRITICAL: The originalCode parameter must match the current file content exactly. If the tool fails due to a mismatch, use read_file_code to retrieve the current content before retrying. This tool uses 1-based line numbers.
Troubleshooting:
If replace_lines_code fails, it is likely because the originalCode provided does not match the file's current state. Always verify line numbers and content with read_file_code first.
// Example conceptual usage of the tools via MCP
// 1. Create a new file
// Tool: create_file_code
// Args: { "path": "src/newfile.ts", "content": "console.log('hello');" }
// 2. Replace a small block of code
// Tool: replace_lines_code
// Args: {
// "path": "src/app.ts",
// "startLine": 10,
// "endLine": 12,
// "content": "const x = 5;",
// "originalCode": "const old = 0;"
// }