Don't throw error if there's a protocol mismatch between client and server (#103)

This commit is contained in:
Mattt 2025-05-06 11:53:02 -07:00 • committed by GitHub
commit 800131fb9c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -337,7 +337,8 @@ public actor Server {
} catch {
// Only add errors to response for requests (notifications don't have responses)
if case .request(let request) = item {
let mcpError = error as? MCPError ?? MCPError.internalError(error.localizedDescription)
let mcpError =
error as? MCPError ?? MCPError.internalError(error.localizedDescription)
responses.append(AnyMethod.response(id: request.id, error: mcpError))
}
}
@ -365,7 +366,9 @@ public actor Server {
/// - request: The request to handle
/// - sendResponse: Whether to send the response immediately (true) or return it (false)
/// - Returns: The response when sendResponse is false
private func handleRequest(_ request: Request<AnyMethod>, sendResponse: Bool = true) async throws -> Response<AnyMethod>? {
private func handleRequest(_ request: Request<AnyMethod>, sendResponse: Bool = true)
async throws -> Response<AnyMethod>?
{
// Check if this is a pre-processed error request (empty method)
if request.method.isEmpty && !sendResponse {
// This is a placeholder for an invalid request that couldn't be parsed in batch mode
@ -478,12 +481,6 @@ public actor Server {
throw MCPError.invalidRequest("Server is already initialized")
}
// Validate protocol version
guard Version.latest == params.protocolVersion else {
throw MCPError.invalidRequest(
"Unsupported protocol version: \(params.protocolVersion)")
}
// Call initialization hook if registered
if let hook = initializeHook {
try await hook(params.clientInfo, params.capabilities)