CherryHQ/cherry-studio · warning · Error

Invalid arguments for grep: ${parsed.error}

Error message

Invalid arguments for grep: ${parsed.error}

What it means

Zod safeParse on the grep tool's arguments failed. GrepToolSchema requires a string `pattern` and accepts optional `path` and `include` strings. parsed.error lists the failing fields. Returned as an isError tool result by the server-level catch.

Source

Thrown at src/main/ai/mcp/servers/filesystem/tools/grep.ts:39

  description: `Fast content search tool that works with any codebase size.

- Searches file contents using regular expressions
- Supports full regex syntax (e.g., "log.*Error", "function\\s+\\w+")
- Filter files by pattern with include (e.g., "*.js", "*.{ts,tsx}")
- Returns absolute file paths and line numbers with matching content
- Results are limited to 100 matches
- Binary files are automatically skipped
- Common directories (node_modules, .git, dist) are excluded
- The path parameter must resolve within the configured workspace root if specified
- If path is not specified, defaults to the base directory`,
  inputSchema: z.toJSONSchema(GrepToolSchema)
}

// Handler implementation
export async function handleGrepTool(args: unknown, baseDir: string) {
  const parsed = GrepToolSchema.safeParse(args)
  if (!parsed.success) {
    throw new Error(`Invalid arguments for grep: ${parsed.error}`)
  }

  const data = parsed.data

  if (!data.pattern) {
    throw new Error('Pattern is required for grep')
  }

  const searchPath = data.path || baseDir
  const validPath = await validatePath(searchPath, baseDir)

  const matches: GrepMatch[] = []
  let truncated = false
  let regex: RegExp

  // Build ripgrep arguments
  const rgArgs: string[] = [
    '--no-heading',

View on GitHub (pinned to 726446b54c)

Solutions

  1. Provide pattern as a non-empty regex string (e.g. 'log.*Error').
  2. If using include, send it as a comma-separated file glob string (e.g. '*.ts,*.tsx').
  3. Read parsed.error to find the failing field.

Example fix

// before
throw new Error(`Invalid arguments for grep: ${parsed.error}`)

// after — structured issues
if (!parsed.success) {
  const issues = parsed.error.issues.map(i => `${i.path.join('.') || '(root)'}: ${i.message}`).join('; ')
  throw new Error(`Invalid arguments for grep: ${issues}`)
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate grep args on the client.
function isValidGrepArgs(a: unknown): a is { pattern: string; path?: string; include?: string } {
  if (typeof a !== 'object' || a === null) return false
  const o = a as any
  return typeof o.pattern === 'string'
    && (o.path === undefined || typeof o.path === 'string')
    && (o.include === undefined || typeof o.include === 'string')
}

Type guard

function isGrepArgs(a: unknown): a is { pattern: string; path?: string; include?: string } {
  return typeof a === 'object' && a !== null && typeof (a as any).pattern === 'string'
}

Prevention

When it happens

Trigger: The arguments object omits pattern, sends pattern as a non-string, or sends include/path as a non-string. An empty-string pattern passes zod but is caught later at error 316.

Common situations: A model calling grep with only include and no pattern; a client sending pattern:null; confusion between the path field and the pattern field.

Related errors


AI-assisted analysis of CherryHQ/cherry-studio@726446b54c (2026-08-12). Data as JSON: /api/errors/4abf3933d9a3d5f7. Report an issue: GitHub.