Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Sources/MCP/Base/UnknownBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/MCP/Client/Elicitation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions Sources/MCP/Client/Sampling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 5 additions & 1 deletion Sources/MCP/Server/Prompts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/MCP/Server/Tools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion Tests/MCPTests/UnknownKindTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}"#
Expand Down
9 changes: 7 additions & 2 deletions docs/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions docs/src/migration.src.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading