Skip to content

Commit 8521b0a

Browse files
committed
Remove top-level MCP enumeration
1 parent 402904e commit 8521b0a

File tree

6 files changed

+39
-46
lines changed

6 files changed

+39
-46
lines changed

Sources/MCP/Base/Error.swift

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,8 @@ 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-
259
/// A model context protocol error.
26-
public enum MCPError: Error, Sendable {
10+
public enum MCPError: Swift.Error, Sendable {
2711
// Standard JSON-RPC 2.0 errors (-32700 to -32603)
2812
case parseError(String?) // -32700
2913
case invalidRequest(String?) // -32600
@@ -36,7 +20,7 @@ public enum MCPError: Error, Sendable {
3620

3721
// Transport specific errors
3822
case connectionClosed
39-
case transportError(Error)
23+
case transportError(Swift.Error)
4024

4125
/// The JSON-RPC 2.0 error code
4226
public var code: Int {
@@ -53,7 +37,7 @@ public enum MCPError: Error, Sendable {
5337
}
5438

5539
/// Check if an error represents a "resource temporarily unavailable" condition
56-
public static func isResourceTemporarilyUnavailable(_ error: Error) -> Bool {
40+
public static func isResourceTemporarilyUnavailable(_ error: Swift.Error) -> Bool {
5741
#if canImport(System)
5842
if let errno = error as? System.Errno, errno == .resourceTemporarilyUnavailable {
5943
return true
@@ -246,3 +230,12 @@ extension MCPError: Hashable {
246230
}
247231
}
248232
}
233+
234+
// MARK: -
235+
236+
/// This is provided to allow existing code that uses `MCP.Error` to continue
237+
/// to work without modification.
238+
///
239+
/// The MCPError type is now the recommended way to handle errors in MCP.
240+
@available(*, deprecated, renamed: "MCPError", message: "Use MCPError instead of MCP.Error")
241+
public typealias Error = MCPError

Sources/MCP/Base/Transports.swift

Lines changed: 12 additions & 12 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, Error>
26+
func receive() -> AsyncThrowingStream<Data, Swift.Error>
2727
}
2828

2929
/// Standard input/output transport implementation
@@ -158,7 +158,7 @@ public actor StdioTransport: Transport {
158158
}
159159
}
160160

161-
public func receive() -> AsyncThrowingStream<Data, Error> {
161+
public func receive() -> AsyncThrowingStream<Data, Swift.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, Error>
183-
private let messageContinuation: AsyncThrowingStream<Data, Error>.Continuation
182+
private let messageStream: AsyncThrowingStream<Data, Swift.Error>
183+
private let messageContinuation: AsyncThrowingStream<Data, Swift.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, Error>.Continuation!
198+
var continuation: AsyncThrowingStream<Data, Swift.Error>.Continuation!
199199
self.messageStream = AsyncThrowingStream { continuation = $0 }
200200
self.messageContinuation = continuation
201201
}
@@ -209,7 +209,7 @@ public actor StdioTransport: Transport {
209209

210210
// Wait for connection to be ready
211211
try await withCheckedThrowingContinuation {
212-
[weak self] (continuation: CheckedContinuation<Void, Error>) in
212+
[weak self] (continuation: CheckedContinuation<Void, Swift.Error>) in
213213
guard let self = self else {
214214
continuation.resume(throwing: MCPError.internalError("Transport deallocated"))
215215
return
@@ -245,7 +245,7 @@ public actor StdioTransport: Transport {
245245
}
246246
}
247247

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

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

271-
private func handleConnectionCancelled(continuation: CheckedContinuation<Void, Error>)
271+
private func handleConnectionCancelled(continuation: CheckedContinuation<Void, Swift.Error>)
272272
async
273273
{
274274
if !connectionContinuationResumed {
@@ -299,7 +299,7 @@ public actor StdioTransport: Transport {
299299
var sendContinuationResumed = false
300300

301301
try await withCheckedThrowingContinuation {
302-
[weak self] (continuation: CheckedContinuation<Void, Error>) in
302+
[weak self] (continuation: CheckedContinuation<Void, Swift.Error>) in
303303
guard let self = self else {
304304
continuation.resume(throwing: MCPError.internalError("Transport deallocated"))
305305
return
@@ -326,7 +326,7 @@ public actor StdioTransport: Transport {
326326
}
327327
}
328328

329-
public func receive() -> AsyncThrowingStream<Data, Error> {
329+
public func receive() -> AsyncThrowingStream<Data, Swift.Error> {
330330
return AsyncThrowingStream { continuation in
331331
Task {
332332
do {
@@ -382,7 +382,7 @@ public actor StdioTransport: Transport {
382382
var receiveContinuationResumed = false
383383

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

Sources/MCP/Client/Client.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,16 @@ public actor Client {
112112
private var task: Task<Void, Never>?
113113

114114
/// An error indicating a type mismatch when decoding a pending request
115-
private struct TypeMismatchError: Error {}
115+
private struct TypeMismatchError: Swift.Error {}
116116

117117
/// A pending request with a continuation for the result
118118
private struct PendingRequest<T> {
119-
let continuation: CheckedContinuation<T, Error>
119+
let continuation: CheckedContinuation<T, Swift.Error>
120120
}
121121

122122
/// A type-erased pending request
123123
private struct AnyPendingRequest {
124-
private let _resume: (Result<Any, Error>) -> Void
124+
private let _resume: (Result<Any, Swift.Error>) -> Void
125125

126126
init<T: Sendable & Decodable>(_ request: PendingRequest<T>) {
127127
_resume = { result in
@@ -146,7 +146,7 @@ public actor Client {
146146
_resume(.success(value))
147147
}
148148

149-
func resume(throwing error: Error) {
149+
func resume(throwing error: Swift.Error) {
150150
_resume(.failure(error))
151151
}
152152
}
@@ -274,7 +274,7 @@ public actor Client {
274274

275275
private func addPendingRequest<T: Sendable & Decodable>(
276276
id: ID,
277-
continuation: CheckedContinuation<T, Error>,
277+
continuation: CheckedContinuation<T, Swift.Error>,
278278
type: T.Type
279279
) {
280280
pendingRequests[id] = AnyPendingRequest(PendingRequest(continuation: continuation))

Tests/MCPTests/ClientTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ struct ClientTests {
132132
try await client.connect(transport: transport)
133133

134134
// Create a task for listPrompts
135-
let promptsTask = Task<Void, Error> {
135+
let promptsTask = Task<Void, Swift.Error> {
136136
do {
137137
_ = try await client.listPrompts()
138138
#expect(Bool(false), "Expected listPrompts to fail in strict mode")

Tests/MCPTests/Helpers/MockTransport.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import class Foundation.JSONEncoder
2-
import class Foundation.JSONDecoder
1+
import Logging
2+
33
import struct Foundation.Data
4+
import class Foundation.JSONDecoder
5+
import class Foundation.JSONEncoder
46
import struct Foundation.POSIXError
57

6-
import Logging
7-
88
@testable import MCP
99

1010
/// Mock transport for testing
@@ -30,7 +30,7 @@ actor MockTransport: Transport {
3030
private var dataToReceive: [Data] = []
3131
private(set) var receivedMessages: [String] = []
3232

33-
private var dataStreamContinuation: AsyncThrowingStream<Data, Error>.Continuation?
33+
private var dataStreamContinuation: AsyncThrowingStream<Data, Swift.Error>.Continuation?
3434

3535
var shouldFailConnect = false
3636
var shouldFailSend = false
@@ -59,8 +59,8 @@ actor MockTransport: Transport {
5959
sentData.append(message)
6060
}
6161

62-
public func receive() -> AsyncThrowingStream<Data, Error> {
63-
return AsyncThrowingStream<Data, Error> { continuation in
62+
public func receive() -> AsyncThrowingStream<Data, Swift.Error> {
63+
return AsyncThrowingStream<Data, Swift.Error> { continuation in
6464
dataStreamContinuation = continuation
6565
for message in dataToReceive {
6666
continuation.yield(message)

Tests/MCPTests/TransportTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct StdioTransportTests {
5757
try writer.close()
5858

5959
// Start receiving messages
60-
let stream: AsyncThrowingStream<Data, Error> = await transport.receive()
60+
let stream: AsyncThrowingStream<Data, Swift.Error> = await transport.receive()
6161
var iterator = stream.makeAsyncIterator()
6262

6363
// Get first message
@@ -79,7 +79,7 @@ struct StdioTransportTests {
7979
try writer.writeAll(invalidJSON.data(using: .utf8)!)
8080
try writer.close()
8181

82-
let stream: AsyncThrowingStream<Data, Error> = await transport.receive()
82+
let stream: AsyncThrowingStream<Data, Swift.Error> = await transport.receive()
8383
var iterator = stream.makeAsyncIterator()
8484

8585
_ = try await iterator.next()

0 commit comments

Comments
 (0)