Repository navigation
fix(mcp): keep oneOf exclusivity when converting schemas - #399
SajjadPSavoji wants to merge 6 commits into
Conversation
Eigenwise
left a comment
There was a problem hiding this comment.
Thanks for picking up #368. The exclusivity check does what the issue asks for the cases in the tests: 1 against integer | number is rejected and 1.5 passes. Three things before it can merge.
1. Branch patterns run through Python re. The branch check validates each original branch with jsonschema, which evaluates pattern with re.search. MCP schemas come from the server, and #363 (merged now) moved pattern handling off that path for this reason.
{"oneOf": [{"type": "string", "pattern": "^(a+)+$"}, {"type": "integer"}]}with the value"a" * 27 + "!"takes 2.7 s here, and the time doubles with every extra character. On main the same value returns instantly.- With
"pattern": "^\\p{L}+$"in a branch, validating"abc"raisesre.PatternError: bad escape \p, which is not aValidationError, so it escapes the model.
Please don't evaluate pattern with re in the branch check. After rebasing on #363 you can reuse its pattern handling, including the fallback for patterns the engine doesn't support.
2. Duplicate branches disagree with the advertised schema. {"oneOf": [{"type": "integer"}, {"type": "integer"}]} advertises plain {"type": "integer"}, because the Union collapses and there is no anyOf left to rename. Validation then rejects every integer with "matches 2 oneOf branches". The schema sent to the model and the validator should agree, whichever way you decide it.
3. Rebase. #335 and #363 both changed schema_transformer.py and are on main now.
|
branch patterns now go through the same Pydantic regex adapter as #363, and unsupported ones are skipped like there. For duplicates I kept JSON Schema semantics, so repeated branches stay separate in the |
|
Two follow-ups on Nullable from atomic_agents.connectors.mcp import SchemaTransformer
schema = {
"type": "object",
"properties": {
"value": {"oneOf": [{"type": "null"}, {"type": "string"}]}
},
"required": ["value"],
}
model = SchemaTransformer.create_model_from_schema(schema, "NullableInput", "nullable")
model.model_json_schema()Pydantic optimizes this to a The regex review has another path: overriding |
|
the nullable |
|
Thanks, I independently rechecked current head The MCP test directory passed locally: 721 tests, Python 3.13.5 / Pydantic 2.14.3. This closes the two findings from my earlier comment, within these checks. AI-assisted verification, not a full-suite result or human approval; no hosted model or MCP server was contacted. |
Eigenwise
left a comment
There was a problem hiding this comment.
Thanks for sticking with this through several rounds. I owe you a straight answer on where it stands, and part of the problem is my own earlier guidance.
The goal is right. A tool's schema is its contract, and on main a oneOf of two object shapes accepts an object that matches neither. That should be rejected locally, before the tool is called.
What I'm not happy with is how the check is built, and that's on me: when I asked for regex safety on the jsonschema path in an earlier round, I pushed this further into running a second validator next to Pydantic. Concretely, with this branch and a property like
{"oneOf": [
{"type": "object", "properties": {"kind": {"const": "file"}, "path": {"type": "string"}}, "required": ["kind", "path"]},
{"type": "object", "properties": {"kind": {"const": "url"}, "url": {"type": "string"}}, "required": ["kind", "url"]}
]}- the schema the model is shown is
{"oneOf": [{"additionalProperties": true, "type": "object"}, {"additionalProperties": true, "type": "object"}]}, so it can't see either variant's fields; - a wrong value is rejected with "Value matches 0 oneOf branches", from a
jsonschemavalidator inside a closure, which doesn't tell the model (or someone with a debugger) what was missing; _OneOfJsonSchemawalks Pydantic's core schema ("choices",generate_inner) to printoneOf, which ties us to internals.
On #340's branch, without this change, the same schema becomes a union of two real nested models. The model is shown both variants with their fields, and the wrong value is rejected by Pydantic itself with an ordinary missing-field error. That is how this project wants contracts expressed: in the models, with nothing hidden.
So here's what I'd like, and I'm happy to do the integration work with you:
- #340 goes in first (I'm finishing it; its author has gone quiet). After that, object variants are covered without any of this.
- This PR then only needs what a union of models can't express: a value that matches more than one branch, like
1against[integer, number]. Please count matches with the branch types that are already converted, instead of a secondjsonschemapass. If that turns out not to work for some case, say which one and we'll look at it together. - Drop
_OneOfJsonSchema, and with thejsonschemapass gone, the custompattern,patternPropertiesandadditionalPropertieskeywords go too. _pattern_adapteris worth keeping on its own: the existing enum check at model construction still sends server patterns to Python'sre, and it should use your helper. That can be a separate small PR if you prefer.
This branch also conflicts with main now (#389 landed in the same file). No need to rebase until #340 is in; I'll ping you here.
|
makes sense. I'll leave the branch as it is until #340 is in, then count matches with the converted branch types and drop the jsonschema pass and |
oneOfwas converted to a plain PydanticUnion, so a value matching more than one branch (like1againstintegerandnumber) was accepted by the model and by the schema sent to the model. Nowjson_to_pydantic_fieldsends a multi-branchoneOfthrough a newSchemaTransformer._one_of_typehelper. It keeps theUnionand adds aBeforeValidatorthat counts how many original branches the JSON form of the value matches and rejects anything but exactly one. It uses the same$ref-tolerant validator as the enum/const path. A small annotation also emitsoneOfinstead ofanyOfin the model JSON schema.anyOfis untouched, and since this is the shared converter it covers array items,$reftargets, optional fields and both MCPFactory input and output schemas.I added
tests/connectors/mcp/test_oneof_exclusivity.py, which compares the generated model and its emitted schema againstDraft202012Validatorfor the cases in the issue plus optional,anyOfand$refcases. I also updated two assertions intest_schema_transformer.pyfor the newAnnotatedtype. The new file fails before the change and passes after, and the wholeatomic-agents/tests/connectors/mcpfolder passes. I didn't run the full suite.Fixes #368