Skip to content

Commit 1c1d80d

Browse files
committed
fix: support freeform stopReason in sampling
Previously, sampling response parsing would fail if a stopReason was provided besides endTurn/stopSequence/maxTokens. This fixes that to map the arbitrary values to UNKNOWN instead. Spec: https://github.com/modelcontextprotocol/modelcontextprotocol/blob/f5ccad944fdf2b7d9cc70cf817f66ca5a8aa03a4/schema/2024-11-05/schema.ts#L807
1 parent b701a36 commit 1c1d80d

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

mcp/src/main/java/io/modelcontextprotocol/spec/McpSchema.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
import java.io.IOException;
88
import java.util.ArrayList;
9+
import java.util.Arrays;
910
import java.util.HashMap;
1011
import java.util.List;
1112
import java.util.Map;
1213

14+
import com.fasterxml.jackson.annotation.JsonCreator;
1315
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
1416
import com.fasterxml.jackson.annotation.JsonInclude;
1517
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -1072,9 +1074,24 @@ public record CreateMessageResult(// @formatter:off
10721074
@JsonProperty("stopReason") StopReason stopReason) {
10731075

10741076
public enum StopReason {
1075-
@JsonProperty("endTurn") END_TURN,
1076-
@JsonProperty("stopSequence") STOP_SEQUENCE,
1077-
@JsonProperty("maxTokens") MAX_TOKENS
1077+
@JsonProperty("endTurn") END_TURN("endTurn"),
1078+
@JsonProperty("stopSequence") STOP_SEQUENCE("stopSequence"),
1079+
@JsonProperty("maxTokens") MAX_TOKENS("maxTokens"),
1080+
UNKNOWN("unknown");
1081+
1082+
private final String value;
1083+
1084+
StopReason(String value) {
1085+
this.value = value;
1086+
}
1087+
1088+
@JsonCreator
1089+
private static StopReason of(String value) {
1090+
return Arrays.stream(StopReason.values())
1091+
.filter(stopReason -> stopReason.value.equals(value))
1092+
.findFirst()
1093+
.orElse(StopReason.UNKNOWN);
1094+
}
10781095
}
10791096

10801097
public static Builder builder() {

mcp/src/test/java/io/modelcontextprotocol/spec/McpSchemaTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,23 @@ void testCreateMessageResult() throws Exception {
829829
{"role":"assistant","content":{"type":"text","text":"Assistant response"},"model":"gpt-4","stopReason":"endTurn"}"""));
830830
}
831831

832+
@Test
833+
void testCreateMessageResultUnknownStopReason() throws Exception {
834+
String input = """
835+
{"role":"assistant","content":{"type":"text","text":"Assistant response"},"model":"gpt-4","stopReason":"arbitrary value"}""";
836+
837+
McpSchema.CreateMessageResult value = mapper.readValue(input, McpSchema.CreateMessageResult.class);
838+
839+
McpSchema.TextContent expectedContent = new McpSchema.TextContent("Assistant response");
840+
McpSchema.CreateMessageResult expected = McpSchema.CreateMessageResult.builder()
841+
.role(McpSchema.Role.ASSISTANT)
842+
.content(expectedContent)
843+
.model("gpt-4")
844+
.stopReason(McpSchema.CreateMessageResult.StopReason.UNKNOWN)
845+
.build();
846+
assertThat(value).isEqualTo(expected);
847+
}
848+
832849
// Elicitation Tests
833850

834851
@Test

0 commit comments

Comments
 (0)