Skip to content

fix(mcp): keep the fields of inline object schemas - #340

Open
mmporong wants to merge 1 commit into
Eigenwise:mainfrom
mmporong:fix-mcp-inline-object-schema
Open

mmporong wants to merge 1 commit into
Eigenwise:mainfrom
mmporong:fix-mcp-inline-object-schema

Conversation

@mmporong

@mmporong mmporong commented Nov 2, 2026

Copy link
Copy Markdown

SchemaTransformer.json_to_pydantic_field maps every "type": "object" schema to Dict[str, ...] and only looks at additionalProperties. An inline nested object therefore loses its properties and required: the tool schema sent to the model has no nested field names or enums, and nested values are not validated before they reach the MCP server. The same object referenced through $ref already becomes a nested model via _create_nested_model.

This builds a nested model for inline object schemas that declare properties, the same way the $ref path does (the model name is the schema title, or NestedObject). Objects without properties keep the existing Dict mapping, so test_object_type is unchanged. This also covers inline objects used as array items.

Tests: added test_inline_object_properties_are_validated (inline object with an enum and a required field, and an array of inline objects). It fails before the change and passes after it; tests/connectors/mcp passes (410 passed). black --check and flake8 pass on the changed files.

@Eigenwise Eigenwise left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The bug is real: an inline object becomes Dict[str, ...] and loses its properties and required. The trouble with the fix is that the nested model drops every key the schema doesn't declare.

I ran it against a live server. Sending {"mode": "x", "extra": 5} to a tool whose inline object only declares mode, the server receives extra on main and doesn't with this PR. It's the same on the way back: a server-only key in structuredContent gets stripped.

Could you:

  • Build the nested model's extra setting from additionalProperties. Absent, true or {} allows extras. false forbids them, and the emitted schema keeps additionalProperties: false. A schema value either validates the extras against it or keeps today's Dict mapping.
  • Put that in one helper used by both the inline path and _resolve_ref, with the same condition in both places. The $ref path drops extras today too, so this fixes both.
  • Add tests for absent / true / false / schema-valued additionalProperties with an undeclared key, plus a structured-output round trip that keeps a server-only key.
  • Drop the comment that restates the code.

#357 just landed in the same function, so this will need a rebase.

@Eigenwise Eigenwise left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The extras handling and the shared helper for inline and referenced objects look right. One regression blocks it, though: nested property names now go straight into create_model, so a name Pydantic won't accept as a field fails the whole tool.

schema = {
    "type": "object",
    "required": ["filter"],
    "properties": {
        "filter": {"type": "object", "required": ["_id"], "properties": {"_id": {"type": "integer"}}}
    },
}
SchemaTransformer.create_model_from_schema(schema, "Probe", "probe")

On main this builds, and filter={"_id": 5} round-trips as a dict. On this branch it raises NameError: Fields must not use names with leading underscores, and the factory then skips the tool. _id is common in MongoDB-style tools, so this would drop tools that work today.

Please keep an inline object as a plain dict (or alias the field) when one of its property names can't be a Pydantic field, and add a test for the _id case. A top-level _id already fails on main; that's a separate problem and not something to fix here.

It also needs a rebase now: #335 changed _resolve_ref and the two conflict in schema_transformer.py. Please keep #335's full-pointer lookup when you resolve it. #363 is in as well, so constraints inside an inline object with declared properties should still apply after the rebase; a test for that would be welcome.

Honor additionalProperties for inline and referenced objects. Keep reserved
Pydantic property names as dictionaries and cover input/output round trips.
Preserve full JSON pointer resolution and nested scalar/array constraints.
@mmporong

Copy link
Copy Markdown
Author

Before I push the rebase onto current main, two of the requested behaviors now conflict with what #389 merged, so I'd like to check which way you want them:

  1. For a nested object with additionalProperties: false, should fix(mcp): keep the fields of inline object schemas #340 accept the input and drop undeclared keys, as Preserve recursive MCP schemas and explicit object data #389 now tests? The first review asked for false to forbid them, and the Preserve recursive MCP schemas and explicit object data #389 approval notes that false no longer turns into a hard reject. For names such as _id, the rebased patch uses the alias path from the follow-up review. First review / Follow-up review / #389 approval / Merged test

  2. When the keyword is omitted, should inline objects keep undeclared keys in tool arguments and structured output, while $ref objects keep dropping them as they do today? That preserves the current behavior on each path. The first review asked for extras to be kept on both.

I'll update the branch once the direction is settled.

This branch has not been deployed

No deployments
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