From 5d58f7763e9de3fff5e7785350dfe04c7a315290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Hanke?= Date: Wed, 5 Aug 2026 19:57:20 +0200 Subject: [PATCH] Use canImport guards for EventSource/FoundationNetworking in HTTPClientTransport The package manifest already limits the EventSource dependency to Apple platforms, but HTTPClientTransport.swift gates its import and the SSE code paths on `#if !os(Linux)`. On any other non-Apple platform (e.g. x86_64-windows-msvc) the module is absent yet the import is still compiled, so the whole MCP target fails with: HTTPClientTransport.swift:5:12: error: no such module 'EventSource' Gate on capabilities instead of naming Linux: - `#if canImport(EventSource)` for the import, the SSE listen loop, and connectToEventStream -- selects exactly the same code on Apple and Linux as before - `#if canImport(FoundationNetworking)` for the data(for:) vs bytes(for:) split and the matching processResponse overloads, since URLSession.AsyncBytes only exists in Darwin Foundation - reword the two log messages that named Linux No behavior change on Apple or Linux; the MCP target now compiles for x86_64-unknown-windows-msvc (verified with Swift 6.3.3). --- .../MCP/Base/Transports/HTTPClientTransport.swift | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Sources/MCP/Base/Transports/HTTPClientTransport.swift b/Sources/MCP/Base/Transports/HTTPClientTransport.swift index 060cd133..d0dbbdb8 100644 --- a/Sources/MCP/Base/Transports/HTTPClientTransport.swift +++ b/Sources/MCP/Base/Transports/HTTPClientTransport.swift @@ -1,7 +1,7 @@ import Foundation import Logging -#if !os(Linux) +#if canImport(EventSource) import EventSource #endif @@ -266,7 +266,7 @@ public actor HTTPClientTransport: Transport { request = requestModifier(request) do { - #if os(Linux) + #if canImport(FoundationNetworking) let (responseData, response) = try await session.data(for: request) try await processResponse(response: response, data: responseData) #else @@ -304,7 +304,7 @@ public actor HTTPClientTransport: Transport { } } - #if os(Linux) + #if canImport(FoundationNetworking) private func processResponse(response: URLResponse, data: Data) async throws { guard let httpResponse = response as? HTTPURLResponse else { throw MCPError.internalError("Invalid HTTP response") @@ -325,7 +325,7 @@ public actor HTTPClientTransport: Transport { guard case 200..<300 = httpResponse.statusCode else { return } if contentType.contains(ContentType.sse) { - logger.warning("SSE responses aren't fully supported on Linux") + logger.warning("SSE responses aren't fully supported on this platform") messageContinuation.yield(data) } else if contentType.contains(ContentType.json) { logger.trace("Received JSON response", metadata: ["size": "\(data.count)"]) @@ -467,10 +467,10 @@ public actor HTTPClientTransport: Transport { // MARK: - SSE private func startListeningForServerEvents() async { - #if os(Linux) + #if !canImport(EventSource) if streaming { logger.warning( - "SSE streaming was requested but is not fully supported on Linux. SSE connection will not be attempted." + "SSE streaming was requested but is not fully supported on this platform. SSE connection will not be attempted." ) } #else @@ -563,7 +563,7 @@ public actor HTTPClientTransport: Transport { #endif } - #if !os(Linux) + #if canImport(EventSource) private func connectToEventStream() async throws { guard isConnected else { logger.debug("⚠️ Skipping connectToEventStream - transport not connected")