From 6a07642bbac5ad9ec919ba3660ede1e102faeb32 Mon Sep 17 00:00:00 2001 From: Alex Bullitt Date: Sat, 28 Nov 2026 17:44:16 -0800 Subject: [PATCH 1/2] docs: Say that an unknown block keeps its JSON value, not its bytes The `.unknown` cases on the content blocks and `CreateElicitation.Parameters` said the block "is encoded as received", and the migration guide said the same. `UnknownBlock` decodes into `Value` and re-encodes it, so key order and number formatting can change (`1.0` is written `1`). Anyone who signs, hashes or byte-compares a block passed through needs to know that. --- Sources/MCP/Client/Elicitation.swift | 4 +++- Sources/MCP/Client/Sampling.swift | 8 ++++++-- Sources/MCP/Server/Prompts.swift | 4 +++- Sources/MCP/Server/Tools.swift | 4 +++- docs/migration.md | 5 +++-- docs/src/migration.src.md | 5 +++-- 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Sources/MCP/Client/Elicitation.swift b/Sources/MCP/Client/Elicitation.swift index a660d162..b874b00b 100644 --- a/Sources/MCP/Client/Elicitation.swift +++ b/Sources/MCP/Client/Elicitation.swift @@ -78,7 +78,9 @@ public enum CreateElicitation: Method { /// URL-based elicitation parameters case url(URLParameters) /// A mode the SDK does not know, from a later revision: the mode and the - /// whole `params` as received. It is encoded as received. + /// whole `params` as received. + /// It encodes back to the same JSON value, though not always the same + /// bytes: key order and number formatting may change. case unknown(mode: Elicitation.Mode, Value) /// Parameters for form-based elicitation diff --git a/Sources/MCP/Client/Sampling.swift b/Sources/MCP/Client/Sampling.swift index 8dc5b726..2ff19aad 100644 --- a/Sources/MCP/Client/Sampling.swift +++ b/Sources/MCP/Client/Sampling.swift @@ -80,7 +80,9 @@ public enum Sampling { /// Tool result content case toolResult(Sampling.ToolResultContent) /// A block of a type the SDK does not know, from a later revision: its - /// `type` and the whole block as received. It is encoded as received. + /// `type` and the whole block as received. + /// It encodes back to the same JSON value, though not always the same + /// bytes: key order and number formatting may change. case unknown(type: String, Value) } @@ -240,7 +242,9 @@ public enum Sampling { /// Resource link case resourceLink(uri: String, name: String, title: String?, description: String?, mimeType: String?, annotations: Resource.Annotations?) /// A block of a type the SDK does not know, from a later revision: its - /// `type` and the whole block as received. It is encoded as received. + /// `type` and the whole block as received. + /// It encodes back to the same JSON value, though not always the same + /// bytes: key order and number formatting may change. case unknown(type: String, Value) } diff --git a/Sources/MCP/Server/Prompts.swift b/Sources/MCP/Server/Prompts.swift index 9de4b12e..e26d7a19 100644 --- a/Sources/MCP/Server/Prompts.swift +++ b/Sources/MCP/Server/Prompts.swift @@ -145,7 +145,9 @@ public struct Prompt: Hashable, Codable, Sendable { /// Resource link case resourceLink(uri: String, name: String, title: String? = nil, description: String? = nil, mimeType: String? = nil, annotations: Resource.Annotations? = nil) /// A block of a type the SDK does not know, from a later revision: its - /// `type` and the whole block as received. It is encoded as received. + /// `type` and the whole block as received. + /// It encodes back to the same JSON value, though not always the same + /// bytes: key order and number formatting may change. case unknown(type: String, Value) } } diff --git a/Sources/MCP/Server/Tools.swift b/Sources/MCP/Server/Tools.swift index a40f034d..9819d1e9 100644 --- a/Sources/MCP/Server/Tools.swift +++ b/Sources/MCP/Server/Tools.swift @@ -147,7 +147,9 @@ public struct Tool: Hashable, Codable, Sendable { annotations: Resource.Annotations? = nil ) /// A block of a type the SDK does not know, from a later revision: its - /// `type` and the whole block as received. It is encoded as received. + /// `type` and the whole block as received. + /// It encodes back to the same JSON value, though not always the same + /// bytes: key order and number formatting may change. case unknown(type: String, Value) private enum CodingKeys: String, CodingKey { diff --git a/docs/migration.md b/docs/migration.md index cddee802..f7fa8e08 100644 --- a/docs/migration.md +++ b/docs/migration.md @@ -304,8 +304,9 @@ failing it: | `InputResponse` | `.unknown(_:)`: a result of another shape | decoded as a sampling result, and failed | | `CreateElicitation.Parameters` | `.unknown(mode:_:)` | the request failed to decode | -A block or request is encoded as it was received. An exhaustive `switch` needs -the new case or a `default`; these enums gain no other case in 1.x. +A block or request encodes back to the same JSON value as received, though key +order and number formatting may change. An exhaustive `switch` needs the new +case or a `default`; these enums gain no other case in 1.x. ### Client convenience methods diff --git a/docs/src/migration.src.md b/docs/src/migration.src.md index a3168fbf..532d5107 100644 --- a/docs/src/migration.src.md +++ b/docs/src/migration.src.md @@ -242,8 +242,9 @@ failing it: | `InputResponse` | `.unknown(_:)`: a result of another shape | decoded as a sampling result, and failed | | `CreateElicitation.Parameters` | `.unknown(mode:_:)` | the request failed to decode | -A block or request is encoded as it was received. An exhaustive `switch` needs -the new case or a `default`; these enums gain no other case in 1.x. +A block or request encodes back to the same JSON value as received, though key +order and number formatting may change. An exhaustive `switch` needs the new +case or a `default`; these enums gain no other case in 1.x. ### Client convenience methods From 5f233c74e163abbe952ea63e8c4f02e8e16c2be8 Mon Sep 17 00:00:00 2001 From: Jean-Matthieu Schaffhauser Date: Sun, 29 Nov 2026 15:43:17 +0000 Subject: [PATCH 2/2] docs: Name number rounding, escapes and the InputRequest exception MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review follow-up. "The same JSON value" was too strong: `Value.Preserved` reads a number as an `Int`, then a `Double`, so an integer outside the `Int` range or a decimal beyond `Double` precision is rounded on the way back. The list of byte-level changes also left out whitespace and string escapes (`\u00e9` comes back as `é`, and the client's `JSONEncoder` writes `/` as `\/`). The migration paragraph spoke for every unknown kind, but an unknown `InputRequest` keeps only `method` and `params`, and a request without `params` (or with `"params": null`) goes back out with `"params": {}`. It now names blocks and elicitation `params`, and states that exception. A test pins it, and the test that said "re-encoded as received" is renamed. Written with AI assistance and reviewed by a human. --- Sources/MCP/Base/UnknownBlock.swift | 6 ++++-- Sources/MCP/Client/Elicitation.swift | 6 ++++-- Sources/MCP/Client/Sampling.swift | 12 ++++++++---- Sources/MCP/Server/Prompts.swift | 6 ++++-- Sources/MCP/Server/Tools.swift | 6 ++++-- Tests/MCPTests/UnknownKindTests.swift | 11 ++++++++++- docs/migration.md | 10 +++++++--- docs/src/migration.src.md | 10 +++++++--- 8 files changed, 48 insertions(+), 19 deletions(-) diff --git a/Sources/MCP/Base/UnknownBlock.swift b/Sources/MCP/Base/UnknownBlock.swift index 87686a20..abddabd4 100644 --- a/Sources/MCP/Base/UnknownBlock.swift +++ b/Sources/MCP/Base/UnknownBlock.swift @@ -5,8 +5,10 @@ import Foundation enum UnknownBlock { /// The object `decoder` holds, with strings as written: a `data:` URL /// stays a string, so the block encodes back to the same JSON value, - /// though not always the same bytes: key order and number formatting - /// may change (`1.0` is written `1`). + /// except that a number is read as an `Int` or a `Double`, so one outside + /// their range or precision is rounded. The bytes can differ: key order, + /// whitespace, string escapes and number formatting may change (`1.0` is + /// written `1`). static func decode(from decoder: any Decoder) throws -> Value { try Value.Preserved(from: decoder).value } diff --git a/Sources/MCP/Client/Elicitation.swift b/Sources/MCP/Client/Elicitation.swift index b874b00b..27984eaf 100644 --- a/Sources/MCP/Client/Elicitation.swift +++ b/Sources/MCP/Client/Elicitation.swift @@ -79,8 +79,10 @@ public enum CreateElicitation: Method { case url(URLParameters) /// A mode the SDK does not know, from a later revision: the mode and the /// whole `params` as received. - /// It encodes back to the same JSON value, though not always the same - /// bytes: key order and number formatting may change. + /// It encodes back to the same JSON value, except that a number is read + /// as an `Int` or a `Double`, so one outside their range or precision is + /// rounded. The bytes can differ: key order, whitespace, string escapes + /// and number formatting may change. case unknown(mode: Elicitation.Mode, Value) /// Parameters for form-based elicitation diff --git a/Sources/MCP/Client/Sampling.swift b/Sources/MCP/Client/Sampling.swift index 2ff19aad..7c9a3aba 100644 --- a/Sources/MCP/Client/Sampling.swift +++ b/Sources/MCP/Client/Sampling.swift @@ -81,8 +81,10 @@ public enum Sampling { case toolResult(Sampling.ToolResultContent) /// A block of a type the SDK does not know, from a later revision: its /// `type` and the whole block as received. - /// It encodes back to the same JSON value, though not always the same - /// bytes: key order and number formatting may change. + /// It encodes back to the same JSON value, except that a number is read + /// as an `Int` or a `Double`, so one outside their range or precision is + /// rounded. The bytes can differ: key order, whitespace, string escapes + /// and number formatting may change. case unknown(type: String, Value) } @@ -243,8 +245,10 @@ public enum Sampling { case resourceLink(uri: String, name: String, title: String?, description: String?, mimeType: String?, annotations: Resource.Annotations?) /// A block of a type the SDK does not know, from a later revision: its /// `type` and the whole block as received. - /// It encodes back to the same JSON value, though not always the same - /// bytes: key order and number formatting may change. + /// It encodes back to the same JSON value, except that a number is read + /// as an `Int` or a `Double`, so one outside their range or precision is + /// rounded. The bytes can differ: key order, whitespace, string escapes + /// and number formatting may change. case unknown(type: String, Value) } diff --git a/Sources/MCP/Server/Prompts.swift b/Sources/MCP/Server/Prompts.swift index e26d7a19..15ad10d4 100644 --- a/Sources/MCP/Server/Prompts.swift +++ b/Sources/MCP/Server/Prompts.swift @@ -146,8 +146,10 @@ public struct Prompt: Hashable, Codable, Sendable { case resourceLink(uri: String, name: String, title: String? = nil, description: String? = nil, mimeType: String? = nil, annotations: Resource.Annotations? = nil) /// A block of a type the SDK does not know, from a later revision: its /// `type` and the whole block as received. - /// It encodes back to the same JSON value, though not always the same - /// bytes: key order and number formatting may change. + /// It encodes back to the same JSON value, except that a number is read + /// as an `Int` or a `Double`, so one outside their range or precision is + /// rounded. The bytes can differ: key order, whitespace, string escapes + /// and number formatting may change. case unknown(type: String, Value) } } diff --git a/Sources/MCP/Server/Tools.swift b/Sources/MCP/Server/Tools.swift index 9819d1e9..f73bb2b7 100644 --- a/Sources/MCP/Server/Tools.swift +++ b/Sources/MCP/Server/Tools.swift @@ -148,8 +148,10 @@ public struct Tool: Hashable, Codable, Sendable { ) /// A block of a type the SDK does not know, from a later revision: its /// `type` and the whole block as received. - /// It encodes back to the same JSON value, though not always the same - /// bytes: key order and number formatting may change. + /// It encodes back to the same JSON value, except that a number is read + /// as an `Int` or a `Double`, so one outside their range or precision is + /// rounded. The bytes can differ: key order, whitespace, string escapes + /// and number formatting may change. case unknown(type: String, Value) private enum CodingKeys: String, CodingKey { diff --git a/Tests/MCPTests/UnknownKindTests.swift b/Tests/MCPTests/UnknownKindTests.swift index 826a975f..45eeffb8 100644 --- a/Tests/MCPTests/UnknownKindTests.swift +++ b/Tests/MCPTests/UnknownKindTests.swift @@ -18,7 +18,7 @@ private func value(_ json: String) throws -> Value { struct UnknownKindTests { private let block = #"{"type":"hologram","uri":"holo://1","frames":[1,2],"_meta":{"k":"v"}}"# - @Test("A tool content block of an unknown type is kept and re-encoded as received") + @Test("A tool content block of an unknown type is kept and encodes back to the same value") func toolContent() throws { let (content, encoded) = try roundTrip(Tool.Content.self, block) guard case .unknown(let type, let body) = content else { @@ -86,6 +86,15 @@ struct UnknownKindTests { #expect(request.isSupported(by: .init())) } + @Test("An input request of an unknown method is written back with its method and params only") + func inputRequestWithoutParams() throws { + for json in [#"{"method":"tasks/approve"}"#, #"{"method":"tasks/approve","params":null,"extra":1}"#] { + let (request, encoded) = try roundTrip(InputRequest.self, json) + #expect(request == .unknown(method: "tasks/approve", params: [:])) + #expect(encoded == ["method": "tasks/approve", "params": [:]]) + } + } + @Test("An input response of an unknown shape is kept") func inputResponse() throws { let json = #"{"approved":true}"# diff --git a/docs/migration.md b/docs/migration.md index f7fa8e08..e069da21 100644 --- a/docs/migration.md +++ b/docs/migration.md @@ -304,9 +304,13 @@ failing it: | `InputResponse` | `.unknown(_:)`: a result of another shape | decoded as a sampling result, and failed | | `CreateElicitation.Parameters` | `.unknown(mode:_:)` | the request failed to decode | -A block or request encodes back to the same JSON value as received, though key -order and number formatting may change. An exhaustive `switch` needs the new -case or a `default`; these enums gain no other case in 1.x. +An unknown block or elicitation `params` encodes back to the same JSON value as +received, except that a number is read as an `Int` or a `Double`, so one +outside their range or precision is rounded. The bytes can differ: key order, +whitespace, string escapes and number formatting may change. An unknown +`InputRequest` is written back with only its `method` and `params`, and with +`"params": {}` when it had none. An exhaustive `switch` needs the new case or a +`default`; these enums gain no other case in 1.x. ### Client convenience methods diff --git a/docs/src/migration.src.md b/docs/src/migration.src.md index 532d5107..437f4cc4 100644 --- a/docs/src/migration.src.md +++ b/docs/src/migration.src.md @@ -242,9 +242,13 @@ failing it: | `InputResponse` | `.unknown(_:)`: a result of another shape | decoded as a sampling result, and failed | | `CreateElicitation.Parameters` | `.unknown(mode:_:)` | the request failed to decode | -A block or request encodes back to the same JSON value as received, though key -order and number formatting may change. An exhaustive `switch` needs the new -case or a `default`; these enums gain no other case in 1.x. +An unknown block or elicitation `params` encodes back to the same JSON value as +received, except that a number is read as an `Int` or a `Double`, so one +outside their range or precision is rounded. The bytes can differ: key order, +whitespace, string escapes and number formatting may change. An unknown +`InputRequest` is written back with only its `method` and `params`, and with +`"params": {}` when it had none. An exhaustive `switch` needs the new case or a +`default`; these enums gain no other case in 1.x. ### Client convenience methods