diff --git a/Sources/MCP/Base/Messages.swift b/Sources/MCP/Base/Messages.swift index 8214a702..4cb66a05 100644 --- a/Sources/MCP/Base/Messages.swift +++ b/Sources/MCP/Base/Messages.swift @@ -1,4 +1,5 @@ -import Foundation +import class Foundation.JSONDecoder +import class Foundation.JSONEncoder private let jsonrpc = "2.0" @@ -131,9 +132,12 @@ final class TypedRequestHandler: RequestHandlerBox, @unchecked Sendab override func callAsFunction(_ request: Request) async throws -> Response { + let encoder = JSONEncoder() + let decoder = JSONDecoder() + // Create a concrete request from the type-erased one - let data = try JSONEncoder().encode(request.params) - let params = try JSONDecoder().decode(M.Parameters.self, from: data) + let data = try encoder.encode(request.params) + let params = try decoder.decode(M.Parameters.self, from: data) let typedRequest = Request(id: request.id, method: M.name, params: params) // Handle with concrete type @@ -142,8 +146,8 @@ final class TypedRequestHandler: RequestHandlerBox, @unchecked Sendab // Convert result to AnyMethod response switch response.result { case .success(let result): - let resultData = try JSONEncoder().encode(result) - let resultValue = try JSONDecoder().decode(Value.self, from: resultData) + let resultData = try encoder.encode(result) + let resultValue = try decoder.decode(Value.self, from: resultData) return Response(id: response.id, result: resultValue) case .failure(let error): return Response(id: response.id, error: error) diff --git a/Sources/MCP/Base/Transports.swift b/Sources/MCP/Base/Transports.swift index b00827a0..b1c31b64 100644 --- a/Sources/MCP/Base/Transports.swift +++ b/Sources/MCP/Base/Transports.swift @@ -3,9 +3,6 @@ import Logging import SystemPackage import struct Foundation.Data -import struct Foundation.Date -import class Foundation.JSONDecoder -import class Foundation.JSONEncoder /// Protocol defining the transport layer for MCP communication public protocol Transport: Actor { diff --git a/Sources/MCP/Server/Server.swift b/Sources/MCP/Server/Server.swift index 495d7680..999d549d 100644 --- a/Sources/MCP/Server/Server.swift +++ b/Sources/MCP/Server/Server.swift @@ -213,7 +213,8 @@ public actor Server { "Error processing message", metadata: ["error": "\(error)"]) let response = AnyMethod.response( id: requestID ?? .random, - error: error as? Error ?? Error.internalError(error.localizedDescription) + error: error as? Error + ?? Error.internalError(error.localizedDescription) ) try? await send(response) } @@ -268,7 +269,11 @@ public actor Server { guard let connection = connection else { throw Error.internalError("Server connection not initialized") } - let responseData = try JSONEncoder().encode(response) + let encoder = JSONEncoder() + encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes] + + let responseData = try encoder.encode(response) + if let responseStr = String(data: responseData, encoding: .utf8) { try await connection.send(responseStr) } @@ -279,7 +284,12 @@ public actor Server { guard let connection = connection else { throw Error.internalError("Server connection not initialized") } - let notificationData = try JSONEncoder().encode(notification) + + let encoder = JSONEncoder() + encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes] + + let notificationData = try encoder.encode(notification) + if let notificationStr = String(data: notificationData, encoding: .utf8) { try await connection.send(notificationStr) }