Skip to content

MCP: complex object parameters (filter, sort) fail with clients that serialize them as JSON strings #5610

@bobey

Description

@bobey

Problem

MCP clients like Claude Code serialize complex object parameters as JSON strings rather than actual JavaScript objects. The Zod validation in the MCP server rejects these because it expects objects, not strings.

For example, calling query_requests with a filter like:

{"request_response_rmt": {"request_id": {"equals": "some-uuid"}}}

fails with a Zod invalid_type error (expected: object, received: string) because the client sends the value as a JSON string rather than a parsed object.

The "all" literal filter works fine since it's already a string.

Suggested fix

Use z.preprocess to transparently parse JSON strings before Zod validation, keeping the existing schema validation intact:

const jsonPreprocess = (schema) => z.preprocess(
  (val) => {
    if (typeof val === "string" && val !== "all") {
      try { return JSON.parse(val); } catch { return val; }
    }
    return val;
  },
  schema
);

Then in index.ts:

filter: jsonPreprocess(requestFilterNodeSchema).optional(),
sort: jsonPreprocess(sortLeafRequestSchema).optional(),

This would make the MCP server resilient to both native objects and JSON-serialized strings, without losing any input validation.

Affected parameters

  • filter on query_requests
  • sort on query_requests
  • filter on query_sessions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions