Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions Sources/MCP/Base/Messages.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import class Foundation.JSONDecoder
import class Foundation.JSONEncoder

private let jsonrpc = "2.0"

Expand Down Expand Up @@ -131,9 +132,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
Expand All @@ -142,8 +146,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)
Expand Down
3 changes: 0 additions & 3 deletions Sources/MCP/Base/Transports.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 13 additions & 3 deletions Sources/MCP/Server/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down