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 a660d162..27984eaf 100644 --- a/Sources/MCP/Client/Elicitation.swift +++ b/Sources/MCP/Client/Elicitation.swift @@ -78,7 +78,11 @@ 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, 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 8dc5b726..7c9a3aba 100644 --- a/Sources/MCP/Client/Sampling.swift +++ b/Sources/MCP/Client/Sampling.swift @@ -80,7 +80,11 @@ 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, 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) } @@ -240,7 +244,11 @@ 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, 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 9de4b12e..15ad10d4 100644 --- a/Sources/MCP/Server/Prompts.swift +++ b/Sources/MCP/Server/Prompts.swift @@ -145,7 +145,11 @@ 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, 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 a40f034d..f73bb2b7 100644 --- a/Sources/MCP/Server/Tools.swift +++ b/Sources/MCP/Server/Tools.swift @@ -147,7 +147,11 @@ 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, 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 cddee802..e069da21 100644 --- a/docs/migration.md +++ b/docs/migration.md @@ -304,8 +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 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. +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 a3168fbf..437f4cc4 100644 --- a/docs/src/migration.src.md +++ b/docs/src/migration.src.md @@ -242,8 +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 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. +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