@@ -378,13 +378,13 @@ static JsonObjectBuilder toJson(McpContent content) {
378378 }
379379
380380 static JsonObjectBuilder toJson (McpSamplingMessage message ) {
381- if (message instanceof McpSamplingTextContentImpl text ) {
381+ if (message instanceof McpSamplingTextMessageImpl text ) {
382382 return toJson (text );
383383 }
384- if (message instanceof McpSamplingImageContentImpl image ) {
384+ if (message instanceof McpSamplingImageMessageImpl image ) {
385385 return toJson (image );
386386 }
387- if (message instanceof McpSamplingAudioContentImpl resource ) {
387+ if (message instanceof McpSamplingAudioMessageImpl resource ) {
388388 return toJson (resource );
389389 }
390390 throw new IllegalArgumentException ("Unsupported content type: " + message .getClass ().getName ());
@@ -427,7 +427,7 @@ static JsonObjectBuilder toJson(McpPromptAudioContent audio) {
427427 .add ("content" , toJson (audio .content ()));
428428 }
429429
430- static JsonObjectBuilder toJson (McpSamplingImageContent image ) {
430+ static JsonObjectBuilder toJson (McpSamplingImageMessage image ) {
431431 return JSON_BUILDER_FACTORY .createObjectBuilder ()
432432 .add ("role" , image .role ().text ())
433433 .add ("content" , JSON_BUILDER_FACTORY .createObjectBuilder ()
@@ -436,15 +436,15 @@ static JsonObjectBuilder toJson(McpSamplingImageContent image) {
436436 .add ("mimeType" , image .mediaType ().text ()));
437437 }
438438
439- static JsonObjectBuilder toJson (McpSamplingTextContent text ) {
439+ static JsonObjectBuilder toJson (McpSamplingTextMessage text ) {
440440 return JSON_BUILDER_FACTORY .createObjectBuilder ()
441441 .add ("role" , text .role ().text ())
442442 .add ("content" , JSON_BUILDER_FACTORY .createObjectBuilder ()
443443 .add ("type" , text .type ().text ())
444444 .add ("text" , text .text ()));
445445 }
446446
447- static JsonObjectBuilder toJson (McpSamplingAudioContent audio ) {
447+ static JsonObjectBuilder toJson (McpSamplingAudioMessage audio ) {
448448 return JSON_BUILDER_FACTORY .createObjectBuilder ()
449449 .add ("role" , audio .role ().text ())
450450 .add ("content" , JSON_BUILDER_FACTORY .createObjectBuilder ()
@@ -577,8 +577,6 @@ static McpSamplingResponse createSamplingResponse(JsonObject object) throws McpS
577577 throw new McpSamplingException (error .message ());
578578 });
579579 try {
580- var builder = McpSamplingResponse .builder ();
581-
582580 var result = find (object , "result" )
583581 .filter (McpJsonRpc ::isJsonObject )
584582 .map (JsonValue ::asJsonObject )
@@ -587,15 +585,13 @@ static McpSamplingResponse createSamplingResponse(JsonObject object) throws McpS
587585 String model = result .getString ("model" );
588586 McpRole role = McpRole .valueOf (result .getString ("role" ).toUpperCase ());
589587 McpSamplingMessage message = parseMessage (role , result .getJsonObject ("content" ));
590- Optional < McpStopReason > stopReason = find (result , "stopReason" )
588+ McpStopReason stopReason = find (result , "stopReason" )
591589 .filter (McpJsonRpc ::isJsonString )
592590 .map (JsonString .class ::cast )
593591 .map (JsonString ::getString )
594- .map (McpStopReason ::map );
595- builder .model (model );
596- builder .message (message );
597- builder .stopReason (stopReason );
598- return builder .build ();
592+ .map (McpStopReason ::map )
593+ .orElse (null );
594+ return new McpSamplingResponseImpl (message , model , stopReason );
599595 } catch (Exception e ) {
600596 throw new McpSamplingException ("Wrong sampling response format" , e );
601597 }
@@ -651,20 +647,19 @@ static JsonObject timeoutResponse(long requestId) {
651647
652648 private static McpSamplingMessage parseMessage (McpRole role , JsonObject object ) {
653649 String type = object .getString ("type" ).toUpperCase ();
654- McpContent . ContentType contentType = McpContent . ContentType .valueOf (type );
655- return switch (contentType ) {
656- case TEXT -> new McpSamplingTextContentImpl (object .getString ("text" ), role );
650+ McpSamplingMessageType messageType = McpSamplingMessageType .valueOf (type );
651+ return switch (messageType ) {
652+ case TEXT -> new McpSamplingTextMessageImpl (object .getString ("text" ), role );
657653 case IMAGE -> {
658654 byte [] data = object .getString ("data" ).getBytes (StandardCharsets .UTF_8 );
659655 MediaType mediaType = MediaTypes .create (object .getString ("mimeType" ));
660- yield new McpSamplingImageContentImpl (data , mediaType , role );
656+ yield new McpSamplingImageMessageImpl (data , mediaType , role );
661657 }
662658 case AUDIO -> {
663659 byte [] data = object .getString ("data" ).getBytes (StandardCharsets .UTF_8 );
664660 MediaType mediaType = MediaTypes .create (object .getString ("mimeType" ));
665- yield new McpSamplingAudioContentImpl (data , mediaType , role );
661+ yield new McpSamplingAudioMessageImpl (data , mediaType , role );
666662 }
667- default -> throw new IllegalArgumentException ("Unknown content type: " + type );
668663 };
669664 }
670665
0 commit comments