Configure JSON encoding format options (#21)

This commit is contained in:
Mattt 2025-03-18 09:44:44 -07:00 • committed by GitHub
commit e0186b5ad5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 11 deletions

View file

@ -1,4 +1,5 @@
import Foundation
import class Foundation.JSONDecoder
import class Foundation.JSONEncoder
private let jsonrpc = "2.0"
@ -130,9 +131,12 @@ final class TypedRequestHandler<M: Method>: RequestHandlerBox, @unchecked Sendab
override func callAsFunction(_ request: Request<AnyMethod>) async throws -> Response<AnyMethod>
{
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<M>(id: request.id, method: M.name, params: params)
// Handle with concrete type
@ -141,8 +145,8 @@ final class TypedRequestHandler<M: Method>: 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)

View file

@ -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 {

View file

@ -205,7 +205,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)
}
@ -260,7 +261,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)
}
@ -271,7 +276,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)
}