Skip to content

Commit 3e9d8bc

Browse files
tumfwkmtumf
authored andcommitted
fix(server.py): remove unused content field from error response in EditTextFileContentsHandler to clean up the response structure
test(server.py): add unit test for EditTextFileContentsHandler to verify handling of multiple patches in a single file to ensure correct functionality and prevent regressions
1 parent 3411ff7 commit 3e9d8bc

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/mcp_text_editor/server.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ async def run_tool(self, arguments: Dict[str, Any]) -> Sequence[TextContent]:
192192
"result": "error",
193193
"reason": "Empty patches list",
194194
"file_hash": file_hash,
195-
"content": None,
196195
}
197196
continue
198197

@@ -206,7 +205,7 @@ async def run_tool(self, arguments: Dict[str, Any]) -> Sequence[TextContent]:
206205
if "path" in file_operation:
207206
file_path = file_operation["path"]
208207
try:
209-
current_content, _, _, current_hash, _, _ = (
208+
_, _, _, current_hash, _, _ = (
210209
await self.editor.read_file_contents(file_path)
211210
)
212211
except Exception:
@@ -216,7 +215,6 @@ async def run_tool(self, arguments: Dict[str, Any]) -> Sequence[TextContent]:
216215
"result": "error",
217216
"reason": str(e),
218217
"file_hash": current_hash,
219-
"content": None,
220218
}
221219

222220
return [TextContent(type="text", text=json.dumps(results, indent=2))]

tests/test_server.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,61 @@ async def test_edit_contents_handler_missing_patches():
404404
with pytest.raises(RuntimeError) as exc_info:
405405
await edit_contents_handler.run_tool(edit_args)
406406
assert "Missing required field: patches" in str(exc_info.value)
407+
408+
409+
@pytest.mark.asyncio
410+
async def test_edit_contents_handler_multiple_patches(tmp_path):
411+
"""Test EditTextFileContents handler with multiple patches in a single file."""
412+
test_file = tmp_path / "test.txt"
413+
test_file.write_text("line1\nline2\nline3\nline4\nline5\n")
414+
file_path = str(test_file)
415+
416+
get_args = {"files": [{"file_path": file_path, "ranges": [{"start": 1}]}]}
417+
get_result = await get_contents_handler.run_tool(get_args)
418+
content_info = json.loads(get_result[0].text)
419+
file_hash = content_info[file_path]["file_hash"]
420+
421+
ranges = [{"start": 2, "end": 2}, {"start": 4, "end": 4}]
422+
get_range_args = {"files": [{"file_path": file_path, "ranges": ranges}]}
423+
range_result = json.loads(
424+
(await get_contents_handler.run_tool(get_range_args))[0].text
425+
)
426+
range_hashes = [r["range_hash"] for r in range_result[file_path]["ranges"]]
427+
428+
edit_args = {
429+
"files": [
430+
{
431+
"path": file_path,
432+
"file_hash": file_hash,
433+
"patches": [
434+
{
435+
"line_start": 2,
436+
"line_end": 2,
437+
"contents": "Modified Line 2\n",
438+
"range_hash": range_hashes[0],
439+
},
440+
{
441+
"line_start": 4,
442+
"line_end": 4,
443+
"contents": "Modified Line 4\n",
444+
"range_hash": range_hashes[1],
445+
},
446+
],
447+
}
448+
]
449+
}
450+
451+
# 編集を適用
452+
result = await edit_contents_handler.run_tool(edit_args)
453+
454+
# 結果の検証
455+
assert len(result) == 1
456+
edit_results = json.loads(result[0].text)
457+
assert file_path in edit_results
458+
assert edit_results[file_path]["result"] == "ok"
459+
460+
# ファイルの内容を確認
461+
with open(file_path) as f:
462+
content = f.read()
463+
assert "Modified Line 2" in content
464+
assert "Modified Line 4" in content

0 commit comments

Comments
 (0)