@@ -388,7 +388,15 @@ public protocol HTTPContextProviding: Sendable {
388388package enum JSONRPCMessageKind : Decodable {
389389 case request ( id : String , method : String )
390390 case notification ( method : String )
391- case response ( id : String )
391+ /// A response; `id` is `nil` for an error response whose `id` is
392+ /// `null`, which answers a request whose `id` could not be read and
393+ /// names no request (JSON-RPC 2.0 §5).
394+ case response ( id : String ? )
395+ /// An answer (a `result` or an `error`) whose `id` is a string or an
396+ /// integer but whose `jsonrpc` is not `"2.0"`. It is answered with
397+ /// -32600 and `"id": null`, and still given to the server, which fails
398+ /// the request it names instead of leaving it waiting.
399+ case invalidResponse ( id : String )
392400 /// A message with a string `method` that is neither a valid request
393401 /// nor a valid notification: its `id` is not a string or an integer, or
394402 /// its `jsonrpc` is not `"2.0"`. It must be answered with -32600, using
@@ -408,6 +416,21 @@ package enum JSONRPCMessageKind: Decodable {
408416 self = decoded
409417 }
410418
419+ /// The error for data that could not be classified as a JSON-RPC message.
420+ static func classificationError ( for data : Data ) -> MCPError {
421+ // JSON-RPC 2.0 §5.1 reserves -32700 for invalid JSON. Scalars and arrays
422+ // are valid JSON, but are not single messages on the HTTP transport.
423+ // JSONDecoder, unlike JSONSerialization, reads a number out of range
424+ // such as `1e400` as valid JSON.
425+ struct AnyJSON : Decodable { init ( from decoder : Decoder ) { } }
426+ do {
427+ _ = try JSONDecoder ( ) . decode ( AnyJSON . self , from : data )
428+ return . invalidRequest ( " Invalid JSON-RPC message " )
429+ } catch {
430+ return . parseError ( " Invalid JSON-RPC message " )
431+ }
432+ }
433+
411434 /// Classifies a JSON-RPC object.
412435 ///
413436 /// A message with a string `method` is a request when it has an `id`
@@ -432,8 +455,19 @@ package enum JSONRPCMessageKind: Decodable {
432455 let reason = InvalidRequestReason . id ( try? container . decode ( Value . self , forKey : . id ) )
433456 self = . invalid ( method : method , id : nil , reason : reason )
434457 }
435- } else if container . contains ( . result ) || container . contains ( . error ) , let id {
436- self = . response ( id : id . description )
458+ } else if container . contains ( . result ) || container . contains ( . error ) ,
459+ id != nil || ( isNull && container . contains ( . error ) )
460+ {
461+ // JSON-RPC 2.0 §5 requires the version on responses too.
462+ guard ( try? container . decode ( String . self , forKey : . jsonrpc ) ) == " 2.0 " else {
463+ guard let id else {
464+ throw DecodingError . dataCorruptedError (
465+ forKey : . jsonrpc , in : container , debugDescription : InvalidRequestReason . jsonrpc )
466+ }
467+ self = . invalidResponse ( id : id . description )
468+ return
469+ }
470+ self = . response ( id : id ? . description )
437471 } else {
438472 throw DecodingError . dataCorrupted (
439473 DecodingError . Context (
0 commit comments