Skip to content

Commit e0186b5

Browse files
authored
Configure JSON encoding format options (#21)
1 parent c672358 commit e0186b5

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

Sources/MCP/Base/Messages.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import Foundation
1+
import class Foundation.JSONDecoder
2+
import class Foundation.JSONEncoder
23

34
private let jsonrpc = "2.0"
45

@@ -130,9 +131,12 @@ final class TypedRequestHandler<M: Method>: RequestHandlerBox, @unchecked Sendab
130131

131132
override func callAsFunction(_ request: Request<AnyMethod>) async throws -> Response<AnyMethod>
132133
{
134+
let encoder = JSONEncoder()
135+
let decoder = JSONDecoder()
136+
133137
// Create a concrete request from the type-erased one
134-
let data = try JSONEncoder().encode(request.params)
135-
let params = try JSONDecoder().decode(M.Parameters.self, from: data)
138+
let data = try encoder.encode(request.params)
139+
let params = try decoder.decode(M.Parameters.self, from: data)
136140
let typedRequest = Request<M>(id: request.id, method: M.name, params: params)
137141

138142
// Handle with concrete type
@@ -141,8 +145,8 @@ final class TypedRequestHandler<M: Method>: RequestHandlerBox, @unchecked Sendab
141145
// Convert result to AnyMethod response
142146
switch response.result {
143147
case .success(let result):
144-
let resultData = try JSONEncoder().encode(result)
145-
let resultValue = try JSONDecoder().decode(Value.self, from: resultData)
148+
let resultData = try encoder.encode(result)
149+
let resultValue = try decoder.decode(Value.self, from: resultData)
146150
return Response(id: response.id, result: resultValue)
147151
case .failure(let error):
148152
return Response(id: response.id, error: error)

Sources/MCP/Base/Transports.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import Logging
33
import SystemPackage
44

55
import struct Foundation.Data
6-
import struct Foundation.Date
7-
import class Foundation.JSONDecoder
8-
import class Foundation.JSONEncoder
96

107
/// Protocol defining the transport layer for MCP communication
118
public protocol Transport: Actor {

Sources/MCP/Server/Server.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ public actor Server {
205205
"Error processing message", metadata: ["error": "\(error)"])
206206
let response = AnyMethod.response(
207207
id: requestID ?? .random,
208-
error: error as? Error ?? Error.internalError(error.localizedDescription)
208+
error: error as? Error
209+
?? Error.internalError(error.localizedDescription)
209210
)
210211
try? await send(response)
211212
}
@@ -260,7 +261,11 @@ public actor Server {
260261
guard let connection = connection else {
261262
throw Error.internalError("Server connection not initialized")
262263
}
263-
let responseData = try JSONEncoder().encode(response)
264+
let encoder = JSONEncoder()
265+
encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes]
266+
267+
let responseData = try encoder.encode(response)
268+
264269
if let responseStr = String(data: responseData, encoding: .utf8) {
265270
try await connection.send(responseStr)
266271
}
@@ -271,7 +276,12 @@ public actor Server {
271276
guard let connection = connection else {
272277
throw Error.internalError("Server connection not initialized")
273278
}
274-
let notificationData = try JSONEncoder().encode(notification)
279+
280+
let encoder = JSONEncoder()
281+
encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes]
282+
283+
let notificationData = try encoder.encode(notification)
284+
275285
if let notificationStr = String(data: notificationData, encoding: .utf8) {
276286
try await connection.send(notificationStr)
277287
}

0 commit comments

Comments
 (0)