Skip to content

Commit 704f8c2

Browse files
Yugi-2claude
authored andcommitted
feat: add extensions to ClientCapabilities
Add the optional `extensions` field to `ClientCapabilities`, mirroring the MCP extensions framework (SEP-2133) as declared in the draft schema and already shipped by the TypeScript, Python, C#, Go, Rust, and Kotlin SDKs. Keys are extension identifiers (e.g. "io.modelcontextprotocol/oauth-client-credentials") and values are per-extension settings objects; an empty object indicates support with no settings. The field is optional, defaults to null, and is omitted from the wire when unset, so it is backward compatible with existing peers. Follows CONTRIBUTING.md "Evolving wire-serialized records" Case A: the new component is appended, annotated with @JsonProperty, boxed/nullable, and the prior 4-arg constructor is retained as a source-compatible overload. Adds a builder `extensions(Map)` setter plus an `extension(id, settings)` helper that normalizes null settings to an empty object. Includes the required absent/omit/unknown-field tests plus a round-trip test in SchemaEvolutionTests. Refs modelcontextprotocol#785 Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent 1cf7903 commit 704f8c2

2 files changed

Lines changed: 77 additions & 2 deletions

File tree

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,14 +577,23 @@ public InitializeResult build() {
577577
* @param roots Present if the client supports listing roots
578578
* @param sampling Present if the client supports sampling from an LLM
579579
* @param elicitation Present if the client supports elicitation from the server
580+
* @param extensions Present if the client supports MCP extensions, keyed by extension
581+
* identifier
580582
*/
581583
@JsonInclude(JsonInclude.Include.NON_ABSENT)
582584
@JsonIgnoreProperties(ignoreUnknown = true)
583585
public record ClientCapabilities( // @formatter:off
584586
@JsonProperty("experimental") Map<String, Object> experimental,
585587
@JsonProperty("roots") RootCapabilities roots,
586588
@JsonProperty("sampling") Sampling sampling,
587-
@JsonProperty("elicitation") Elicitation elicitation) { // @formatter:on
589+
@JsonProperty("elicitation") Elicitation elicitation,
590+
@JsonProperty("extensions") Map<String, Object> extensions) { // @formatter:on
591+
592+
// Keep the old constructor so existing callers still compile
593+
public ClientCapabilities(Map<String, Object> experimental, RootCapabilities roots, Sampling sampling,
594+
Elicitation elicitation) {
595+
this(experimental, roots, sampling, elicitation, null);
596+
}
588597

589598
/**
590599
* Present if the client supports listing roots.
@@ -723,6 +732,8 @@ public static class Builder {
723732

724733
private Elicitation elicitation;
725734

735+
private Map<String, Object> extensions;
736+
726737
public Builder experimental(Map<String, Object> experimental) {
727738
this.experimental = experimental;
728739
return this;
@@ -765,8 +776,29 @@ public Builder elicitation(Elicitation elicitation) {
765776
return this;
766777
}
767778

779+
public Builder extensions(Map<String, Object> extensions) {
780+
this.extensions = extensions;
781+
return this;
782+
}
783+
784+
/**
785+
* Adds support for a single extension; {@code null} settings are sent as an
786+
* empty JSON object.
787+
* @param id the extension identifier
788+
* @param settings the per-extension settings, or {@code null}
789+
* @return this builder
790+
*/
791+
public Builder extension(String id, Map<String, Object> settings) {
792+
Assert.hasText(id, "id must not be null or empty");
793+
Map<String, Object> merged = (this.extensions != null) ? new HashMap<>(this.extensions)
794+
: new HashMap<>();
795+
merged.put(id, settings != null ? settings : Map.of());
796+
this.extensions = merged;
797+
return this;
798+
}
799+
768800
public ClientCapabilities build() {
769-
return new ClientCapabilities(experimental, roots, sampling, elicitation);
801+
return new ClientCapabilities(experimental, roots, sampling, elicitation, extensions);
770802
}
771803

772804
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.io.IOException;
1111
import java.util.List;
12+
import java.util.Map;
1213

1314
import io.modelcontextprotocol.json.McpJsonMapper;
1415
import org.junit.jupiter.api.Test;
@@ -143,6 +144,48 @@ void serverCapabilitiesUnknownFieldsIgnored() throws IOException {
143144
assertThat(caps.tools().listChanged()).isTrue();
144145
}
145146

147+
// -----------------------------------------------------------------------
148+
// ClientCapabilities.extensions
149+
// -----------------------------------------------------------------------
150+
151+
@Test
152+
void clientCapabilitiesWithoutExtensionsDeserializesAsNull() throws IOException {
153+
String json = """
154+
{"roots":{"listChanged":true},"sampling":{}}
155+
""";
156+
McpSchema.ClientCapabilities caps = mapper.readValue(json, McpSchema.ClientCapabilities.class);
157+
assertThat(caps.extensions()).isNull();
158+
}
159+
160+
@Test
161+
void clientCapabilitiesWithNullExtensionsOmitsFieldOnWire() throws IOException {
162+
String json = mapper.writeValueAsString(McpSchema.ClientCapabilities.builder().sampling().build());
163+
assertThat(json).doesNotContain("extensions");
164+
}
165+
166+
@Test
167+
void clientCapabilitiesWithExtensionsUnknownFieldsIgnored() throws IOException {
168+
String json = """
169+
{"extensions":{"io.modelcontextprotocol/tasks":{}},"futureCapability":{"a":1}}
170+
""";
171+
McpSchema.ClientCapabilities caps = mapper.readValue(json, McpSchema.ClientCapabilities.class);
172+
assertThat(caps.extensions()).containsOnlyKeys("io.modelcontextprotocol/tasks");
173+
}
174+
175+
@Test
176+
void clientCapabilitiesExtensionsRoundTrip() throws IOException {
177+
McpSchema.ClientCapabilities caps = McpSchema.ClientCapabilities.builder()
178+
.extension("com.example/ext-with-settings", Map.of("maxDepth", 3))
179+
.extension("com.example/ext-without-settings", null)
180+
.build();
181+
182+
String json = mapper.writeValueAsString(caps);
183+
assertThat(json).contains("\"com.example/ext-without-settings\":{}");
184+
185+
McpSchema.ClientCapabilities parsed = mapper.readValue(json, McpSchema.ClientCapabilities.class);
186+
assertThat(parsed.extensions()).isEqualTo(caps.extensions());
187+
}
188+
146189
// -----------------------------------------------------------------------
147190
// JSONRPCError
148191
// -----------------------------------------------------------------------

0 commit comments

Comments
 (0)