Skip to content

Commit fb87900

Browse files
committed
Merge branch 'feature/specify-encoding' into develop
2 parents 6a45d21 + 969c8ad commit fb87900

File tree

6 files changed

+482
-693
lines changed

6 files changed

+482
-693
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ typecheck:
2424
# Run all checks required before pushing
2525
check: lint typecheck test
2626
fix: check format
27-
all: format check
27+
all: format check coverage

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ MCP Text Editor Server is designed to facilitate safe and efficient line-based t
4747
- Read multiple ranges from multiple files in a single operation
4848
- Line-based patch application with correct handling of line number shifts
4949
- Edit text file contents with conflict detection
50+
- Flexible character encoding support (utf-8, shift_jis, latin1, etc.)
5051
- Support for multiple file operations
5152
- Proper handling of concurrent edits with hash-based validation
5253
- Memory-efficient processing of large files
@@ -143,6 +144,7 @@ Parameters:
143144
- `file_path`: Path to the text file
144145
- `line_start`/`start`: Line number to start from (1-based)
145146
- `line_end`/`end`: Line number to end at (inclusive, null for end of file)
147+
- `encoding`: File encoding (default: "utf-8"). Specify the encoding of the text file (e.g., "shift_jis", "latin1")
146148

147149
**Single Range Response:**
148150

@@ -238,6 +240,7 @@ Important Notes:
238240
3. Patches must not overlap within the same file
239241
4. Line numbers are 1-based
240242
5. If original content ends with newline, ensure patch content also ends with newline
243+
6. File encoding must match the encoding used in get_text_file_contents
241244

242245
**Success Response:**
243246

@@ -295,6 +298,7 @@ result = await edit_text_file_contents({
295298
{
296299
"path": "file.txt",
297300
"hash": contents["file.txt"][0]["hash"],
301+
"encoding": "utf-8", # Optional, defaults to "utf-8"
298302
"patches": [
299303
{
300304
"line_start": 5,
@@ -325,6 +329,7 @@ The server handles various error cases:
325329
- Hash mismatches (concurrent edit detection)
326330
- Invalid patch ranges
327331
- Overlapping patches
332+
- Encoding errors (when file cannot be decoded with specified encoding)
328333
- Line number out of bounds
329334

330335
## Security Considerations

src/mcp_text_editor/server.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ def get_tool_description(self) -> Tool:
6767
},
6868
"required": ["file_path", "ranges"],
6969
},
70-
}
70+
},
71+
"encoding": {
72+
"type": "string",
73+
"description": "Text encoding (default: 'utf-8')",
74+
"default": "utf-8",
75+
},
7176
},
7277
"required": ["files"],
7378
},
@@ -81,7 +86,10 @@ async def run_tool(self, arguments: Dict[str, Any]) -> Sequence[TextContent]:
8186
raise RuntimeError("Missing required argument: 'files'")
8287

8388
# Handle request
84-
result = await self.editor.read_multiple_ranges(arguments["files"])
89+
encoding = arguments.get("encoding", "utf-8")
90+
result = await self.editor.read_multiple_ranges(
91+
arguments["files"], encoding=encoding
92+
)
8593
response = result
8694

8795
return [TextContent(type="text", text=json.dumps(response, indent=2))]
@@ -130,14 +138,23 @@ def get_tool_description(self) -> Tool:
130138
"default": None,
131139
},
132140
"contents": {"type": "string"},
141+
"range_hash": {
142+
"type": "string",
143+
"description": "Hash of the content being replaced (required except for new files and append operations)",
144+
},
133145
},
134146
"required": ["contents"],
135147
},
136148
},
137149
},
138150
"required": ["path", "file_hash", "patches"],
139151
},
140-
}
152+
},
153+
"encoding": {
154+
"type": "string",
155+
"description": "Text encoding (default: 'utf-8')",
156+
"default": "utf-8",
157+
},
141158
},
142159
"required": ["files"],
143160
},
@@ -189,8 +206,9 @@ async def run_tool(self, arguments: Dict[str, Any]) -> Sequence[TextContent]:
189206
}
190207
continue
191208

209+
encoding = arguments.get("encoding", "utf-8")
192210
result = await self.editor.edit_file_contents(
193-
file_path, file_hash, patches
211+
file_path, file_hash, patches, encoding=encoding
194212
)
195213
results[file_path] = result
196214
except Exception as e:

0 commit comments

Comments
 (0)