Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
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
13 changes: 5 additions & 8 deletions Sources/MCP/Server/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down