Skip to content

Commit 80cf7d9

Browse files
ochafikclaude
andcommitted
fix(kotlin-host): handle size-changed notification to resize WebView
The size-changed notification from Apps was being logged but not applied. Now the WebView properly resizes when the App requests a different height. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 114f2bc commit 80cf7d9

File tree

1 file changed

+8
-2
lines changed
  • examples/basic-host-kotlin/src/main/kotlin/com/example/mcpappshost

1 file changed

+8
-2
lines changed

examples/basic-host-kotlin/src/main/kotlin/com/example/mcpappshost/MainActivity.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ fun ToolCallCard(
238238
onToolCall: (suspend (name: String, arguments: Map<String, Any>?) -> String)? = null
239239
) {
240240
var isInputExpanded by remember { mutableStateOf(false) }
241+
var webViewHeight by remember { mutableIntStateOf(toolCall.preferredHeight) }
241242

242243
// Dimmed appearance when destroying (waiting for teardown)
243244
val cardAlpha = if (toolCall.isDestroying) 0.5f else 1f
@@ -308,9 +309,10 @@ fun ToolCallCard(
308309
isDestroying = toolCall.isDestroying,
309310
onTeardownComplete = onCloseComplete,
310311
onToolCall = onToolCall,
312+
onSizeChanged = { height -> webViewHeight = height },
311313
modifier = Modifier
312314
.fillMaxWidth()
313-
.height(toolCall.preferredHeight.dp)
315+
.height(webViewHeight.dp)
314316
)
315317
}
316318
toolCall.state == ToolCallState.State.COMPLETED && toolCall.result != null -> {
@@ -339,6 +341,7 @@ fun McpAppWebView(
339341
isDestroying: Boolean = false,
340342
onTeardownComplete: (() -> Unit)? = null,
341343
onToolCall: (suspend (name: String, arguments: Map<String, Any>?) -> String)? = null,
344+
onSizeChanged: ((height: Int) -> Unit)? = null,
342345
modifier: Modifier = Modifier
343346
) {
344347
val context = LocalContext.current
@@ -348,6 +351,7 @@ fun McpAppWebView(
348351
var initialized by remember { mutableStateOf(false) }
349352
var teardownRequestId by remember { mutableStateOf(0) }
350353
var teardownCompleted by remember { mutableStateOf(false) }
354+
var currentHeight by remember { mutableIntStateOf(toolCall.preferredHeight) }
351355

352356
// Inject bridge script into HTML
353357
val injectedHtml = remember(toolCall.htmlContent) {
@@ -485,8 +489,10 @@ fun McpAppWebView(
485489
"ui/notifications/size-changed" -> {
486490
val params = msg["params"]?.jsonObject
487491
val height = params?.get("height")?.jsonPrimitive?.intOrNull
488-
if (height != null) {
492+
if (height != null && height > 0) {
489493
android.util.Log.i("McpAppWebView", "Size changed: height=$height")
494+
currentHeight = height
495+
onSizeChanged?.invoke(height)
490496
}
491497
}
492498
"ui/message" -> {

0 commit comments

Comments
 (0)