Skip to content

Commit 09da2a1

Browse files
authored
Rename Error to MCPError (#44)
* Rename Error to MCPError * Conform MCPError to Swift.Error * Use unqualified Error instead of Swift.Error * Use unqualified MCPError instead of MCP.MCPError * Rename file back to Error.swift * Introduce top-level MCP namespace with nested Error typealias to provide deprecation notice for existing usage of MCP.Error * Fix markup for MCP enum warning
1 parent a9bcbf9 commit 09da2a1

File tree

10 files changed

+108
-88
lines changed

10 files changed

+108
-88
lines changed

Sources/MCP/Base/Error.swift

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,24 @@ import Foundation
66
@preconcurrency import SystemPackage
77
#endif
88

9+
/// Top-level namespace for backward compatibility
10+
///
11+
/// This is provided to allow existing code that uses `MCP.Error` to continue
12+
/// to work without modification.
13+
///
14+
/// The MCPError type is now the recommended way to handle errors in MCP.
15+
///
16+
/// - Warning: This namespace is deprecated and will be removed in a future version.
17+
public enum MCP {
18+
/// Deprecated type alias for MCPError
19+
@available(*, deprecated, renamed: "MCPError", message: "Use MCPError instead of MCP.Error")
20+
public typealias Error = MCPError
21+
}
22+
23+
// MARK: -
24+
925
/// A model context protocol error.
10-
public enum Error: Sendable {
26+
public enum MCPError: Error, Sendable {
1127
// Standard JSON-RPC 2.0 errors (-32700 to -32603)
1228
case parseError(String?) // -32700
1329
case invalidRequest(String?) // -32600
@@ -20,7 +36,7 @@ public enum Error: Sendable {
2036

2137
// Transport specific errors
2238
case connectionClosed
23-
case transportError(Swift.Error)
39+
case transportError(Error)
2440

2541
/// The JSON-RPC 2.0 error code
2642
public var code: Int {
@@ -37,7 +53,7 @@ public enum Error: Sendable {
3753
}
3854

3955
/// Check if an error represents a "resource temporarily unavailable" condition
40-
public static func isResourceTemporarilyUnavailable(_ error: Swift.Error) -> Bool {
56+
public static func isResourceTemporarilyUnavailable(_ error: Error) -> Bool {
4157
#if canImport(System)
4258
if let errno = error as? System.Errno, errno == .resourceTemporarilyUnavailable {
4359
return true
@@ -53,7 +69,7 @@ public enum Error: Sendable {
5369

5470
// MARK: LocalizedError
5571

56-
extension Error: LocalizedError {
72+
extension MCPError: LocalizedError {
5773
public var errorDescription: String? {
5874
switch self {
5975
case .parseError(let detail):
@@ -116,7 +132,7 @@ extension Error: LocalizedError {
116132

117133
// MARK: CustomDebugStringConvertible
118134

119-
extension Error: CustomDebugStringConvertible {
135+
extension MCPError: CustomDebugStringConvertible {
120136
public var debugDescription: String {
121137
switch self {
122138
case .transportError(let error):
@@ -131,7 +147,7 @@ extension Error: CustomDebugStringConvertible {
131147

132148
// MARK: Codable
133149

134-
extension Error: Codable {
150+
extension MCPError: Codable {
135151
private enum CodingKeys: String, CodingKey {
136152
case code, message, data
137153
}
@@ -199,15 +215,15 @@ extension Error: Codable {
199215

200216
// MARK: Equatable
201217

202-
extension Error: Equatable {
203-
public static func == (lhs: Error, rhs: Error) -> Bool {
218+
extension MCPError: Equatable {
219+
public static func == (lhs: MCPError, rhs: MCPError) -> Bool {
204220
lhs.code == rhs.code
205221
}
206222
}
207223

208224
// MARK: Hashable
209225

210-
extension Error: Hashable {
226+
extension MCPError: Hashable {
211227
public func hash(into hasher: inout Hasher) {
212228
hasher.combine(code)
213229
switch self {

Sources/MCP/Base/Messages.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ extension Method {
6060
}
6161

6262
/// Create a response with the given error.
63-
public static func response(id: ID, error: Error) -> Response<Self> {
63+
public static func response(id: ID, error: MCPError) -> Response<Self> {
6464
Response(id: id, error: error)
6565
}
6666
}
@@ -186,14 +186,14 @@ public struct Response<M: Method>: Hashable, Identifiable, Codable, Sendable {
186186
/// The response ID.
187187
public let id: ID
188188
/// The response result.
189-
public let result: Swift.Result<M.Result, Error>
189+
public let result: Swift.Result<M.Result, MCPError>
190190

191191
public init(id: ID, result: M.Result) {
192192
self.id = id
193193
self.result = .success(result)
194194
}
195195

196-
public init(id: ID, error: Error) {
196+
public init(id: ID, error: MCPError) {
197197
self.id = id
198198
self.result = .failure(error)
199199
}
@@ -224,7 +224,7 @@ public struct Response<M: Method>: Hashable, Identifiable, Codable, Sendable {
224224
id = try container.decode(ID.self, forKey: .id)
225225
if let result = try? container.decode(M.Result.self, forKey: .result) {
226226
self.result = .success(result)
227-
} else if let error = try? container.decode(Error.self, forKey: .error) {
227+
} else if let error = try? container.decode(MCPError.self, forKey: .error) {
228228
self.result = .failure(error)
229229
} else {
230230
throw DecodingError.dataCorrupted(

Sources/MCP/Base/Transports.swift

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public protocol Transport: Actor {
2323
func send(_ data: Data) async throws
2424

2525
/// Receives data in an async sequence
26-
func receive() -> AsyncThrowingStream<Data, Swift.Error>
26+
func receive() -> AsyncThrowingStream<Data, Error>
2727
}
2828

2929
/// Standard input/output transport implementation
@@ -74,11 +74,11 @@ public actor StdioTransport: Transport {
7474
private func setNonBlocking(fileDescriptor: FileDescriptor) throws {
7575
let flags = fcntl(fileDescriptor.rawValue, F_GETFL)
7676
guard flags >= 0 else {
77-
throw Error.transportError(Errno.badFileDescriptor)
77+
throw MCPError.transportError(Errno.badFileDescriptor)
7878
}
7979
let result = fcntl(fileDescriptor.rawValue, F_SETFL, flags | O_NONBLOCK)
8080
guard result >= 0 else {
81-
throw Error.transportError(Errno.badFileDescriptor)
81+
throw MCPError.transportError(Errno.badFileDescriptor)
8282
}
8383
}
8484

@@ -110,7 +110,7 @@ public actor StdioTransport: Transport {
110110
messageContinuation.yield(Data(messageData))
111111
}
112112
}
113-
} catch let error where Error.isResourceTemporarilyUnavailable(error) {
113+
} catch let error where MCPError.isResourceTemporarilyUnavailable(error) {
114114
try? await Task.sleep(for: .milliseconds(10))
115115
continue
116116
} catch {
@@ -133,7 +133,7 @@ public actor StdioTransport: Transport {
133133

134134
public func send(_ message: Data) async throws {
135135
guard isConnected else {
136-
throw Error.transportError(Errno.socketNotConnected)
136+
throw MCPError.transportError(Errno.socketNotConnected)
137137
}
138138

139139
// Add newline as delimiter
@@ -149,16 +149,16 @@ public actor StdioTransport: Transport {
149149
if written > 0 {
150150
remaining = remaining.dropFirst(written)
151151
}
152-
} catch let error where Error.isResourceTemporarilyUnavailable(error) {
152+
} catch let error where MCPError.isResourceTemporarilyUnavailable(error) {
153153
try await Task.sleep(for: .milliseconds(10))
154154
continue
155155
} catch {
156-
throw Error.transportError(error)
156+
throw MCPError.transportError(error)
157157
}
158158
}
159159
}
160160

161-
public func receive() -> AsyncThrowingStream<Data, Swift.Error> {
161+
public func receive() -> AsyncThrowingStream<Data, Error> {
162162
return AsyncThrowingStream { continuation in
163163
Task {
164164
for await message in messageStream {
@@ -179,8 +179,8 @@ public actor StdioTransport: Transport {
179179
public nonisolated let logger: Logger
180180

181181
private var isConnected = false
182-
private let messageStream: AsyncThrowingStream<Data, Swift.Error>
183-
private let messageContinuation: AsyncThrowingStream<Data, Swift.Error>.Continuation
182+
private let messageStream: AsyncThrowingStream<Data, Error>
183+
private let messageContinuation: AsyncThrowingStream<Data, Error>.Continuation
184184

185185
// Track connection state for continuations
186186
private var connectionContinuationResumed = false
@@ -195,7 +195,7 @@ public actor StdioTransport: Transport {
195195
)
196196

197197
// Create message stream
198-
var continuation: AsyncThrowingStream<Data, Swift.Error>.Continuation!
198+
var continuation: AsyncThrowingStream<Data, Error>.Continuation!
199199
self.messageStream = AsyncThrowingStream { continuation = $0 }
200200
self.messageContinuation = continuation
201201
}
@@ -209,9 +209,9 @@ public actor StdioTransport: Transport {
209209

210210
// Wait for connection to be ready
211211
try await withCheckedThrowingContinuation {
212-
[weak self] (continuation: CheckedContinuation<Void, Swift.Error>) in
212+
[weak self] (continuation: CheckedContinuation<Void, Error>) in
213213
guard let self = self else {
214-
continuation.resume(throwing: MCP.Error.internalError("Transport deallocated"))
214+
continuation.resume(throwing: MCPError.internalError("Transport deallocated"))
215215
return
216216
}
217217

@@ -245,7 +245,7 @@ public actor StdioTransport: Transport {
245245
}
246246
}
247247

248-
private func handleConnectionReady(continuation: CheckedContinuation<Void, Swift.Error>)
248+
private func handleConnectionReady(continuation: CheckedContinuation<Void, Error>)
249249
async
250250
{
251251
if !connectionContinuationResumed {
@@ -259,7 +259,7 @@ public actor StdioTransport: Transport {
259259
}
260260

261261
private func handleConnectionFailed(
262-
error: Swift.Error, continuation: CheckedContinuation<Void, Swift.Error>
262+
error: Error, continuation: CheckedContinuation<Void, Error>
263263
) async {
264264
if !connectionContinuationResumed {
265265
connectionContinuationResumed = true
@@ -268,13 +268,13 @@ public actor StdioTransport: Transport {
268268
}
269269
}
270270

271-
private func handleConnectionCancelled(continuation: CheckedContinuation<Void, Swift.Error>)
271+
private func handleConnectionCancelled(continuation: CheckedContinuation<Void, Error>)
272272
async
273273
{
274274
if !connectionContinuationResumed {
275275
connectionContinuationResumed = true
276276
logger.warning("Connection cancelled")
277-
continuation.resume(throwing: MCP.Error.internalError("Connection cancelled"))
277+
continuation.resume(throwing: MCPError.internalError("Connection cancelled"))
278278
}
279279
}
280280

@@ -288,7 +288,7 @@ public actor StdioTransport: Transport {
288288

289289
public func send(_ message: Data) async throws {
290290
guard isConnected else {
291-
throw MCP.Error.internalError("Transport not connected")
291+
throw MCPError.internalError("Transport not connected")
292292
}
293293

294294
// Add newline as delimiter
@@ -299,9 +299,9 @@ public actor StdioTransport: Transport {
299299
var sendContinuationResumed = false
300300

301301
try await withCheckedThrowingContinuation {
302-
[weak self] (continuation: CheckedContinuation<Void, Swift.Error>) in
302+
[weak self] (continuation: CheckedContinuation<Void, Error>) in
303303
guard let self = self else {
304-
continuation.resume(throwing: MCP.Error.internalError("Transport deallocated"))
304+
continuation.resume(throwing: MCPError.internalError("Transport deallocated"))
305305
return
306306
}
307307

@@ -316,7 +316,7 @@ public actor StdioTransport: Transport {
316316
if let error = error {
317317
self.logger.error("Send error: \(error)")
318318
continuation.resume(
319-
throwing: MCP.Error.internalError("Send error: \(error)"))
319+
throwing: MCPError.internalError("Send error: \(error)"))
320320
} else {
321321
continuation.resume()
322322
}
@@ -326,7 +326,7 @@ public actor StdioTransport: Transport {
326326
}
327327
}
328328

329-
public func receive() -> AsyncThrowingStream<Data, Swift.Error> {
329+
public func receive() -> AsyncThrowingStream<Data, Error> {
330330
return AsyncThrowingStream { continuation in
331331
Task {
332332
do {
@@ -363,7 +363,7 @@ public actor StdioTransport: Transport {
363363
} catch let error as NWError {
364364
if !Task.isCancelled {
365365
logger.error("Network error occurred", metadata: ["error": "\(error)"])
366-
messageContinuation.finish(throwing: MCP.Error.transportError(error))
366+
messageContinuation.finish(throwing: MCPError.transportError(error))
367367
}
368368
break
369369
} catch {
@@ -382,9 +382,9 @@ public actor StdioTransport: Transport {
382382
var receiveContinuationResumed = false
383383

384384
return try await withCheckedThrowingContinuation {
385-
[weak self] (continuation: CheckedContinuation<Data, Swift.Error>) in
385+
[weak self] (continuation: CheckedContinuation<Data, Error>) in
386386
guard let self = self else {
387-
continuation.resume(throwing: MCP.Error.internalError("Transport deallocated"))
387+
continuation.resume(throwing: MCPError.internalError("Transport deallocated"))
388388
return
389389
}
390390

@@ -394,12 +394,12 @@ public actor StdioTransport: Transport {
394394
if !receiveContinuationResumed {
395395
receiveContinuationResumed = true
396396
if let error = error {
397-
continuation.resume(throwing: MCP.Error.transportError(error))
397+
continuation.resume(throwing: MCPError.transportError(error))
398398
} else if let content = content {
399399
continuation.resume(returning: content)
400400
} else {
401401
continuation.resume(
402-
throwing: MCP.Error.internalError("No data received"))
402+
throwing: MCPError.internalError("No data received"))
403403
}
404404
}
405405
}

0 commit comments

Comments
 (0)