MCPcopy Create free account
hub / github.com/vercel/examples / edit_code

Function edit_code

python/vibe-coding-ide/backend/src/agent/tools/core.py:71–144  ·  view source on GitHub ↗

Make a precise, in-place change within a file. Behavior: - Operates only on lines [find_start_line, find_end_line] (1-based, inclusive). - 'find' must appear within that range; only that matched text is replaced. - 'replace' is the full new text for the matched portion; no line numb

(
    ctx: RunContextWrapper[IDEContext],
    file_path: str,
    find: str,
    find_start_line: int,
    find_end_line: int,
    replace: str,
)

Source from the content-addressed store, hash-verified

69
70@function_tool
71async def edit_code(
72 ctx: RunContextWrapper[IDEContext],
73 file_path: str,
74 find: str,
75 find_start_line: int,
76 find_end_line: int,
77 replace: str,
78) -> str:
79 """Make a precise, in-place change within a file.
80
81 Behavior:
82 - Operates only on lines [find_start_line, find_end_line] (1-based, inclusive).
83 - 'find' must appear within that range; only that matched text is replaced.
84 - 'replace' is the full new text for the matched portion; no line numbers.
85 - Content outside the selected range is preserved exactly.
86
87 Guidelines:
88 - Choose the smallest line range that brackets the intended change.
89 - For multiple non-adjacent edits, call this tool multiple times.
90 - Preserve formatting, imports, and surrounding structure.
91
92 Args:
93 file_path: Project-relative file path.
94 find: Exact text to replace within the specified range.
95 find_start_line: Start line (1-based, inclusive).
96 find_end_line: End line (1-based, inclusive).
97 replace: Replacement text (no line numbers).
98 Returns:
99 JSON string describing the edit or an error.
100 """
101 tool_id = f"tc_{len(ctx.context.events) + 1}"
102 args = {
103 "file_path": file_path,
104 "find": find,
105 "find_start_line": find_start_line,
106 "find_end_line": find_end_line,
107 "replace": replace,
108 }
109 ctx.context.events.append(
110 {
111 "phase": "started",
112 "tool_id": tool_id,
113 "name": "edit_code",
114 "arguments": args,
115 }
116 )
117 if file_path not in ctx.context.project:
118 output = {"error": f"File not found: {file_path}"}
119 else:
120 output = _perform_edit_code(ctx.context.project[file_path], args)
121 if "new_code" in output:
122 ctx.context.project[file_path] = output["new_code"]
123 # enrich output with file info for the UI
124 output = {
125 **output,
126 "file_path": file_path,
127 "new_file_content": output["new_code"],
128 }

Callers

nothing calls this directly

Calls 2

autosync_after_fs_changeFunction · 0.90
_perform_edit_codeFunction · 0.85

Tested by

no test coverage detected