Narrow fragment spreads to their type condition in MCP tool schemas - #10329
Open
glen-84 wants to merge 1 commit into
Open
Narrow fragment spreads to their type condition in MCP tool schemas#10329glen-84 wants to merge 1 commit into
glen-84 wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes MCP tool output-schema generation for GraphQL operations that use named fragment spreads by narrowing field resolution to the fragment’s type condition (matching inline-fragment behavior) and aligning requiredness rules so fields selected only through a type-conditional fragment are not marked required.
Changes:
- Narrow the
OperationToolFactorywalker frame to a fragment spread’s type condition while visiting that fragment’s selection set. - Use a fragment spread’s type condition when computing
SelectionStateso requiredness matches inline-fragment semantics. - Add snapshot coverage for fragment spreads selecting implementation-declared fields and for spreads on the parent type; update one existing snapshot to reflect new requiredness.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/HotChocolate/Adapters/src/Adapters.Mcp.Core/OperationToolFactory.cs | Narrows walker type during fragment spread visitation and passes fragment type condition into selection-state computation. |
| src/HotChocolate/Adapters/src/Adapters.Mcp.Core/Extensions/FieldNodeExtensions.cs | Extends selection-state computation to treat fragment spreads as type-conditional when their type condition differs from the parent type. |
| src/HotChocolate/Adapters/test/Adapters.Mcp.Tests/OperationToolFactoryTests.cs | Adds tests covering spread narrowing to an implementation type and requiredness when spreading on the parent type. |
| src/HotChocolate/Adapters/test/Adapters.Mcp.Tests/snapshots/OperationToolFactoryTests.CreateTool_FragmentSpreadsSelectSameField_MergesIntoSingleProperty.json | Updates requiredness snapshot to reflect optional fields when reached only via concrete-type fragment spreads. |
| src/HotChocolate/Adapters/test/Adapters.Mcp.Tests/snapshots/OperationToolFactoryTests.CreateTool_FragmentSpreadSelectsImplementationDeclaredField_NarrowsToTypeCondition.json | New snapshot for successful schema generation when a concrete-type fragment selects an implementation-declared field. |
| src/HotChocolate/Adapters/test/Adapters.Mcp.Tests/snapshots/OperationToolFactoryTests.CreateTool_FragmentSpreadOnParentType_KeepsFieldsRequired.json | New snapshot ensuring spreads on the parent type keep fields required. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+443
to
+447
| var declaringNode = context.Nodes.ElementAtOrDefault(2); | ||
|
|
||
| NamedTypeNode? fragmentTypeCondition = null; | ||
| if (declaringNode is FragmentSpreadNode spreadNode | ||
| && context.Fragments.TryGetValue(spreadNode.Name.Value, out var fragmentNode)) |
Contributor
Patch coverage100.0% of changed lines covered (20/20)
Project coverage: 57.7% (286089/495867 lines) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
OperationToolFactoryvisited a named fragment's selection set without narrowing to the fragment's type condition, so a fragment on a concrete type selecting an implementation-declared field failed the field lookup on the abstract parent type (KeyNotFoundExceptionfromCreateTool). Fragment spreads now push a narrowed frame for the duration of the fragment, exactly like inline fragments.required.Test plan
required, aligning spread and inline-fragment semantics.Adapters.Mcp.Testssuite passes (246 tests).