fix: support valid JSON value in tool structured content output #551
+283
−39
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.
This allows tools to return arrays as structured content, not just objects, which is required by the MCP specification for tools with array-type output schemas.
It is related to MPC spec fix: modelcontextprotocol/modelcontextprotocol#834
Fixes #550
This PR extends the MCP Java SDK to support arrays as structured content output from tools, not just objects. The main change is updating
CallToolResult.structuredContent
fromMap<String,Object>
toObject
to accommodate both object and array return types.Motivation and Context
This change addresses issue #550. Previously, the SDK only supported tools that returned objects as structured content, but the MCP specification allows tools to define output schemas that return arrays. This limitation prevented developers from creating tools that return lists of items (e.g., a tool that returns a list of users, search results, or any collection of structured data).
Without this change, tools with array-type output schemas would fail validation or cause runtime errors when trying to return array data.
How Has This Been Tested?
testStructuredOutputOfObjectArrayValidationSuccess
in multiple test classes to verify array supportBreaking Changes
Yes, this is a breaking change. Users will need to update their code when:
Accessing
CallToolResult.structuredContent()
: Add explicit casting since it now returnsObject
instead ofMap<String,Object>
Creating
CallToolResult
instances: The constructors are deprecated, use the builder patternImplementing custom
JsonSchemaValidator
: Update the validate method signatureTypes of changes
Checklist
Additional context
Design Decisions:
structuredContent
toObject
type to support both Maps and Lists, aligning with JSON's two container typesCallToolResult
to provide a cleaner API and better forward compatibility@Deprecated
)Migration Path:
The deprecated constructors are retained for backward compatibility but should be removed in a future major version. Users are encouraged to migrate to the builder pattern immediately.
Related MCP Specification:
This change aligns with the MCP Tools Specification which states that structured content can be any valid JSON value, including arrays.
modelcontextprotocol/modelcontextprotocol#834