Skip to content

Commit 4c85963

Browse files
chemicLtzolov
authored andcommitted
feat!: consistent JSON forward/backward compatibility - 2.0 foundation (#972)
The MCP specification evolves continuously; domain types must absorb new fields and subtypes without breaking existing clients or servers. On the 1.x line this is structurally prevented by sealed interfaces, which make it impossible to add a permitted subtype without breaking exhaustive pattern-match switch expressions in caller code. This commit opens the 2.0 release line, where those constraints are lifted and serialization is made self-contained — independent of any global ObjectMapper configuration. Breaking changes for users migrating from 1.x - Sealed interfaces removed from JSONRPCMessage, Request, Result, Notification, ResourceContents, CompleteReference and Content. Exhaustive switch expressions over these types must add a default branch. - Prompt(name, description, null) no longer silently coerces null arguments to an empty list. Use Prompt.withDefaults() to preserve the previous behaviour. - CompleteCompletion.total and .hasMore are now absent from the wire when not set, rather than being emitted as null. - ServerParameters no longer carries Jackson annotations; it is an internal configuration class, not a wire type. What now works that did not before - CompleteReference polymorphic dispatch (PromptReference vs ResourceReference) works through a plain readValue or convertValue call — no hand-rolled map inspection required. - LoggingLevel deserialization is lenient: unknown level strings produce null instead of throwing. - All domain records now tolerate unknown JSON fields, so a client built against an older SDK version will not fail when a newer server sends fields it does not yet recognise. - Null optional fields are consistently absent from serialized output regardless of ObjectMapper configuration. Documentation - CONTRIBUTING adds an "Evolving wire-serialized records" section: a 9-rule recipe and example for adding a field safely. - MIGRATION-2.0 documents all breaking changes listed above. Follow-up coming next Several spec-required fields (e.g. JSONRPCError.code/message, ProgressNotification.progress, CreateMessageRequest.maxTokens, CallToolResult.content) are stored as nullable Java types without a null guard. If constructed with null, the NON_ABSENT rule silently omits them, producing invalid wire JSON without throwing. Fix: compact canonical constructors with Assert.notNull, following the pattern already in JSONRPCRequest. Signed-off-by: Dariusz Jędrzejczyk <2554306+chemicL@users.noreply.github.com>
1 parent d182338 commit 4c85963

10 files changed

Lines changed: 688 additions & 117 deletions

File tree

CONTRIBUTING.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,83 @@ git checkout -b feature/your-feature-name
7575
allow the reviewer to focus on incremental changes instead of having to restart the
7676
review process.
7777

78+
## Evolving wire-serialized records
79+
80+
Records in `McpSchema` are serialized directly to the MCP JSON wire format. Follow these rules whenever you add a field to an existing record to keep the protocol forward- and backward-compatible.
81+
82+
### Rules
83+
84+
1. **Add new components only at the end** of the record's component list. Never reorder or rename existing components.
85+
2. **Annotate every component** with `@JsonProperty("fieldName")` even when the Java name already matches. This survives local renames via refactoring tools.
86+
3. **Use boxed types** (`Boolean`, `Integer`, `Long`, `Double`) so the field can be absent on the wire without a special sentinel.
87+
4. **Default to `null`**, not an empty collection or neutral value, so the `@JsonInclude(NON_NULL)` rule omits the field for clients that don't know about it yet.
88+
5. **Keep existing constructors as source-compatible overloads** that delegate to the new canonical constructor and pass `null` for the new component. Do not remove them in the same release that adds the field.
89+
6. **Do not put `@JsonCreator` on the canonical constructor** unless strictly necessary. Jackson auto-detects record canonical constructors; adding `@JsonCreator` pins deserialization to that exact parameter order forever.
90+
7. **Do not convert `null` to a default value in the canonical constructor.** Null carries "absent" semantics and must be preserved through the serialization round-trip.
91+
8. **Add three tests per new field** (put them in the relevant test class in `mcp-test`):
92+
- Deserialize JSON *without* the field → succeeds, field is `null`.
93+
- Serialize an instance with the field unset (`null`) → the key is absent from output.
94+
- Deserialize JSON with an extra *unknown* field → succeeds.
95+
9. **An inner `Builder` subclass can be used.** This improves the developer experience since frequently not all fields are required.
96+
97+
### Example
98+
99+
Suppose `ToolAnnotations` gains an optional `audience` field:
100+
101+
```java
102+
// Before
103+
@JsonInclude(JsonInclude.Include.NON_NULL)
104+
@JsonIgnoreProperties(ignoreUnknown = true)
105+
public record ToolAnnotations(
106+
@JsonProperty("title") String title,
107+
@JsonProperty("readOnlyHint") Boolean readOnlyHint,
108+
@JsonProperty("destructiveHint") Boolean destructiveHint,
109+
@JsonProperty("idempotentHint") Boolean idempotentHint,
110+
@JsonProperty("openWorldHint") Boolean openWorldHint) { ... }
111+
112+
// After — new component appended at the end
113+
@JsonInclude(JsonInclude.Include.NON_NULL)
114+
@JsonIgnoreProperties(ignoreUnknown = true)
115+
public record ToolAnnotations(
116+
@JsonProperty("title") String title,
117+
@JsonProperty("readOnlyHint") Boolean readOnlyHint,
118+
@JsonProperty("destructiveHint") Boolean destructiveHint,
119+
@JsonProperty("idempotentHint") Boolean idempotentHint,
120+
@JsonProperty("openWorldHint") Boolean openWorldHint,
121+
@JsonProperty("audience") List<String> audience) { // new — added at end
122+
123+
// Keep the old constructor so existing callers still compile
124+
public ToolAnnotations(String title, Boolean readOnlyHint,
125+
Boolean destructiveHint, Boolean idempotentHint, Boolean openWorldHint) {
126+
this(title, readOnlyHint, destructiveHint, idempotentHint, openWorldHint, null);
127+
}
128+
}
129+
```
130+
131+
Tests to add:
132+
133+
```java
134+
@Test
135+
void toolAnnotationsDeserializesWithoutAudience() throws IOException {
136+
ToolAnnotations a = mapper.readValue("""
137+
{"title":"My tool","readOnlyHint":true}""", ToolAnnotations.class);
138+
assertThat(a.audience()).isNull();
139+
}
140+
141+
@Test
142+
void toolAnnotationsOmitsNullAudience() throws IOException {
143+
String json = mapper.writeValueAsString(new ToolAnnotations("t", null, null, null, null));
144+
assertThat(json).doesNotContain("audience");
145+
}
146+
147+
@Test
148+
void toolAnnotationsToleratesUnknownFields() throws IOException {
149+
ToolAnnotations a = mapper.readValue("""
150+
{"title":"t","futureField":42}""", ToolAnnotations.class);
151+
assertThat(a.title()).isEqualTo("t");
152+
}
153+
```
154+
78155
## Code of Conduct
79156

80157
This project follows a Code of Conduct. Please review it in

MIGRATION-2.0.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Migration Guide — 2.0
2+
3+
This document covers breaking and behavioural changes introduced in the 2.0 release of the MCP Java SDK.
4+
5+
---
6+
7+
## Jackson / JSON serialization changes
8+
9+
### Sealed interfaces removed
10+
11+
The following interfaces were `sealed` in 1.x and are now plain interfaces in 2.0:
12+
13+
- `McpSchema.JSONRPCMessage`
14+
- `McpSchema.Request`
15+
- `McpSchema.Result`
16+
- `McpSchema.Notification`
17+
- `McpSchema.ResourceContents`
18+
- `McpSchema.CompleteReference`
19+
- `McpSchema.Content`
20+
21+
**Impact:** Exhaustive `switch` expressions or `switch` statements that relied on the sealed hierarchy for completeness checking must add a `default` branch. The compiler will no longer reject switches that omit one of the known subtypes.
22+
23+
### `CompleteReference` now carries `@JsonTypeInfo`
24+
25+
`CompleteReference` (and its implementations `PromptReference` and `ResourceReference`) is now annotated with `@JsonTypeInfo(use = NAME, include = EXISTING_PROPERTY, property = "type", visible = true)`. Jackson will automatically dispatch to the correct subtype based on the `"type"` field in the JSON without any hand-written map-walking code.
26+
27+
**Action:** Remove any custom code that manually inspected the `"type"` field of a completion reference map and instantiated `PromptReference` / `ResourceReference` by hand. A plain `mapper.readValue(json, CompleteRequest.class)` or `mapper.convertValue(paramsMap, CompleteRequest.class)` is sufficient.
28+
29+
### `Prompt` canonical constructor no longer coerces `null` arguments
30+
31+
In 1.x, `new Prompt(name, description, null)` silently stored an empty list for `arguments`. In 2.0 it stores `null`.
32+
33+
**Action:**
34+
35+
- Code that expected `prompt.arguments()` to return an empty list when not provided will now receive `null`. Add a null-check or use the new `Prompt.withDefaults(name, description, arguments)` factory, which preserves the old behaviour by coercing `null` to `[]`.
36+
- On the wire, a prompt without an `arguments` field deserializes with `arguments == null` (it is not coerced to an empty list).
37+
38+
### `CompleteCompletion` optional fields omitted when null
39+
40+
`CompleteResult.CompleteCompletion.total` and `CompleteCompletion.hasMore` are now omitted from serialized JSON when they are `null` (previously they were always emitted). Deserializers that required these fields to be present in every response must be updated to treat their absence as `null`.
41+
42+
### `CompleteCompletion.values` is mandatory in the Java API
43+
44+
The compact constructor for `CompleteCompletion` asserts that `values` is not `null`. Code that constructed a completion result with a null `values` list will now fail at runtime.
45+
46+
**Action:** Always pass a non-null list (for example `List.of()` when there are no suggestions).
47+
48+
### `LoggingLevel` deserialization is lenient
49+
50+
`LoggingLevel` now uses a `@JsonCreator` factory (`fromValue`) so that JSON string values deserialize in a case-insensitive way. **Unrecognized level strings deserialize to `null`** instead of causing deserialization to fail.
51+
52+
**Impact:** `SetLevelRequest`, `LoggingMessageNotification`, and any other type that embeds `LoggingLevel` can observe a `null` level when the wire value is unknown or misspelled. Downstream code must null-check or validate before use.
53+
54+
### `Content.type()` is ignored for Jackson serialization
55+
56+
The `Content` interface still exposes `type()` as a convenience for Java callers, but the method is annotated with `@JsonIgnore` so Jackson does not treat it as a duplicate `"type"` property alongside `@JsonTypeInfo` on the interface.
57+
58+
**Impact:** Custom serializers or `ObjectMapper` configuration that relied on serializing `Content` through the default `type()` accessor alone should use the concrete content records (each of which carries a real `"type"` property) or the polymorphic setup on `Content`.
59+
60+
### `ServerParameters` no longer carries Jackson annotations
61+
62+
`ServerParameters` (in `client/transport`) has had its `@JsonProperty` and `@JsonInclude` annotations removed. It was never a wire type and is not serialized to JSON in normal SDK usage. If your code serialized or deserialized `ServerParameters` using Jackson, switch to a plain map or a dedicated DTO.
63+
64+
### Record annotation sweep
65+
66+
Wire-oriented `public record` types in `McpSchema` consistently use `@JsonInclude(JsonInclude.Include.NON_ABSENT)` (or equivalent per-type configuration) and `@JsonIgnoreProperties(ignoreUnknown = true)`. Nested capability objects under `ClientCapabilities` / `ServerCapabilities` (for example `Sampling`, `Elicitation`, `CompletionCapabilities`, `LoggingCapabilities`, prompt/resource/tool capability records) also ignore unknown JSON properties. This means:
67+
68+
- **Unknown fields** in incoming JSON are silently ignored, improving forward compatibility with newer server or client versions.
69+
- **Absent optional properties** are omitted from outgoing JSON where `NON_ABSENT` applies, and optional Java components deserialize as `null` when missing on the wire.
70+
71+
### `Tool.inputSchema` is `Map<String, Object>`, not `JsonSchema`
72+
73+
The `Tool` record now models `inputSchema` (and `outputSchema`) as arbitrary JSON Schema objects as `Map<String, Object>`, so dialect-specific keywords (`$ref`, `unevaluatedProperties`, vendor extensions, and so on) round-trip without being trimmed by a narrow `JsonSchema` record.
74+
75+
**Impact:**
76+
77+
- Java code that used `Tool.inputSchema()` as a `JsonSchema` must switch to `Map<String, Object>` (or copy into your own schema wrapper).
78+
- `Tool.Builder.inputSchema(JsonSchema)` remains as a **deprecated** helper that maps the old record into a map; prefer `inputSchema(Map)` or `inputSchema(McpJsonMapper, String)`.
79+
80+
### Optional JSON Schema validation on `tools/call` (server)
81+
82+
When a `JsonSchemaValidator` is available (including the default from `McpJsonDefaults.getSchemaValidator()` when you do not configure one explicitly) and `validateToolInputs` is left at its default of `true`, the server validates incoming tool arguments against `tool.inputSchema()` before invoking the tool. Failed validation produces a `CallToolResult` with `isError` set and a textual error in the content.
83+
84+
**Action:** Ensure `inputSchema` maps are valid for your validator, tighten client arguments, or disable validation with `validateToolInputs(false)` on the server builder if you must preserve pre-2.0 behaviour.

mcp-core/src/main/java/io/modelcontextprotocol/client/transport/ServerParameters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024-2024 the original author or authors.
2+
* Copyright 2024-2026 the original author or authors.
33
*/
44

55
package io.modelcontextprotocol.client.transport;

mcp-core/src/main/java/io/modelcontextprotocol/server/McpAsyncServer.java

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024-2024 the original author or authors.
2+
* Copyright 2024-2026 the original author or authors.
33
*/
44

55
package io.modelcontextprotocol.server;
@@ -971,7 +971,8 @@ private McpRequestHandler<Object> setLoggerRequestHandler() {
971971
private McpRequestHandler<McpSchema.CompleteResult> completionCompleteRequestHandler() {
972972
return (exchange, params) -> {
973973

974-
McpSchema.CompleteRequest request = parseCompletionParams(params);
974+
McpSchema.CompleteRequest request = jsonMapper.convertValue(params, new TypeRef<>() {
975+
});
975976

976977
if (request.ref() == null) {
977978
return Mono.error(
@@ -1072,50 +1073,6 @@ private McpRequestHandler<McpSchema.CompleteResult> completionCompleteRequestHan
10721073
};
10731074
}
10741075

1075-
/**
1076-
* Parses the raw JSON-RPC request parameters into a {@link McpSchema.CompleteRequest}
1077-
* object.
1078-
* <p>
1079-
* This method manually extracts the `ref` and `argument` fields from the input map,
1080-
* determines the correct reference type (either prompt or resource), and constructs a
1081-
* fully-typed {@code CompleteRequest} instance.
1082-
* @param object the raw request parameters, expected to be a Map containing "ref" and
1083-
* "argument" entries.
1084-
* @return a {@link McpSchema.CompleteRequest} representing the structured completion
1085-
* request.
1086-
* @throws IllegalArgumentException if the "ref" type is not recognized.
1087-
*/
1088-
@SuppressWarnings("unchecked")
1089-
private McpSchema.CompleteRequest parseCompletionParams(Object object) {
1090-
Map<String, Object> params = (Map<String, Object>) object;
1091-
Map<String, Object> refMap = (Map<String, Object>) params.get("ref");
1092-
Map<String, Object> argMap = (Map<String, Object>) params.get("argument");
1093-
Map<String, Object> contextMap = (Map<String, Object>) params.get("context");
1094-
Map<String, Object> meta = (Map<String, Object>) params.get("_meta");
1095-
1096-
String refType = (String) refMap.get("type");
1097-
1098-
McpSchema.CompleteReference ref = switch (refType) {
1099-
case PromptReference.TYPE -> new McpSchema.PromptReference(refType, (String) refMap.get("name"),
1100-
refMap.get("title") != null ? (String) refMap.get("title") : null);
1101-
case ResourceReference.TYPE -> new McpSchema.ResourceReference(refType, (String) refMap.get("uri"));
1102-
default -> throw new IllegalArgumentException("Invalid ref type: " + refType);
1103-
};
1104-
1105-
String argName = (String) argMap.get("name");
1106-
String argValue = (String) argMap.get("value");
1107-
McpSchema.CompleteRequest.CompleteArgument argument = new McpSchema.CompleteRequest.CompleteArgument(argName,
1108-
argValue);
1109-
1110-
McpSchema.CompleteRequest.CompleteContext context = null;
1111-
if (contextMap != null) {
1112-
Map<String, String> arguments = (Map<String, String>) contextMap.get("arguments");
1113-
context = new McpSchema.CompleteRequest.CompleteContext(arguments);
1114-
}
1115-
1116-
return new McpSchema.CompleteRequest(ref, argument, meta, context);
1117-
}
1118-
11191076
/**
11201077
* This method is package-private and used for test only. Should not be called by user
11211078
* code.

mcp-core/src/main/java/io/modelcontextprotocol/server/McpStatelessAsyncServer.java

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024-2024 the original author or authors.
2+
* Copyright 2024-2026 the original author or authors.
33
*/
44

55
package io.modelcontextprotocol.server;
@@ -715,7 +715,8 @@ private McpStatelessRequestHandler<McpSchema.GetPromptResult> promptsGetRequestH
715715

716716
private McpStatelessRequestHandler<McpSchema.CompleteResult> completionCompleteRequestHandler() {
717717
return (ctx, params) -> {
718-
McpSchema.CompleteRequest request = parseCompletionParams(params);
718+
McpSchema.CompleteRequest request = jsonMapper.convertValue(params, new TypeRef<>() {
719+
});
719720

720721
if (request.ref() == null) {
721722
return Mono.error(
@@ -815,42 +816,6 @@ private McpStatelessRequestHandler<McpSchema.CompleteResult> completionCompleteR
815816
};
816817
}
817818

818-
/**
819-
* Parses the raw JSON-RPC request parameters into a {@link McpSchema.CompleteRequest}
820-
* object.
821-
* <p>
822-
* This method manually extracts the `ref` and `argument` fields from the input map,
823-
* determines the correct reference type (either prompt or resource), and constructs a
824-
* fully-typed {@code CompleteRequest} instance.
825-
* @param object the raw request parameters, expected to be a Map containing "ref" and
826-
* "argument" entries.
827-
* @return a {@link McpSchema.CompleteRequest} representing the structured completion
828-
* request.
829-
* @throws IllegalArgumentException if the "ref" type is not recognized.
830-
*/
831-
@SuppressWarnings("unchecked")
832-
private McpSchema.CompleteRequest parseCompletionParams(Object object) {
833-
Map<String, Object> params = (Map<String, Object>) object;
834-
Map<String, Object> refMap = (Map<String, Object>) params.get("ref");
835-
Map<String, Object> argMap = (Map<String, Object>) params.get("argument");
836-
837-
String refType = (String) refMap.get("type");
838-
839-
McpSchema.CompleteReference ref = switch (refType) {
840-
case PromptReference.TYPE -> new McpSchema.PromptReference(refType, (String) refMap.get("name"),
841-
refMap.get("title") != null ? (String) refMap.get("title") : null);
842-
case ResourceReference.TYPE -> new McpSchema.ResourceReference(refType, (String) refMap.get("uri"));
843-
default -> throw new IllegalArgumentException("Invalid ref type: " + refType);
844-
};
845-
846-
String argName = (String) argMap.get("name");
847-
String argValue = (String) argMap.get("value");
848-
McpSchema.CompleteRequest.CompleteArgument argument = new McpSchema.CompleteRequest.CompleteArgument(argName,
849-
argValue);
850-
851-
return new McpSchema.CompleteRequest(ref, argument);
852-
}
853-
854819
/**
855820
* This method is package-private and used for test only. Should not be called by user
856821
* code.

0 commit comments

Comments
 (0)