fix: flatten top-level anyOf/oneOf/allOf in MCP tool schemas #10726
+218
−15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
MCP servers can define tool schemas with
anyOf,oneOf, orallOfat the top level. When these schemas are passed to OpenRouter with Anthropic models, the API rejects them with:PostHog Issue: https://us.posthog.com/error_tracking/019bb5ab-be91-7160-8060-06f185ee5fcb
Solution
Extended
normalizeToolSchema()insrc/utils/json-schema.tsto flatten top-level schema composition keywords before sending to providers.Changes:
Added
flattenTopLevelComposition()function that:anyOf,oneOf, orallOfat the top leveldescription,$schema){ type: "object", additionalProperties: false }if no object variant existsUpdated tests in
src/utils/__tests__/json-schema.spec.tsImpact
The model sees a slightly simplified schema (just the object type, not "object OR null"). In practice, most MCP tools expect object inputs anyway, so the model's generated calls remain valid. The MCP server performs its own validation regardless of what schema we send to the LLM.
Testing
All 5242 tests pass.
Closes COM-485
Important
Flatten top-level
anyOf,oneOf,allOfin MCP tool schemas for provider compatibility innormalizeToolSchema().normalizeToolSchema()injson-schema.tsnow flattens top-levelanyOf,oneOf,allOfto a single object schema usingflattenTopLevelComposition().{ type: "object", additionalProperties: false }if no object variant exists.json-schema.spec.tsto verify flattening of top-level schema composition keywords.anyOfremains unchanged.json-schema.tsto reflect new functionality.This description was created by
for 6476e00. You can customize this summary. It will automatically update as commits are pushed.