Skip to content

Throw more specific networking errors in NetworkTransport #5

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
Mar 4, 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
15 changes: 13 additions & 2 deletions Sources/MCP/Base/Transports.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@ public actor StdioTransport: Transport {
messageContinuation.yield(message)
}
}
} catch let error as NWError {
if !Task.isCancelled {
logger.error("Network error occurred", metadata: ["error": "\(error)"])
messageContinuation.finish(throwing: MCP.Error.transportError(error))
}
break
} catch {
if !Task.isCancelled {
logger.error("Receive error: \(error)")
Expand All @@ -375,7 +381,6 @@ public actor StdioTransport: Transport {
}

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

return try await withCheckedThrowingContinuation {
Expand All @@ -391,7 +396,13 @@ public actor StdioTransport: Transport {
if !receiveContinuationResumed {
receiveContinuationResumed = true
if let error = error {
continuation.resume(throwing: error)
if let nwError = error as? NWError {
continuation.resume(throwing: MCP.Error.transportError(nwError))
} else {
continuation.resume(
throwing: MCP.Error.internalError("Receive error: \(error)")
)
}
} else if let content = content {
continuation.resume(returning: content)
} else {
Expand Down