Skip to content

fix(mcp-clients): support JSON Schema union type arrays in the validation hint - #6248

Open
pedrofrxncx wants to merge 1 commit into
mainfrom
fix/mcp-validation-hint-union-types-w1
Open

fix(mcp-clients): support JSON Schema union type arrays in the validation hint#6248
pedrofrxncx wants to merge 1 commit into
mainfrom
fix/mcp-validation-hint-union-types-w1

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

Source: bug found auditing apps/api/src/mcp-clients/validation-hint.ts after this week's coerce/retry hardening (#6210, #6216, #6242).

Why a maintainer wants it: JSON Schema lets a property's type be a union array (e.g. a nullable field as ["string", "null"]"), and the MCP SDK's Tool.inputSchema.properties values are untyped custom objects, so nothing stops an upstream server from advertising one. matchesDeclaredType and coerceArgsToSchema only ever compared against a single type string, so any correctly-typed value against a union-typed property was always reported as "Wrong type" in the repair hint sent back to the agent — actively misleading it about what's actually wrong — and coerceArgsToSchema could never recognize a string value as already valid for such a property.

Failure scenario: a tool declares note: { type: ["string", "null"] }. The agent sends a correct string value but the call still fails validation for an unrelated reason (e.g. a missing required field). The generated hint falsely says note (expected string,null, got string) even though note was fine, confusing the retry. Regression test: validation-hint.test.ts — "does not flag a nullable union type's matching member as wrong-typed" / "flags a value matching none of a union type's members" / "coerces against a union type, skipping when string is already a member".

Verify: bun test apps/api/src/mcp-clients/validation-hint.test.ts (19 pass).

Checks run locally: bun run fmt, cd apps/api && bunx tsc --noEmit (clean), bunx oxlint apps/api/src/mcp-clients/validation-hint.ts apps/api/src/mcp-clients/validation-hint.test.ts (0 warnings/errors), targeted test file above. Full CI validates the rest.


Summary by cubic

Fixes validation and repair hints for tools that declare JSON Schema union types (e.g., ["string", "null"]). Previously, union-typed properties were always flagged as wrong-typed; now we correctly match and coerce across union members.

  • Accepts type as string | string[] and normalizes to an array.
  • matchesDeclaredType matches any union member (keeps special handling for "integer").
  • coerceArgsToSchema tries coercion across union members and skips coercion when "string" is already allowed.
  • buildValidationHint formats expected union types as a|b and no longer flags values that match a union member.
  • Adds targeted tests; no behavior change for single-type schemas.

Written for commit 1c2adfb. Summary will update on new commits.

Review in cubic

…tion hint

MCP tool input schemas can declare a property's type as a union array
(e.g. a nullable field as ["string", "null"]) — valid JSON Schema, and
the SDK's inputSchema property values are untyped, so nothing stops an
upstream tool from sending one. matchesDeclaredType/coerceArgsToSchema
only handled a single type string, so a correctly-typed value against a
union schema was always flagged as a type mismatch in the repair hint,
and coerceArgsToSchema would never recognize the value as already valid.
@pedrofrxncx
pedrofrxncx enabled auto-merge (squash) August 19, 2026 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant