1616import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
1717import com .fasterxml .jackson .annotation .JsonInclude ;
1818import com .fasterxml .jackson .annotation .JsonProperty ;
19+ import com .fasterxml .jackson .annotation .JsonSetter ;
1920import com .fasterxml .jackson .annotation .JsonSubTypes ;
2021import com .fasterxml .jackson .annotation .JsonTypeInfo ;
22+ import com .fasterxml .jackson .annotation .Nulls ;
23+ import io .modelcontextprotocol .json .JsonNull ;
2124import io .modelcontextprotocol .json .McpJsonMapper ;
2225import io .modelcontextprotocol .json .TypeRef ;
2326import io .modelcontextprotocol .util .Assert ;
@@ -3227,7 +3230,8 @@ public CallToolRequest build() {
32273230 * @param isError If true, indicates that the tool execution failed and the content
32283231 * contains error information. If false or absent, indicates successful execution.
32293232 * @param structuredContent An optional structured JSON value. Java {@code null}
3230- * indicates that the field is absent.
3233+ * indicates that the field is absent; {@link JsonNull#INSTANCE} represents explicit
3234+ * JSON {@code null}.
32313235 * @param meta See specification for notes on _meta usage
32323236 * <p>
32333237 * Note: {@code content} is required by the MCP specification. Deserialization accepts
@@ -3246,15 +3250,36 @@ public record CallToolResult( // @formatter:off
32463250 Assert .notNull (content , "content must not be null" );
32473251 }
32483252
3249- @ JsonCreator
3250- static CallToolResult fromJson (@ JsonProperty ("content" ) List <Content > content ,
3251- @ JsonProperty ("isError" ) Boolean isError , @ JsonProperty ("structuredContent" ) Object structuredContent ,
3252- @ JsonProperty ("_meta" ) Map <String , Object > meta ) {
3253+ @ JsonCreator (mode = JsonCreator .Mode .DELEGATING )
3254+ static CallToolResult fromJson (CallToolResultJson result ) {
3255+ List <Content > content = result .content ;
32533256 if (content == null ) {
32543257 logger .warn ("CallToolResult: missing required fields during deserialization: content -> []" );
32553258 content = List .of ();
32563259 }
3257- return new CallToolResult (content , isError , structuredContent , meta );
3260+ return new CallToolResult (content , result .isError , result .structuredContentValue , result .meta );
3261+ }
3262+
3263+ @ JsonIgnoreProperties (ignoreUnknown = true )
3264+ private static final class CallToolResultJson {
3265+
3266+ @ JsonProperty ("content" )
3267+ private List <Content > content ;
3268+
3269+ @ JsonProperty ("isError" )
3270+ private Boolean isError ;
3271+
3272+ @ JsonProperty ("_meta" )
3273+ private Map <String , Object > meta ;
3274+
3275+ @ JsonIgnore
3276+ private Object structuredContentValue ;
3277+
3278+ @ JsonSetter (value = "structuredContent" , nulls = Nulls .SET )
3279+ private void setStructuredContent (Object value ) {
3280+ this .structuredContentValue = value == null ? JsonNull .INSTANCE : value ;
3281+ }
3282+
32583283 }
32593284
32603285 /**
@@ -3310,22 +3335,31 @@ public Builder content(List<Content> content) {
33103335 return this ;
33113336 }
33123337
3338+ /**
3339+ * Sets the structured content value. Use {@link JsonNull#INSTANCE} for
3340+ * explicit JSON {@code null}; omit this call to leave the field absent.
3341+ * @param structuredContent the non-null structured JSON value
3342+ * @return this builder
3343+ * @throws IllegalArgumentException if structuredContent is Java {@code null}
3344+ */
33133345 public Builder structuredContent (Object structuredContent ) {
33143346 Assert .notNull (structuredContent , "structuredContent must not be null" );
33153347 this .structuredContent = structuredContent ;
33163348 return this ;
33173349 }
33183350
33193351 /**
3320- * Sets the structured content by parsing a non-null JSON value.
3352+ * Sets the structured content by parsing a JSON value, including JSON
3353+ * {@code null}, which is represented by {@link JsonNull#INSTANCE}.
33213354 * @param jsonMapper the mapper used to parse the value
3322- * @param structuredContent the JSON-encoded non-null structured content
3355+ * @param structuredContent the JSON-encoded structured content
33233356 * @return this builder
33243357 */
33253358 public Builder structuredContent (McpJsonMapper jsonMapper , String structuredContent ) {
33263359 Assert .hasText (structuredContent , "structuredContent must not be empty" );
33273360 try {
3328- this .structuredContent (jsonMapper .readValue (structuredContent , Object .class ));
3361+ Object value = jsonMapper .readValue (structuredContent , Object .class );
3362+ this .structuredContent (value == null ? JsonNull .INSTANCE : value );
33293363 }
33303364 catch (IOException e ) {
33313365 throw new IllegalArgumentException ("Invalid structured content: " + structuredContent , e );
0 commit comments