{"record":{"id":"2f2726c8d20d4077","repo":"CherryHQ/cherry-studio","slug":"invalid-arguments-for-edit-parsed-error","errorCode":null,"errorMessage":"Invalid arguments for edit: ${parsed.error}","messagePattern":"Invalid arguments for edit: (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"src/main/ai/mcp/servers/filesystem/tools/edit.ts","lineNumber":36,"sourceCode":"  description: `Performs exact string replacements in files.\n\n- You must use the 'read' tool at least once before editing\n- The file_path must resolve within the configured workspace root\n- Preserve exact indentation from read output (after the line number prefix)\n- Never include line number prefixes in old_string or new_string\n- ALWAYS prefer editing existing files over creating new ones\n- The edit will FAIL if old_string is not found in the file\n- The edit will FAIL if old_string appears multiple times (provide more context or use replace_all)\n- The edit will FAIL if old_string equals new_string\n- Use replace_all to rename variables or replace all occurrences`,\n  inputSchema: z.toJSONSchema(EditToolSchema)\n}\n\n// Handler implementation\nexport async function handleEditTool(args: unknown, baseDir: string) {\n  const parsed = EditToolSchema.safeParse(args)\n  if (!parsed.success) {\n    throw new Error(`Invalid arguments for edit: ${parsed.error}`)\n  }\n\n  const { file_path: filePath, old_string: oldString, new_string: newString, replace_all: replaceAll } = parsed.data\n\n  // Validate path\n  const validPath = await validatePath(filePath, baseDir)\n\n  // Check if file exists\n  try {\n    const stats = await fs.stat(validPath)\n    if (!stats.isFile()) {\n      throw new Error(`Path is not a file: ${filePath}`)\n    }\n  } catch (error: any) {\n    if (error.code === 'ENOENT') {\n      // If old_string is empty, this is a create new file operation\n      if (oldString === '') {\n        // Create parent directory if needed","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/CherryHQ/cherry-studio/blob/726446b54cd69ffe51a276638672f6d95ca0768c/src/main/ai/mcp/servers/filesystem/tools/edit.ts#L18-L54","documentation":"Zod safeParse on the edit tool's arguments failed. EditToolSchema requires file_path, old_string, new_string as strings and accepts an optional replace_all boolean (default false). parsed.error is a ZodError enumerating each failing field. Returned to the MCP client as an isError tool result via the server-level catch.","triggerScenarios":"The arguments object is missing any of the three required strings, has a non-string value, or is not a parseable object. A common case is the model sending old_string and new_string but forgetting file_path.","commonSituations":"A model attempting a 'create file' edit by sending only new_string (the empty-old_string create path still requires the field to be present); schema drift between client and server; a client sending arguments as a JSON string instead of a parsed object.","solutions":["Provide all three required fields as strings: file_path, old_string, new_string.","For file creation, send old_string as the empty string '' explicitly rather than omitting it.","Read parsed.error in the returned message — it identifies the missing or mistyped field."],"exampleFix":"// before\nif (!parsed.success) {\n  throw new Error(`Invalid arguments for edit: ${parsed.error}`)\n}\n\n// after — name the failing fields explicitly\nif (!parsed.success) {\n  const issues = parsed.error.issues.map(i => `${i.path.join('.') || '(root)'}: ${i.message}`).join('; ')\n  throw new Error(`Invalid arguments for edit: ${issues}`)\n}","handlingStrategy":"validation","validationCode":"// Validate edit args client-side before dispatching.\nfunction isValidEditArgs(a: unknown): a is { file_path: string; old_string: string; new_string: string; replace_all?: boolean } {\n  if (typeof a !== 'object' || a === null) return false\n  const o = a as any\n  return typeof o.file_path === 'string' && typeof o.old_string === 'string' && typeof o.new_string === 'string'\n    && (o.replace_all === undefined || typeof o.replace_all === 'boolean')\n}","typeGuard":"function isEditArgs(a: unknown): a is { file_path: string; old_string: string; new_string: string } {\n  return typeof a === 'object' && a !== null && typeof (a as any).file_path === 'string'\n    && typeof (a as any).old_string === 'string' && typeof (a as any).new_string === 'string'\n}","tryCatchPattern":null,"preventionTips":["Always include file_path, old_string, and new_string as strings.","For file creation, send old_string:'' explicitly rather than omitting it.","Generate client types from z.toJSONSchema(EditToolSchema).","Run safeParse on the client to catch missing fields before the round trip."],"tags":["zod","validation","edit","mcp-tool","filesystem"],"backgroundTag":null,"analyzedSha":"726446b54cd69ffe51a276638672f6d95ca0768c","analyzedAt":"2026-08-12T17:30:37.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}