Skip to content

Commit 6271126

Browse files
ochafikclaude
andcommitted
fix(swift-host): update example to use new typed params API
Updated McpHostViewModel to use the new typed params API: - onMessage, onOpenLink, onLoggingMessage, onSizeChange now take typed params - sendToolInput now takes McpUiToolInputParams - sendToolResult now takes McpUiToolResultParams with proper content items 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 72c8b82 commit 6271126

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

examples/basic-host-swift/Sources/BasicHostApp/McpHostViewModel.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -398,37 +398,38 @@ class ToolCallInfo: ObservableObject, Identifiable {
398398
bridge.onInitialized = { [weak self] in
399399
Task { @MainActor in
400400
guard let self = self else { return }
401-
try? await bridge.sendToolInput(
401+
let params = McpUiToolInputParams(
402402
arguments: self.input.mapValues { AnyCodable($0) }
403403
)
404+
try? await bridge.sendToolInput(params)
404405
if let result = self.result {
405406
try? await self.sendToolResult(result, to: bridge)
406407
}
407408
}
408409
}
409410

410-
bridge.onMessage = { role, content in
411-
print("[Host] Message from Guest UI: \(role)")
411+
bridge.onMessage = { params in
412+
print("[Host] Message from Guest UI: \(params.role)")
412413
return McpUiMessageResult(isError: false)
413414
}
414415

415-
bridge.onOpenLink = { url in
416-
print("[Host] Open link request: \(url)")
417-
if let urlObj = URL(string: url) {
416+
bridge.onOpenLink = { params in
417+
print("[Host] Open link request: \(params.url)")
418+
if let urlObj = URL(string: params.url) {
418419
await MainActor.run {
419420
UIApplication.shared.open(urlObj)
420421
}
421422
}
422423
return McpUiOpenLinkResult(isError: false)
423424
}
424425

425-
bridge.onLoggingMessage = { level, data, logger in
426-
print("[Host] Guest UI log [\(level)]: \(data.value)")
426+
bridge.onLoggingMessage = { params in
427+
print("[Host] Guest UI log [\(params.level)]: \(params.data.value)")
427428
}
428429

429-
bridge.onSizeChange = { [weak self] width, height in
430-
print("[Host] Size change: \(width ?? 0) x \(height ?? 0)")
431-
if let height = height {
430+
bridge.onSizeChange = { [weak self] params in
431+
print("[Host] Size change: \(params.width ?? 0) x \(params.height ?? 0)")
432+
if let height = params.height {
432433
Task { @MainActor in
433434
self?.preferredHeight = CGFloat(height)
434435
}
@@ -486,19 +487,18 @@ class ToolCallInfo: ObservableObject, Identifiable {
486487
}
487488

488489
private func sendToolResult(_ result: ToolResult, to bridge: AppBridge) async throws {
489-
try await bridge.sendToolResult([
490-
"content": AnyCodable(result.content.map { c -> [String: Any] in
491-
switch c {
492-
case .text(let text):
493-
return ["type": "text", "text": text]
494-
case .image(let data, let mimeType, _):
495-
return ["type": "image", "data": data, "mimeType": mimeType]
496-
default:
497-
return ["type": "text", "text": ""]
498-
}
499-
}),
500-
"isError": AnyCodable(result.isError ?? false)
501-
])
490+
let contentItems: [McpUiToolResultNotificationParamsContentItem] = result.content.compactMap { c in
491+
switch c {
492+
case .text(let text):
493+
return .text(McpUiToolResultNotificationParamsContentItemText(type: "text", text: text))
494+
case .image(let data, let mimeType, _):
495+
return .image(McpUiToolResultNotificationParamsContentItemImage(type: "image", data: data, mimeType: mimeType))
496+
default:
497+
return nil
498+
}
499+
}
500+
let params = McpUiToolResultParams(content: contentItems, isError: result.isError)
501+
try await bridge.sendToolResult(params)
502502
}
503503

504504
/// Teardown the app bridge before removing the tool call

0 commit comments

Comments
 (0)