@@ -48,6 +48,12 @@ import Logging
48
48
/// // Initialize the transport with the connection
49
49
/// let transport = NetworkTransport(connection: connection)
50
50
///
51
+ /// // For large messages (e.g., images), configure unlimited buffer size
52
+ /// let largeBufferTransport = NetworkTransport(
53
+ /// connection: connection,
54
+ /// bufferConfig: .unlimited
55
+ /// )
56
+ ///
51
57
/// // Use the transport with an MCP client
52
58
/// let client = Client(name: "MyApp", version: "1.0.0")
53
59
/// try await client.connect(transport: transport)
@@ -207,6 +213,26 @@ import Logging
207
213
}
208
214
}
209
215
216
+ /// Configuration for buffer behavior.
217
+ public struct BufferConfiguration : Hashable , Sendable {
218
+ /// Maximum buffer size for receiving data chunks.
219
+ /// Set to nil for unlimited (uses system default).
220
+ public let maxReceiveBufferSize : Int ?
221
+
222
+ /// Creates a new buffer configuration.
223
+ ///
224
+ /// - Parameter maxReceiveBufferSize: Maximum buffer size in bytes (default: 10MB, nil for unlimited)
225
+ public init ( maxReceiveBufferSize: Int ? = 10 * 1024 * 1024 ) {
226
+ self . maxReceiveBufferSize = maxReceiveBufferSize
227
+ }
228
+
229
+ /// Default buffer configuration with 10MB limit.
230
+ public static let `default` = BufferConfiguration ( )
231
+
232
+ /// Configuration with no buffer size limit.
233
+ public static let unlimited = BufferConfiguration ( maxReceiveBufferSize: nil )
234
+ }
235
+
210
236
// State tracking
211
237
private var isConnected = false
212
238
private var isStopping = false
@@ -228,6 +254,7 @@ import Logging
228
254
// Configuration
229
255
private let heartbeatConfig : HeartbeatConfiguration
230
256
private let reconnectionConfig : ReconnectionConfiguration
257
+ private let bufferConfig : BufferConfiguration
231
258
232
259
/// Creates a new NetworkTransport with the specified NWConnection
233
260
///
@@ -236,25 +263,29 @@ import Logging
236
263
/// - logger: Optional logger instance for transport events
237
264
/// - reconnectionConfig: Configuration for reconnection behavior (default: .default)
238
265
/// - heartbeatConfig: Configuration for heartbeat behavior (default: .default)
266
+ /// - bufferConfig: Configuration for buffer behavior (default: .default)
239
267
public init (
240
268
connection: NWConnection ,
241
269
logger: Logger ? = nil ,
242
270
heartbeatConfig: HeartbeatConfiguration = . default,
243
- reconnectionConfig: ReconnectionConfiguration = . default
271
+ reconnectionConfig: ReconnectionConfiguration = . default,
272
+ bufferConfig: BufferConfiguration = . default
244
273
) {
245
274
self . init (
246
275
connection,
247
276
logger: logger,
248
277
heartbeatConfig: heartbeatConfig,
249
- reconnectionConfig: reconnectionConfig
278
+ reconnectionConfig: reconnectionConfig,
279
+ bufferConfig: bufferConfig
250
280
)
251
281
}
252
282
253
283
init (
254
284
_ connection: NetworkConnectionProtocol ,
255
285
logger: Logger ? = nil ,
256
286
heartbeatConfig: HeartbeatConfiguration = . default,
257
- reconnectionConfig: ReconnectionConfiguration = . default
287
+ reconnectionConfig: ReconnectionConfiguration = . default,
288
+ bufferConfig: BufferConfiguration = . default
258
289
) {
259
290
self . connection = connection
260
291
self . logger =
@@ -265,6 +296,7 @@ import Logging
265
296
)
266
297
self . reconnectionConfig = reconnectionConfig
267
298
self . heartbeatConfig = heartbeatConfig
299
+ self . bufferConfig = bufferConfig
268
300
269
301
// Create message stream
270
302
var continuation : AsyncThrowingStream < Data , Swift . Error > . Continuation !
@@ -773,7 +805,8 @@ import Logging
773
805
return
774
806
}
775
807
776
- connection. receive ( minimumIncompleteLength: 1 , maximumLength: 65536 ) {
808
+ let maxLength = bufferConfig. maxReceiveBufferSize ?? Int . max
809
+ connection. receive ( minimumIncompleteLength: 1 , maximumLength: maxLength) {
777
810
content, _, isComplete, error in
778
811
Task { @MainActor in
779
812
if !receiveContinuationResumed {
0 commit comments