Skip to content

Fix JsonSchema null collection fields causing serialization failures#853

Open
rameshreddy-adutla wants to merge 1 commit intomodelcontextprotocol:mainfrom
rameshreddy-adutla:fix/664-jsonschema-nullable-defaults
Open

Fix JsonSchema null collection fields causing serialization failures#853
rameshreddy-adutla wants to merge 1 commit intomodelcontextprotocol:mainfrom
rameshreddy-adutla:fix/664-jsonschema-nullable-defaults

Conversation

@rameshreddy-adutla
Copy link

Summary

Fixes #664

When an MCP server (e.g. Python/fastmcp) omits optional schema fields like required, $defs, and definitions, Jackson deserializes them as null. This causes:

  • IllegalArgumentException when re-serializing with a standard ObjectMapper
  • NullPointerException when iterating over these collections

Changes

McpSchema.JsonSchema (mcp-core)

  • Compact constructor: Defaults null collection fields (required, properties, defs, definitions) to empty immutable collections (List.of(), Map.of())
  • @JsonInclude(NON_EMPTY) on collection fields: Ensures empty defaults are omitted during serialization, preserving wire format compatibility

Tests (mcp-test)

  • testJsonSchemaWithMissingOptionalFields: Verifies deserialization of a minimal schema (no required/$defs/definitions) produces non-null collections and round-trips correctly
  • testJsonSchemaConstructorDefaultsForNullCollections: Verifies direct constructor with null args produces empty collections

Before

JsonSchema schema = mapper.readValue("{\"type\":\"object\"}", JsonSchema.class);
schema.required()  // → null
new ObjectMapper().valueToTree(schema)  // → IllegalArgumentException

After

JsonSchema schema = mapper.readValue("{\"type\":\"object\"}", JsonSchema.class);
schema.required()  // → [] (empty list, never null)
new ObjectMapper().valueToTree(schema)  // → works correctly

When an MCP server (e.g. Python/fastmcp) omits optional schema fields
like required, $defs, and definitions, Jackson deserializes them as
null. This causes failures when callers re-serialize the JsonSchema
with a standard ObjectMapper or iterate over these collections.

Add a compact constructor that defaults null collection fields to
empty immutable collections (List.of(), Map.of()). Also add
@JsonInclude(NON_EMPTY) on collection fields so that empty defaults
are omitted during serialization, preserving wire format compatibility.

Fixes modelcontextprotocol#664

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

Null fields in JsonSchema without @Nullable annotation break serialization with Jackson

1 participant