Repository navigation
fix(mcpcompat): accept a non-string top-level schema type - #328
Conversation
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
left a comment
There was a problem hiding this comment.
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 innormalizeObjectSchema; a follow-up could collapse["object","null"]→"object"for input schemas.
|
Follow-up to my approval: one small regression I missed. Reusing a decoded schema can emit a stale Because 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 Suggested fix: clear it before interpreting tas.Type = ""(Stale |
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]>
|
Thanks for the review. Both points are addressed in a6e658c. Stale Conflict-check test gap — added Left as follow-ups, per your notes: stale 🤖 Generated with Claude Code |
`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.
Summary
ToolArgumentsSchema.Typeis astring, so a tool whose top-level schematypeis a JSON Schema type array (valid JSON Schema, e.g.["object","null"]) fails to decode withjson: cannot unmarshal array into Go struct field .type of type string. Atools/listpage 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).UnmarshalJSONnow decodestypeas raw JSON. A string still fillsType, so the exported API is unchanged. Any other value is kept verbatim inExtra["type"]and re-emitted byMarshalJSON, so the schema round-trips losslessly rather than being rejected or coerced.Tool.MarshalJSONgated itsoutputSchemaemission and its raw-vs-structured conflict checks onType != "", which would have silently dropped a type-array output schema. They now use ahasType()helper that also recognises a type preserved inExtra.Type of change
Test plan
task lint-fix(0 issues),task license-checkgo test -race ./mcpcompat/...TestToolInputSchema_NonStringType(type array, single-element array, string type unchanged, non-string non-array preserved) andTestTool_TypeArraySchemasThroughTool(input and output type arrays survive decode and re-marshal, output schema not dropped). Both fail onmainwith the error above.Notes for reviewers
Typestays empty for a type array, so code that checksType == "object"will not match["object","null"]. Consumers that marshal the schema (as toolhive does) see the array unchanged. Modelling the array onTypewould be a breaking change to an exported field, so I avoided it.propertiesdoes not add an emptyproperties/required, since that defaulting only applies whenType == "object".🤖 Generated with Claude Code