Read multiple files in a single operation (for batch reading) Args: file_requests: JSON string with file requests, e.g., '{"file1.py": {}, "file2.py": {"start_line": 1, "end_line": 10}}' or simple array: '["file1.py", "file2.py"]'
(file_requests: str, max_files: int = 5)
| 178 | |
| 179 | @mcp.tool() |
| 180 | async def read_multiple_files(file_requests: str, max_files: int = 5) -> str: |
| 181 | """ |
| 182 | Read multiple files in a single operation (for batch reading) |
| 183 | |
| 184 | Args: |
| 185 | file_requests: JSON string with file requests, e.g., |
| 186 | '{"file1.py": {}, "file2.py": {"start_line": 1, "end_line": 10}}' |
| 187 | or simple array: '["file1.py", "file2.py"]' |
| 188 | max_files: Maximum number of files to read in one operation (default: 5) |
| 189 | |
| 190 | Returns: |
| 191 | JSON string of operation results for all files |
| 192 | """ |
| 193 | try: |
| 194 | # Parse the file requests |
| 195 | try: |
| 196 | requests_data = json.loads(file_requests) |
| 197 | except json.JSONDecodeError as e: |
| 198 | return json.dumps( |
| 199 | { |
| 200 | "status": "error", |
| 201 | "message": f"Invalid JSON format for file_requests: {str(e)}", |
| 202 | "operation_type": "multi_file", |
| 203 | "timestamp": datetime.now().isoformat(), |
| 204 | }, |
| 205 | ensure_ascii=False, |
| 206 | indent=2, |
| 207 | ) |
| 208 | |
| 209 | # Normalize requests format |
| 210 | if isinstance(requests_data, list): |
| 211 | # Convert simple array to dict format |
| 212 | normalized_requests = {file_path: {} for file_path in requests_data} |
| 213 | elif isinstance(requests_data, dict): |
| 214 | normalized_requests = requests_data |
| 215 | else: |
| 216 | return json.dumps( |
| 217 | { |
| 218 | "status": "error", |
| 219 | "message": "file_requests must be a JSON object or array", |
| 220 | "operation_type": "multi_file", |
| 221 | "timestamp": datetime.now().isoformat(), |
| 222 | }, |
| 223 | ensure_ascii=False, |
| 224 | indent=2, |
| 225 | ) |
| 226 | |
| 227 | # Validate input |
| 228 | if len(normalized_requests) == 0: |
| 229 | return json.dumps( |
| 230 | { |
| 231 | "status": "error", |
| 232 | "message": "No files provided for reading", |
| 233 | "operation_type": "multi_file", |
| 234 | "timestamp": datetime.now().isoformat(), |
| 235 | }, |
| 236 | ensure_ascii=False, |
| 237 | indent=2, |
nothing calls this directly
no test coverage detected