Skip to content

Commit 79af627

Browse files
author
Yoshihiro Takahara
committed
chore: simplify server code and fix lint errors
- Improve error handling in GetTextFileContentsHandler - Maintain consistency in error messages - Remove legacy format support in GetTextFileContentsHandler - Add proper error handling for missing required fields
1 parent 132cb7e commit 79af627

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/mcp_text_editor/server.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ def get_tool_description(self) -> Tool:
7676
async def run_tool(self, arguments: Dict[str, Any]) -> Sequence[TextContent]:
7777
"""Execute the tool with given arguments."""
7878
try:
79-
# Handle 'files' key missing
8079
if "files" not in arguments:
8180
if "file_path" not in arguments:
8281
raise RuntimeError("Missing required argument: 'files'")
8382

84-
# Convert legacy format to new format
83+
# Legacy format support
8584
file_path = arguments["file_path"]
8685
line_start = arguments.get("line_start", 1)
8786
line_end = arguments.get("line_end")
87+
# Convert to new format with mandatory fields
8888
arguments = {
8989
"files": [
9090
{
@@ -98,15 +98,17 @@ async def run_tool(self, arguments: Dict[str, Any]) -> Sequence[TextContent]:
9898
}
9999
]
100100
}
101+
# Legacy format request
101102
legacy_format = True
102103
else:
103104
legacy_format = False
104105

105106
# Handle request
106107
result = await self.editor.read_multiple_ranges(arguments["files"])
107108

108-
# Convert to legacy format if it was a legacy request
109+
# Convert to appropriate format based on request type
109110
if legacy_format:
111+
# Legacy format expects a flat structure
110112
file_path = arguments["files"][0]["file_path"]
111113
file_result = result[file_path]
112114
range_result = file_result["ranges"][0]
@@ -120,14 +122,15 @@ async def run_tool(self, arguments: Dict[str, Any]) -> Sequence[TextContent]:
120122
"file_size": range_result["content_size"],
121123
}
122124
else:
125+
# New format returns multi-file, multi-range structure
123126
response = result
124127

125128
return [TextContent(type="text", text=json.dumps(response, indent=2))]
126129

127130
except KeyError as e:
128-
raise RuntimeError(f"Missing required argument: '{e}'")
131+
raise RuntimeError(f"Missing required argument: '{e}'") from e
129132
except Exception as e:
130-
raise RuntimeError(f"Error processing request: {str(e)}")
133+
raise RuntimeError(f"Error processing request: {str(e)}") from e
131134

132135

133136
class EditTextFileContentsHandler:

0 commit comments

Comments
 (0)