{"record":{"id":"7beec02fa4fd8970","repo":"CherryHQ/cherry-studio","slug":"invalid-arguments-for-write-parsed-error","errorCode":null,"errorMessage":"Invalid arguments for write: ${parsed.error}","messagePattern":"Invalid arguments for write: (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/main/ai/mcp/servers/filesystem/tools/write.ts","lineNumber":31,"sourceCode":"// Tool definition with detailed description\nexport const writeToolDefinition = {\n  name: 'write',\n  description: `Writes a file to the local filesystem.\n\n- This tool will overwrite the existing file if one exists at the path\n- You MUST use the read tool first to understand what you're overwriting\n- ALWAYS prefer using the 'edit' tool for existing files\n- NEVER proactively create documentation files unless explicitly requested\n- Parent directories will be created automatically if they don't exist\n- The file_path must resolve within the configured workspace root`,\n  inputSchema: z.toJSONSchema(WriteToolSchema)\n}\n\n// Handler implementation\nexport async function handleWriteTool(args: unknown, baseDir: string) {\n  const parsed = WriteToolSchema.safeParse(args)\n  if (!parsed.success) {\n    throw new Error(`Invalid arguments for write: ${parsed.error}`)\n  }\n\n  const filePath = parsed.data.file_path\n  const validPath = await validatePath(filePath, baseDir)\n\n  // Create parent directory if it doesn't exist\n  const parentDir = path.dirname(validPath)\n  try {\n    await fs.mkdir(parentDir, { recursive: true })\n  } catch (error: any) {\n    if (error.code !== 'EEXIST') {\n      throw new Error(`Failed to create parent directory: ${error.message}`)\n    }\n  }\n\n  // Check if file exists (for logging)\n  let isOverwrite = false\n  try {","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/CherryHQ/cherry-studio/blob/726446b54cd69ffe51a276638672f6d95ca0768c/src/main/ai/mcp/servers/filesystem/tools/write.ts#L13-L49","documentation":"Thrown by the write tool when WriteToolSchema.safeParse(args) fails. The schema (write.ts:8) requires file_path: string and content: string. Zod's default object mode does not reject unknown keys, so the failure is specifically about file_path or content being absent, undefined, or not a string. The full Zod issue tree is appended via parsed.error.","triggerScenarios":"Calling handleWriteTool with missing file_path or content; passing content as a non-string (number, object, Buffer); passing an object whose keys are nested differently than {file_path, content}; args is undefined/null is caught earlier only if Zod receives it (safeParse handles it and reports type errors).","commonSituations":"Caller uses a camelCase key (filePath) instead of file_path; content omitted because the caller assumed an overwrite-from-empty default; args built dynamically where a field ended up undefined after a conditional; SDK/client that strips falsy strings sent content as '' which is actually valid (empty string passes).","solutions":["Ensure args is exactly { file_path: string, content: string } with both fields present and string-typed.","Use the snake_case key names file_path and content as defined in WriteToolSchema, not camelCase aliases.","Run WriteToolSchema.safeParse(args) on the caller side first and surface parsed.error before sending."],"exampleFix":"// before\nawait handleWriteTool({ filePath: 'a.txt', content: 'hi' }, baseDir) // throws: Invalid arguments (file_path missing)\n\n// after\nawait handleWriteTool({ file_path: 'a.txt', content: 'hi' }, baseDir)","handlingStrategy":"validation","validationCode":"import { WriteToolSchema } from './write'\nconst parsed = WriteToolSchema.safeParse(args)\nif (!parsed.success) {\n  // report parsed.error to the user before calling handleWriteTool\n}","typeGuard":"function isWriteArgs(a: unknown): a is { file_path: string; content: string } {\n  return typeof a === 'object' && a !== null &&\n    typeof (a as any).file_path === 'string' &&\n    typeof (a as any).content === 'string'\n}","tryCatchPattern":"try {\n  await handleWriteTool(args, baseDir)\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Invalid arguments for write')) {\n    // fix args shape using the Zod error details\n  } else throw e\n}","preventionTips":["Always use the snake_case keys file_path and content exactly as declared in WriteToolSchema.","Run the shared Zod schema on the caller side first to get structured errors.","Treat empty-string content as valid only if you intentionally want to truncate the file."],"tags":["filesystem","validation","zod","mcp","write-tool"],"backgroundTag":null,"analyzedSha":"726446b54cd69ffe51a276638672f6d95ca0768c","analyzedAt":"2026-08-12T17:30:37.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}