|
8 | 8 |
|
9 | 9 | from mcp.types import TextContent, Tool
|
10 | 10 |
|
| 11 | +from ..models import DeleteTextFileContentsRequest, FileRange |
11 | 12 | from .base import BaseHandler
|
12 | 13 |
|
13 | 14 | logger = logging.getLogger("mcp-text-editor")
|
@@ -88,26 +89,33 @@ async def run_tool(self, arguments: Dict[str, Any]) -> Sequence[TextContent]:
|
88 | 89 |
|
89 | 90 | encoding = arguments.get("encoding", "utf-8")
|
90 | 91 |
|
91 |
| - # Create patches for deletion (replacing content with empty string) |
92 |
| - patches = [ |
93 |
| - { |
94 |
| - "start": r["start"], |
95 |
| - "end": r["end"], |
96 |
| - "contents": "", |
97 |
| - "range_hash": r["range_hash"], |
98 |
| - } |
| 92 | + # Create file ranges for deletion |
| 93 | + ranges = [ |
| 94 | + FileRange( |
| 95 | + start=r["start"], end=r.get("end"), range_hash=r["range_hash"] |
| 96 | + ) |
99 | 97 | for r in arguments["ranges"]
|
100 | 98 | ]
|
101 | 99 |
|
102 |
| - # Use the existing edit_file_contents method |
103 |
| - result = await self.editor.edit_file_contents( |
104 |
| - file_path, |
105 |
| - expected_hash=arguments["file_hash"], |
106 |
| - patches=patches, |
| 100 | + # Create delete request |
| 101 | + request = DeleteTextFileContentsRequest( |
| 102 | + file_path=file_path, |
| 103 | + file_hash=arguments["file_hash"], |
| 104 | + ranges=ranges, |
107 | 105 | encoding=encoding,
|
108 | 106 | )
|
109 | 107 |
|
110 |
| - return [TextContent(type="text", text=json.dumps(result, indent=2))] |
| 108 | + # Execute deletion using the service |
| 109 | + result_dict = self.editor.service.delete_text_file_contents(request) |
| 110 | + |
| 111 | + # Convert EditResults to dictionaries |
| 112 | + serializable_result = {} |
| 113 | + for file_path, edit_result in result_dict.items(): |
| 114 | + serializable_result[file_path] = edit_result.to_dict() |
| 115 | + |
| 116 | + return [ |
| 117 | + TextContent(type="text", text=json.dumps(serializable_result, indent=2)) |
| 118 | + ] |
111 | 119 |
|
112 | 120 | except Exception as e:
|
113 | 121 | logger.error(f"Error processing request: {str(e)}")
|
|
0 commit comments