Skip to content

Commit d0364a9

Browse files
authored
Throw more specific networking errors in NetworkTransport (#5)
1 parent b761d3e commit d0364a9

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Sources/MCP/Base/Transports.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ public actor StdioTransport: Transport {
362362
messageContinuation.yield(message)
363363
}
364364
}
365+
} catch let error as NWError {
366+
if !Task.isCancelled {
367+
logger.error("Network error occurred", metadata: ["error": "\(error)"])
368+
messageContinuation.finish(throwing: MCP.Error.transportError(error))
369+
}
370+
break
365371
} catch {
366372
if !Task.isCancelled {
367373
logger.error("Receive error: \(error)")
@@ -375,7 +381,6 @@ public actor StdioTransport: Transport {
375381
}
376382

377383
private func receiveData() async throws -> Data {
378-
// Use a local actor-isolated variable to track continuation state
379384
var receiveContinuationResumed = false
380385

381386
return try await withCheckedThrowingContinuation {
@@ -391,7 +396,13 @@ public actor StdioTransport: Transport {
391396
if !receiveContinuationResumed {
392397
receiveContinuationResumed = true
393398
if let error = error {
394-
continuation.resume(throwing: error)
399+
if let nwError = error as? NWError {
400+
continuation.resume(throwing: MCP.Error.transportError(nwError))
401+
} else {
402+
continuation.resume(
403+
throwing: MCP.Error.internalError("Receive error: \(error)")
404+
)
405+
}
395406
} else if let content = content {
396407
continuation.resume(returning: content)
397408
} else {

0 commit comments

Comments
 (0)