Skip to content

Commit 40cbb90

Browse files
committed
refactor(text_editor.py): replace 'content' key with 'file_hash' in error responses to streamline error handling
test(text_editor.py): update tests to reflect changes in error response structure by removing assertions for 'content' key
1 parent b16b6f4 commit 40cbb90

File tree

2 files changed

+9
-31
lines changed

2 files changed

+9
-31
lines changed

src/mcp_text_editor/text_editor.py

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ async def edit_file_contents(
239239
- result: "ok" or "error"
240240
- hash: New file hash if successful, None if error
241241
- reason: Error message if result is "error"
242-
"content": None,
242+
"file_hash": None,
243243
}
244244
245245
# Read current file content and verify hash
@@ -251,7 +251,6 @@ async def edit_file_contents(
251251
return {
252252
"result": "error",
253253
"reason": "File not found and non-empty hash provided",
254-
"content": None,
255254
}
256255
# Create parent directories if they don't exist
257256
parent_dir = os.path.dirname(file_path)
@@ -262,7 +261,6 @@ async def edit_file_contents(
262261
return {
263262
"result": "error",
264263
"reason": f"Failed to create directory: {str(e)}",
265-
"content": None,
266264
}
267265
# Initialize empty state for new file
268266
current_content = ""
@@ -284,14 +282,11 @@ async def edit_file_contents(
284282
return {
285283
"result": "error",
286284
"reason": "Unexpected error - Cannot treat existing file as new",
287-
"file_hash": None,
288-
"content": None,
289285
}
290286
elif current_hash != expected_hash:
291287
return {
292288
"result": "error",
293289
"reason": "FileHash mismatch - Please use get_text_file_contents tool to get current content and hashes, then retry with the updated hashes.",
294-
"content": None,
295290
}
296291
else:
297292
lines = current_content.splitlines(keepends=True)
@@ -324,8 +319,6 @@ async def edit_file_contents(
324319
return {
325320
"result": "error",
326321
"reason": "Overlapping patches detected",
327-
"hash": None,
328-
"content": None,
329322
}
330323

331324
# Apply patches
@@ -355,12 +348,7 @@ async def edit_file_contents(
355348
and current_content
356349
and expected_hash == ""
357350
):
358-
return {
359-
"result": "error",
360-
"reason": "Unexpected error",
361-
"file_hash": None,
362-
"content": None,
363-
}
351+
return {"result": "error", "reason": "Unexpected error"}
364352

365353
# Calculate line ranges for zero-based indexing
366354
start_zero = start - 1
@@ -400,7 +388,6 @@ async def edit_file_contents(
400388
return {
401389
"result": "error",
402390
"reason": "Content range hash mismatch - Please use get_text_file_contents tool with the same start and end to get current content and hashes, then retry with the updated hashes.",
403-
"content": current_content,
404391
}
405392

406393
# Prepare new content
@@ -435,18 +422,9 @@ async def edit_file_contents(
435422
}
436423

437424
except FileNotFoundError:
438-
return {
439-
"result": "error",
440-
"reason": f"File not found: {file_path}",
441-
"file_hash": None,
442-
"content": None,
443-
}
425+
return {"result": "error", "reason": f"File not found: {file_path}"}
444426
except (IOError, UnicodeError, PermissionError) as e:
445-
return {
446-
"result": "error",
447-
"reason": f"Error editing file: {str(e)}",
448-
"content": None,
449-
}
427+
return {"result": "error", "reason": f"Error editing file: {str(e)}"}
450428
except Exception as e:
451429
import traceback
452430

@@ -455,7 +433,7 @@ async def edit_file_contents(
455433
return {
456434
"result": "error",
457435
"reason": "Unexpected error occurred",
458-
"content": None,
436+
"file_hash": None,
459437
}
460438

461439
async def insert_text_file_contents(

tests/test_text_editor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ def mock_makedirs(*args, **kwargs):
430430
assert "Failed to create directory" in result["reason"]
431431
assert "Permission denied" in result["reason"]
432432
assert "file_hash" not in result
433-
assert result["content"] is None
433+
assert "content" not in result
434434

435435

436436
@pytest.mark.asyncio
@@ -488,7 +488,7 @@ async def test_file_write_permission_error(editor, tmp_path):
488488
assert "Error editing file" in result["reason"]
489489
assert "Permission" in result["reason"]
490490
assert "file_hash" not in result
491-
assert result["content"] is None
491+
assert "content" not in result
492492

493493

494494
@pytest.mark.asyncio
@@ -566,7 +566,7 @@ async def test_new_file_with_non_empty_hash(editor, tmp_path):
566566
assert result["result"] == "error"
567567
assert "File not found and non-empty hash provided" in result["reason"]
568568
assert "file_hash" not in result
569-
assert result["content"] is None
569+
assert "content" not in result
570570

571571

572572
@pytest.mark.asyncio
@@ -619,7 +619,7 @@ def mock_makedirs(*args, **kwargs):
619619
assert "Failed to create directory" in result["reason"]
620620
assert "Permission denied" in result["reason"]
621621
assert "file_hash" not in result
622-
assert result["content"] is None
622+
assert "content" not in result
623623

624624

625625
@pytest.mark.asyncio

0 commit comments

Comments
 (0)