Configure JSON encoding format options (#21)
This commit is contained in:
parent
c672358e7b
commit
e0186b5ad5
3 changed files with 22 additions and 11 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import Foundation
|
import class Foundation.JSONDecoder
|
||||||
|
import class Foundation.JSONEncoder
|
||||||
|
|
||||||
private let jsonrpc = "2.0"
|
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>
|
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
|
// Create a concrete request from the type-erased one
|
||||||
let data = try JSONEncoder().encode(request.params)
|
let data = try encoder.encode(request.params)
|
||||||
let params = try JSONDecoder().decode(M.Parameters.self, from: data)
|
let params = try decoder.decode(M.Parameters.self, from: data)
|
||||||
let typedRequest = Request<M>(id: request.id, method: M.name, params: params)
|
let typedRequest = Request<M>(id: request.id, method: M.name, params: params)
|
||||||
|
|
||||||
// Handle with concrete type
|
// Handle with concrete type
|
||||||
|
|
@ -141,8 +145,8 @@ final class TypedRequestHandler<M: Method>: RequestHandlerBox, @unchecked Sendab
|
||||||
// Convert result to AnyMethod response
|
// Convert result to AnyMethod response
|
||||||
switch response.result {
|
switch response.result {
|
||||||
case .success(let result):
|
case .success(let result):
|
||||||
let resultData = try JSONEncoder().encode(result)
|
let resultData = try encoder.encode(result)
|
||||||
let resultValue = try JSONDecoder().decode(Value.self, from: resultData)
|
let resultValue = try decoder.decode(Value.self, from: resultData)
|
||||||
return Response(id: response.id, result: resultValue)
|
return Response(id: response.id, result: resultValue)
|
||||||
case .failure(let error):
|
case .failure(let error):
|
||||||
return Response(id: response.id, error: error)
|
return Response(id: response.id, error: error)
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,6 @@ import Logging
|
||||||
import SystemPackage
|
import SystemPackage
|
||||||
|
|
||||||
import struct Foundation.Data
|
import struct Foundation.Data
|
||||||
import struct Foundation.Date
|
|
||||||
import class Foundation.JSONDecoder
|
|
||||||
import class Foundation.JSONEncoder
|
|
||||||
|
|
||||||
/// Protocol defining the transport layer for MCP communication
|
/// Protocol defining the transport layer for MCP communication
|
||||||
public protocol Transport: Actor {
|
public protocol Transport: Actor {
|
||||||
|
|
|
||||||
|
|
@ -205,7 +205,8 @@ public actor Server {
|
||||||
"Error processing message", metadata: ["error": "\(error)"])
|
"Error processing message", metadata: ["error": "\(error)"])
|
||||||
let response = AnyMethod.response(
|
let response = AnyMethod.response(
|
||||||
id: requestID ?? .random,
|
id: requestID ?? .random,
|
||||||
error: error as? Error ?? Error.internalError(error.localizedDescription)
|
error: error as? Error
|
||||||
|
?? Error.internalError(error.localizedDescription)
|
||||||
)
|
)
|
||||||
try? await send(response)
|
try? await send(response)
|
||||||
}
|
}
|
||||||
|
|
@ -260,7 +261,11 @@ public actor Server {
|
||||||
guard let connection = connection else {
|
guard let connection = connection else {
|
||||||
throw Error.internalError("Server connection not initialized")
|
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) {
|
if let responseStr = String(data: responseData, encoding: .utf8) {
|
||||||
try await connection.send(responseStr)
|
try await connection.send(responseStr)
|
||||||
}
|
}
|
||||||
|
|
@ -271,7 +276,12 @@ public actor Server {
|
||||||
guard let connection = connection else {
|
guard let connection = connection else {
|
||||||
throw Error.internalError("Server connection not initialized")
|
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) {
|
if let notificationStr = String(data: notificationData, encoding: .utf8) {
|
||||||
try await connection.send(notificationStr)
|
try await connection.send(notificationStr)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue