CherryHQ/cherry-studio · error · Error

Invalid arguments:\n${errorDetails}

Error message

Invalid arguments:\n${errorDetails}

What it means

Generic Error thrown by the DifyKnowledgeServer search_knowledge handler when the Zod safeParse of the arguments fails. SearchKnowledgeArgsSchema requires id (string) and query (string), with optional topK (number). The error message includes a formatted JSON dump of the Zod validation errors for diagnosis. This is caught by the outer try-catch and returned as an MCP isError response (not re-thrown).

Source

Thrown at src/main/ai/mcp/servers/difyKnowledge.ts:112

            description: 'Search knowledge by id and query',
            inputSchema: z.toJSONSchema(SearchKnowledgeArgsSchema)
          }
        ]
      }
    })

    this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
      try {
        const { name, arguments: args } = request.params
        switch (name) {
          case 'list_knowledges': {
            return await this.performListKnowledges(this.config.difyKey, this.config.apiHost)
          }
          case 'search_knowledge': {
            const parsed = SearchKnowledgeArgsSchema.safeParse(args)
            if (!parsed.success) {
              const errorDetails = JSON.stringify(parsed.error.format(), null, 2)
              throw new Error(`Invalid arguments:\n${errorDetails}`)
            }
            return await this.performSearchKnowledge(
              parsed.data.id,
              parsed.data.query,
              parsed.data.topK || 6,
              this.config.difyKey,
              this.config.apiHost
            )
          }
          default:
            throw new Error(`Unknown tool: ${name}`)
        }
      } catch (error) {
        const errorMessage = error instanceof Error ? error.message : String(error)
        return {
          content: [{ type: 'text', text: `Error: ${errorMessage}` }],
          isError: true
        }

View on GitHub (pinned to 726446b54c)

Solutions

  1. Ensure both id and query are provided as non-empty strings.
  2. Pass topK as a number, not a string, if provided.
  3. Validate arguments against SearchKnowledgeArgsSchema before calling the tool.

Example fix

// before
await server.callTool('search_knowledge', { id: 'kb-123' }) // missing query

// after
const args = { id: 'kb-123', query: 'how to reset password', topK: 5 }
const parsed = SearchKnowledgeArgsSchema.safeParse(args)
if (!parsed.success) throw new Error(parsed.error.message)
await server.callTool('search_knowledge', parsed.data)
Defensive patterns

Strategy: validation

Validate before calling

import { z } from 'zod'
const SearchKnowledgeArgsSchema = z.object({
  id: z.string(),
  query: z.string(),
  topK: z.number().optional()
})
const parsed = SearchKnowledgeArgsSchema.safeParse(args)
if (!parsed.success) {
  throw new Error(`Invalid search_knowledge arguments: ${parsed.error.message}`)
}
// use parsed.data
await server.callTool('search_knowledge', parsed.data)

Type guard

function isValidSearchArgs(args: unknown): args is { id: string; query: string; topK?: number } {
  if (typeof args !== 'object' || args === null) return false
  const a = args as Record<string, unknown>
  return typeof a.id === 'string' && typeof a.query === 'string'
}

Prevention

When it happens

Trigger: Calling search_knowledge with missing id or query, wrong types (e.g., topK as a string), or extra constraints failing Zod validation. The safeParse error is stringified into the message.

Common situations: The LLM omitted the required query or id field; topK was passed as a string instead of a number; the arguments object was malformed or empty.

Related errors


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