Skip to content

Commit b2ff5db

Browse files
ochafikclaude
andcommitted
fix(swift-sdk): Fix teardown params and WebView lifecycle
- Send params: [:] instead of nil (TS SDK expects object, not undefined) - Use strong reference for WebView in transport to prevent premature dealloc - Add guard for nil transport with proper error - Add debug logging to sendRequest 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 35d20ff commit b2ff5db

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

swift/Sources/McpApps/AppBridge.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public actor AppBridge {
232232
}
233233

234234
public func sendResourceTeardown() async throws -> McpUiResourceTeardownResult {
235-
_ = try await sendRequest(method: "ui/resource-teardown", params: nil)
235+
_ = try await sendRequest(method: "ui/resource-teardown", params: [:])
236236
return McpUiResourceTeardownResult()
237237
}
238238

@@ -247,12 +247,20 @@ public actor AppBridge {
247247
nextRequestId += 1
248248
let request = JSONRPCRequest(id: id, method: method, params: params)
249249

250+
guard transport != nil else {
251+
print("[AppBridge] sendRequest failed: transport is nil")
252+
throw BridgeError.disconnected
253+
}
254+
250255
return try await withCheckedThrowingContinuation { continuation in
251256
pendingRequests[id] = continuation
252257
Task {
253258
do {
259+
print("[AppBridge] Sending request: \(method)")
254260
try await transport?.send(.request(request))
261+
print("[AppBridge] Request sent successfully, waiting for response...")
255262
} catch {
263+
print("[AppBridge] Transport send failed: \(error)")
256264
pendingRequests.removeValue(forKey: id)?.resume(throwing: error)
257265
}
258266
}

swift/Sources/McpApps/Transport/WKWebViewTransport.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public actor WKWebViewTransport: McpAppsTransport {
2525
private var messageHandler: MessageHandlerProxy?
2626

2727
// Store webView reference for MainActor access
28-
@MainActor private weak var webView: WKWebView?
28+
// Note: Using strong reference to ensure webView stays alive for teardown messages
29+
@MainActor private var webView: WKWebView?
2930

3031
public let incoming: AsyncThrowingStream<JSONRPCMessage, Error>
3132

@@ -109,6 +110,7 @@ public actor WKWebViewTransport: McpAppsTransport {
109110
await MainActor.run {
110111
webView?.configuration.userContentController.removeScriptMessageHandler(forName: name)
111112
webView?.configuration.userContentController.removeAllUserScripts()
113+
webView = nil // Release strong reference
112114
}
113115

114116
messageHandler = nil

0 commit comments

Comments
 (0)