Skip to content

Commit 9e6c742

Browse files
committed
Fix #64: Ensure transport sends are bound to Session.current
Wrapped all `transport?.send(notification)` calls in `Session.swift` with `try await self.work { ... }`. This guarantees that `Session.current` is correctly set for multi-client transports (HTTPSSE, TCPBonjour) when fanning out notifications out-of-band (e.g., `broadcastLog`).
1 parent 17812b6 commit 9e6c742

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

Sources/SwiftMCP/Transport/Session.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ extension Session {
227227

228228
let notification = JSONRPCMessage.notification(method: "notifications/progress",
229229
params: params)
230-
try? await transport?.send(notification)
230+
try? await self.work { _ in
231+
try await self.transport?.send(notification)
232+
}
231233
}
232234

233235
/// Send a log message notification to the client associated with this session, filtered by minimumLogLevel.
@@ -242,7 +244,9 @@ extension Session {
242244

243245
let notification = JSONRPCMessage.notification(method: "notifications/message",
244246
params: params)
245-
try? await transport?.send(notification)
247+
try? await self.work { _ in
248+
try await self.transport?.send(notification)
249+
}
246250
}
247251

248252
/// Send a roots/list request to the client and return the roots.
@@ -277,18 +281,24 @@ extension Session {
277281
/// Send a notification that the list of available tools changed.
278282
public func sendToolListChanged() async throws {
279283
let notification = JSONRPCMessage.notification(method: "notifications/tools/list_changed")
280-
try await transport?.send(notification)
284+
try await self.work { _ in
285+
try await self.transport?.send(notification)
286+
}
281287
}
282288

283289
/// Send a notification that the list of available resources changed.
284290
public func sendResourceListChanged() async throws {
285291
let notification = JSONRPCMessage.notification(method: "notifications/resources/list_changed")
286-
try await transport?.send(notification)
292+
try await self.work { _ in
293+
try await self.transport?.send(notification)
294+
}
287295
}
288296

289297
/// Send a notification that the list of available prompts changed.
290298
public func sendPromptListChanged() async throws {
291299
let notification = JSONRPCMessage.notification(method: "notifications/prompts/list_changed")
292-
try await transport?.send(notification)
300+
try await self.work { _ in
301+
try await self.transport?.send(notification)
302+
}
293303
}
294304
}

0 commit comments

Comments
 (0)