Skip to content

fix(mcpcompat): accept a non-string top-level schema type - #328

Merged
reyortiz3 merged 4 commits into
mainfrom
fix/schema-type-array
Oct 13, 2026
Merged

reyortiz3 merged 4 commits into
mainfrom
fix/schema-type-array

Conversation

@reyortiz3

Copy link
Copy Markdown
Contributor

Summary

  • ToolArgumentsSchema.Type is a string, so a tool whose top-level schema type is a JSON Schema type array (valid JSON Schema, e.g. ["object","null"]) fails to decode with json: cannot unmarshal array into Go struct field .type of type string. A tools/list page is decoded as a whole, so that one tool makes every other tool on the page unusable. Seen downstream when vMCP connects to a remote MCP server (Skip undecodable items in Modern list responses toolhive#6772 works around it by skipping the bad item).
  • UnmarshalJSON now decodes type as raw JSON. A string still fills Type, so the exported API is unchanged. Any other value is kept verbatim in Extra["type"] and re-emitted by MarshalJSON, so the schema round-trips losslessly rather than being rejected or coerced.
  • Tool.MarshalJSON gated its outputSchema emission and its raw-vs-structured conflict checks on Type != "", which would have silently dropped a type-array output schema. They now use a hasType() helper that also recognises a type preserved in Extra.

Type of change

  • Bug fix

Test plan

  • task lint-fix (0 issues), task license-check
  • go test -race ./mcpcompat/...
  • New TestToolInputSchema_NonStringType (type array, single-element array, string type unchanged, non-string non-array preserved) and TestTool_TypeArraySchemasThroughTool (input and output type arrays survive decode and re-marshal, output schema not dropped). Both fail on main with the error above.

Notes for reviewers

  • Type stays empty for a type array, so code that checks Type == "object" will not match ["object","null"]. Consumers that marshal the schema (as toolhive does) see the array unchanged. Modelling the array on Type would be a breaking change to an exported field, so I avoided it.
  • Marshalling a type-array schema without explicit properties does not add an empty properties/required, since that defaulting only applies when Type == "object".

🤖 Generated with Claude Code

ToolArgumentsSchema.Type is a string, so a tool whose top-level schema
"type" is a JSON Schema type array (valid JSON Schema, for example
["object","null"]) failed to decode with "cannot unmarshal array into Go
struct field .type of type string". Because a tools/list page is decoded
as a whole, that one tool made the entire page, and every other tool on
it, unusable.

Decode "type" as raw JSON. A string still populates Type, so the exported
API is unchanged. Any other value is kept verbatim in Extra["type"] and
re-emitted on marshal, so the schema round-trips losslessly instead of
being rejected or coerced.

Tool.MarshalJSON gated its output-schema and raw-vs-structured conflict
checks on Type != "", which would have dropped a type-array output schema.
They now use a helper that also recognises a type preserved in Extra.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
jhrozek
jhrozek previously approved these changes Oct 12, 2026

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Narrow, lossless fix; exported API unchanged. Verified both new tests fail on main and pass here; lint/license clean.

Non-blocking:

  • LOW: no test for the RawInputSchema/RawOutputSchema conflict error when the structured schema's type lives only in Extra["type"] (tools.go:364,375).
  • INFO: type-array input schemas still trip go-sdk's AddTool (requires literal "object") when re-served via mcpcompat/server — already documented in normalizeObjectSchema; a follow-up could collapse ["object","null"] → "object" for input schemas.

@jhrozek

jhrozek commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Follow-up to my approval: one small regression I missed.

Reusing a decoded schema can emit a stale type (mcpcompat/mcp/tools.go, ToolArgumentsSchema.UnmarshalJSON)

Because RawType now shadows Alias.Type, the decoder no longer writes Type; only the string branch assigns it. Decoding a type array into a receiver that previously held "type":"object" leaves Type == "object", and MarshalJSON writes Type after Extra, so the stale string wins:

var s mcp.ToolInputSchema
_ = json.Unmarshal([]byte(`{"type":"object"}`), &s)
_ = json.Unmarshal([]byte(`{"type":["object","null"]}`), &s)
out, _ := json.Marshal(s)
// Type="object" out={"properties":{},"required":[],"type":"object"}

On main a present type key always overwrote Type, so this is new. It needs receiver reuse (e.g. decoding into an existing []Tool, where encoding/json decodes into elements in place), so low impact — the client ListTools path allocates fresh.

Suggested fix: clear it before interpreting RawType, and add a test that decodes twice into the same receiver:

tas.Type = ""

(Stale Extra/Defs on reuse is pre-existing and can be a separate cleanup.)

RawType shadows Alias.Type, so UnmarshalJSON no longer writes Type
itself; only the string branch assigns it. Decoding a type array into a
receiver that previously held "type":"object" left Type == "object", and
MarshalJSON writes Type after Extra, so the stale string won over the
preserved array. encoding/json decodes into existing elements in place,
so this is reachable when decoding into a reused []Tool.

Clear Type whenever "type" is present, matching the previous behavior
where a present key always overwrote Type. An absent key still leaves
Type untouched.

Also cover the raw-versus-structured schema conflict check when the
structured schema's type lives only in Extra["type"].

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@reyortiz3

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Both points are addressed in a6e658c.

Stale type on receiver reuse — fixed. UnmarshalJSON now clears Type whenever "type" is present, before interpreting it. I scoped it to the present-key case rather than clearing unconditionally, so an absent type still leaves Type untouched, as on main. TestToolInputSchema_TypeReplacedOnReuse decodes twice into one receiver in both directions (string → array and array → string). It fails without the fix with your exact output (type":"object" winning over the array).

Conflict-check test gap — added TestTool_RawSchemaConflictWithTypeArray, covering the RawInputSchema/RawOutputSchema conflict error when the structured schema's type lives only in Extra["type"].

Left as follow-ups, per your notes: stale Extra/Defs on reuse (pre-existing), and collapsing ["object","null"] to "object" for input schemas when re-served through mcpcompat/server/go-sdk's AddTool.

🤖 Generated with Claude Code

@reyortiz3
reyortiz3 merged commit 6771379 into main Oct 13, 2026
6 checks passed
@reyortiz3
reyortiz3 deleted the fix/schema-type-array branch October 13, 2026 16:21
LukeEvansTech added a commit to LukeEvansTech/talos-cluster that referenced this pull request Oct 13, 2026
`latest` is a floating tag. It moved to the v2 major on 2026-10-07
without a Renovate PR, so the jump was never reviewed. This pins
`v2.0.2`, the digest already running, so the rollout changes nothing;
future releases arrive as visible version bumps.

v2.0.0 to v2.0.2 change no flags, env vars or toolsets this MCPServer
sets (`GITHUB_READ_ONLY` and `GITHUB_PERSONAL_ACCESS_TOKEN` are
unchanged).

Known issue, not changed here: since v2 the server advertises output
schemas whose `type` is a JSON Schema array (`["null","object"]`) to
clients on the 2026-07-28 protocol. The ToolHive vMCP (v0.52.0) cannot
decode that, so the `github` backend of the `mcp-gateway`
VirtualMCPServer has been unavailable since the v2 rollout, and
LiteLLM's tool listing for `github` errors too. Upstream tracks the fix
in stacklok/toolhive#6814 and stacklok/toolhive-core#328. Restoring it
now would mean pinning v1.14.0, a downgrade left for a separate
decision.
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.

2 participants