Skip to content

Commit 61c6a06

Browse files
Yugi-2claude
authored andcommitted
feat: add extensions to ServerCapabilities
Mirror the ClientCapabilities `extensions` change on ServerCapabilities, as the MCP extensions framework (SEP-2133) declares the field on both capability objects. Same Case A treatment: appended optional component, retained 6-arg constructor overload, builder `extensions(Map)` + `extension(id, settings)` helper, and `mutate()` carries the new field. Adds absent/omit/round-trip tests in SchemaEvolutionTests. Refs modelcontextprotocol#785 Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent 704f8c2 commit 61c6a06

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎mcp-core/src/main/java/io/modelcontextprotocol/spec/McpSchema.java‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,8 @@ public ClientCapabilities build() {
817817
* @param prompts Present if the server offers any prompt templates
818818
* @param resources Present if the server offers any resources to read
819819
* @param tools Present if the server offers any tools to call
820+
* @param extensions Present if the server supports MCP extensions, keyed by extension
821+
* identifier
820822
*/
821823
@JsonInclude(JsonInclude.Include.NON_ABSENT)
822824
@JsonIgnoreProperties(ignoreUnknown = true)
@@ -826,7 +828,15 @@ public record ServerCapabilities( // @formatter:off
826828
@JsonProperty("logging") LoggingCapabilities logging,
827829
@JsonProperty("prompts") PromptCapabilities prompts,
828830
@JsonProperty("resources") ResourceCapabilities resources,
829-
@JsonProperty("tools") ToolCapabilities tools) { // @formatter:on
831+
@JsonProperty("tools") ToolCapabilities tools,
832+
@JsonProperty("extensions") Map<String, Object> extensions) { // @formatter:on
833+
834+
// Keep the old constructor so existing callers still compile
835+
public ServerCapabilities(CompletionCapabilities completions, Map<String, Object> experimental,
836+
LoggingCapabilities logging, PromptCapabilities prompts, ResourceCapabilities resources,
837+
ToolCapabilities tools) {
838+
this(completions, experimental, logging, prompts, resources, tools, null);
839+
}
830840

831841
/**
832842
* Present if the server supports argument autocompletion suggestions.
@@ -955,6 +965,7 @@ public Builder mutate() {
955965
builder.prompts = this.prompts;
956966
builder.resources = this.resources;
957967
builder.tools = this.tools;
968+
builder.extensions = this.extensions;
958969
return builder;
959970
}
960971

@@ -976,6 +987,8 @@ public static class Builder {
976987

977988
private ToolCapabilities tools;
978989

990+
private Map<String, Object> extensions;
991+
979992
public Builder completions() {
980993
this.completions = new CompletionCapabilities();
981994
return this;
@@ -1006,8 +1019,30 @@ public Builder tools(Boolean listChanged) {
10061019
return this;
10071020
}
10081021

1022+
public Builder extensions(Map<String, Object> extensions) {
1023+
this.extensions = extensions;
1024+
return this;
1025+
}
1026+
1027+
/**
1028+
* Adds support for a single extension; {@code null} settings are sent as an
1029+
* empty JSON object.
1030+
* @param id the extension identifier
1031+
* @param settings the per-extension settings, or {@code null}
1032+
* @return this builder
1033+
*/
1034+
public Builder extension(String id, Map<String, Object> settings) {
1035+
Assert.hasText(id, "id must not be null or empty");
1036+
Map<String, Object> merged = (this.extensions != null) ? new HashMap<>(this.extensions)
1037+
: new HashMap<>();
1038+
merged.put(id, settings != null ? settings : Map.of());
1039+
this.extensions = merged;
1040+
return this;
1041+
}
1042+
10091043
public ServerCapabilities build() {
1010-
return new ServerCapabilities(completions, experimental, logging, prompts, resources, tools);
1044+
return new ServerCapabilities(completions, experimental, logging, prompts, resources, tools,
1045+
extensions);
10111046
}
10121047

10131048
}

‎mcp-test/src/test/java/io/modelcontextprotocol/spec/SchemaEvolutionTests.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,35 @@ void serverCapabilitiesUnknownFieldsIgnored() throws IOException {
144144
assertThat(caps.tools().listChanged()).isTrue();
145145
}
146146

147+
@Test
148+
void serverCapabilitiesWithoutExtensionsDeserializesAsNull() throws IOException {
149+
String json = """
150+
{"tools":{"listChanged":true}}
151+
""";
152+
McpSchema.ServerCapabilities caps = mapper.readValue(json, McpSchema.ServerCapabilities.class);
153+
assertThat(caps.extensions()).isNull();
154+
}
155+
156+
@Test
157+
void serverCapabilitiesWithNullExtensionsOmitsFieldOnWire() throws IOException {
158+
String json = mapper.writeValueAsString(McpSchema.ServerCapabilities.builder().tools(true).build());
159+
assertThat(json).doesNotContain("extensions");
160+
}
161+
162+
@Test
163+
void serverCapabilitiesExtensionsRoundTrip() throws IOException {
164+
McpSchema.ServerCapabilities caps = McpSchema.ServerCapabilities.builder()
165+
.extension("com.example/ext-with-settings", Map.of("maxDepth", 3))
166+
.extension("com.example/ext-without-settings", null)
167+
.build();
168+
169+
String json = mapper.writeValueAsString(caps);
170+
assertThat(json).contains("\"com.example/ext-without-settings\":{}");
171+
172+
McpSchema.ServerCapabilities parsed = mapper.readValue(json, McpSchema.ServerCapabilities.class);
173+
assertThat(parsed.extensions()).isEqualTo(caps.extensions());
174+
}
175+
147176
// -----------------------------------------------------------------------
148177
// ClientCapabilities.extensions
149178
// -----------------------------------------------------------------------

0 commit comments

Comments
 (0)