Skip to content

Commit 37bee30

Browse files
committed
Merge branch 'fix-patch-end-none' into develop
2 parents eb77392 + baaf290 commit 37bee30

File tree

4 files changed

+64
-13
lines changed

4 files changed

+64
-13
lines changed

src/mcp_text_editor/handlers/patch_text_file_contents.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get_tool_description(self) -> Tool:
4646
"description": "Starting line number (1-based).it should match the range hash.",
4747
},
4848
"end": {
49-
"type": ["integer", "null"],
49+
"type": "integer",
5050
"description": "Ending line number (null for end of file).it should match the range hash.",
5151
},
5252
"contents": {
@@ -58,7 +58,7 @@ def get_tool_description(self) -> Tool:
5858
"description": "Hash of the content being replaced. it should get from get_text_file_contents tool with the same start and end.",
5959
},
6060
},
61-
"required": ["start", "contents", "range_hash"],
61+
"required": ["start", "end", "contents", "range_hash"],
6262
},
6363
},
6464
"encoding": {

src/mcp_text_editor/models.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ class EditPatch(BaseModel):
3434
)
3535

3636
@model_validator(mode="after")
37-
def validate_end_line(self) -> "EditPatch":
38-
"""Validate that end line is present when not in append mode."""
37+
def validate_range_hash(self) -> "EditPatch":
38+
"""Validate that range_hash is set and handle end field validation."""
3939
# range_hash must be explicitly set
4040
if self.range_hash is None:
4141
raise ValueError("range_hash is required")
4242

43-
# For modifications (non-empty range_hash), end is required
44-
if self.range_hash != "" and self.end is None:
45-
raise ValueError("end line is required when not in append mode")
43+
# For safety, convert None to the special range hash value
44+
if self.end is None and self.range_hash != "":
45+
# Special case: patch with end=None is allowed
46+
pass
47+
4648
return self
4749

4850

tests/test_models.py

-6
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ def test_edit_patch():
7474
with pytest.raises(ValidationError):
7575
EditPatch()
7676

77-
# Test validation error - missing end in modification mode
78-
with pytest.raises(
79-
ValueError, match="end line is required when not in append mode"
80-
):
81-
EditPatch(start=1, contents="content", range_hash="somehash")
82-
8377

8478
def test_edit_file_operation():
8579
"""Test EditFileOperation model."""
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Test module for patch_text_file with end=None."""
2+
3+
import os
4+
5+
import pytest
6+
7+
from mcp_text_editor.text_editor import TextEditor
8+
9+
10+
@pytest.mark.asyncio
11+
async def test_patch_text_file_end_none(tmp_path):
12+
"""Test patching text file with end=None."""
13+
# Create a test file
14+
file_path = os.path.join(tmp_path, "test.txt")
15+
editor = TextEditor()
16+
17+
# Create initial content
18+
content = "line1\nline2\nline3\nline4\nline5\n"
19+
with open(file_path, "w", encoding="utf-8") as f:
20+
f.write(content)
21+
22+
# Get file hash and range hash
23+
file_info = await editor.read_multiple_ranges(
24+
[
25+
{
26+
"file_path": str(file_path),
27+
"ranges": [{"start": 2, "end": None}], # Test with end=None
28+
}
29+
]
30+
)
31+
32+
# Extract file and range hashes
33+
file_content = file_info[str(file_path)]
34+
file_hash = file_content["file_hash"]
35+
range_hash = file_content["ranges"][0]["range_hash"]
36+
37+
# Patch the file
38+
new_content = "new line2\nnew line3\nnew line4\nnew line5\n"
39+
patch = {
40+
"start": 2,
41+
"end": None, # Test with end=None
42+
"contents": new_content,
43+
"range_hash": range_hash,
44+
}
45+
result = await editor.edit_file_contents(
46+
str(file_path),
47+
file_hash,
48+
[patch],
49+
)
50+
51+
# Verify the patch was successful
52+
assert result["result"] == "ok"
53+
with open(file_path, "r", encoding="utf-8") as f:
54+
updated_content = f.read()
55+
assert updated_content == "line1\nnew line2\nnew line3\nnew line4\nnew line5\n"

0 commit comments

Comments
 (0)