Skip to content

Fix JsonSchema null collection fields causing serialization failures - #853

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

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

Conversation

@rameshreddy-adutla

Copy link
Copy Markdown
Contributor

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

Servers that omit required, properties, $defs or definitions produce
null collections, which break re-serialisation and cause NPEs when
iterated. Default them to empty immutable collections and omit empty
values on output. Fixes modelcontextprotocol#664.
@rameshreddy-adutla
rameshreddy-adutla force-pushed the fix/664-jsonschema-nullable-defaults branch from 7aa3465 to 2846e53 Compare September 28, 2026 12:37

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.

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

1 participant