adk-surface 3.0.0

Agent surfaces for ADK-Rust: one engine that turns a kernel's sessions, decisions and activity into plain words for every surface, and the surfaces that show them
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
//! The MCP Apps host (spec 3.3 §6.1 `genui-apps`, wave 4 decisions 15–17): an MCP server's
//! `_meta.ui` app shown in a thread, sandboxed on its own origin, which can never approve
//! anything.
//!
//! | Step | What the host does |
//! |---|---|
//! | Projection | A tool call whose event carries the MCP tool's own `adk.mcp.app` record becomes [`ThreadItem::App`](crate::ThreadItem::App) |
//! | Open | Reads the `ui://` resource from the server that published the tool, checks its MIME type, checks every declared site (nothing on this computer, its network, the home or the page's own origin, nor a wildcard over them), scans the HTML for a decision's words and undeclared sites, and frames it on the apps origin under a CSP built from what passed, which only the home's page may frame |
//! | `tools/call` | Only a tool the same server publishes for both the model and the app, a tool without `_meta.ui` included, by the name the agent knows that tool by, asked for as a requested call held by the agent whose call showed the app and answered once the agent has held or refused it; an app-only tool is "This app needs a tool this home can't run yet"; the same call again is the same decision, and an app with three calls waiting asks for no more |
//! | `ui/message` | "The app wants to send: …" with **Send it** and **Not now**; sent as the person's message inside an untrusted-content block labelled "Suggested by the Weather app", which every surface shows as the message and that label |
//! | `ui/open-link` | "Open example.com?", opened only by the person's press; a link to this computer or its network is refused |
//! | `ui/download-file` | "The Weather app wants to save forecast.csv (2 KB)", saved only by the person's press: an embedded resource up to 5 MB, its name sanitized, never a file that runs or opens as a page |
//! | Origins | With a `localhost` apps origin, each server's apps on an origin of their own under it ([`McpAppsHostBuilder::origin_per_server`]); `ui/initialize` reports what the frame was granted (`hostCapabilities.sandbox`) |
//! | `ui/update-model-context` | Logged, never forwarded to the model |
//! | Anything else | "not supported here" |
//!
//! The apps origin is a second loopback listener ([`McpAppsHost::sandbox_router`]) or a remote
//! home's `--apps-origin`; the surface API refuses every request from it
//! ([`refuse_apps_origin`](crate::web::genui::refuse_apps_origin)). Without an apps origin, on
//! the phone and in the terminal, an app is its text result and "Open the home on this
//! computer to see this app".

mod scan;
mod sites;

use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use adk_core::{Event, Part, RequestedCall, RequestedVia, RequestedViaKind};
use adk_tool::mcp::{
    MCP_APP_STATE_KEY, MCP_APPS_VERSION, McpAppRecord, McpAppResource, McpServerManager,
    McpUiBinding, McpUiVisibility, ResourceContents,
};
use async_trait::async_trait;
use axum::Router;
use axum::extract::{Path, State};
use axum::http::{HeaderValue, StatusCode, header};
use axum::response::{IntoResponse, Response};
use axum::routing::get;
use serde_json::{Value, json};

use super::app_view::app_name;
use super::{AppAnswer, AppBlockReason, AppError, AppOpen, AppPrompt, AppRequest, AppView};
use crate::engine::{Command, CommandOutcome, SurfaceEngine};
use crate::error::{Result, SurfaceError};
use crate::model::{DecisionCard, ThreadItem};
use crate::origin::{Caller, SurfaceKind};
use crate::project::Projector;
use crate::words;

/// Audit record: an app was not framed, with its reason.
pub const SURFACE_AUDIT_APP_BLOCKED: &str = "surface.app.blocked";
/// Audit record: an app's tool call was refused, by the home or by the agent.
pub const SURFACE_AUDIT_APP_CALL_REFUSED: &str = "surface.app.call_refused";
/// Audit record: an app's tool call was asked of the agent whose call showed the app.
pub const SURFACE_AUDIT_APP_CALL_ASKED: &str = "surface.app.call_asked";
/// Audit record: the agent held an app's tool call for the person's decision.
pub const SURFACE_AUDIT_APP_CALL_HELD: &str = "surface.app.call_held";
/// Audit record: requests from the apps origin, or an app's opaque frame, were refused
/// (at most one record a minute, with the count).
pub const SURFACE_AUDIT_APP_ORIGIN_REFUSED: &str = "surface.app.origin_refused";
/// Audit record: the person sent a message an app suggested.
pub const SURFACE_AUDIT_APP_MESSAGE_SENT: &str = "surface.app.message_sent";

/// The tallest an app's frame grows, in CSS pixels.
const MAX_HEIGHT: u32 = 640;
/// How long a framed app's page stays available on the apps origin.
const FRAME_LIFETIME: Duration = Duration::from_secs(15 * 60);
/// The longest message an app may suggest, in characters.
const MAX_MESSAGE_CHARS: usize = 4_000;
/// The largest view state an app may ask the host to keep.
const MAX_VIEW_STATE_BYTES: usize = 64 * 1024;
/// How many apps' grants are kept before the oldest are forgotten.
const MAX_GRANTS_KEPT: usize = 1_024;
/// JSON-RPC: the method is not offered here.
const METHOD_NOT_FOUND: i64 = -32_601;
/// JSON-RPC: the parameters are not what the method takes.
const INVALID_PARAMS: i64 = -32_602;
/// JSON-RPC: the home refused the request.
const REFUSED: i64 = -32_000;
/// The most calls one app may have waiting for the person at once.
const MAX_WAITING: usize = 3;
/// How long an app's tool call waits for the agent to hold or refuse it before the app is
/// answered anyway.
const OUTCOME_WAIT: Duration = Duration::from_secs(10);
/// How often the thread is read for that outcome.
const OUTCOME_POLL: Duration = Duration::from_millis(50);

/// The MCP servers whose apps a home shows (spec 3.3 §6.1 `genui-apps`): each server's tool
/// bindings and its `ui://` resources. [`McpServerManager`] is one.
#[async_trait]
pub trait AppServers: Send + Sync {
    /// Every `_meta.ui` binding `server` publishes, the app-only tools included; `None` when
    /// this set does not run `server`.
    async fn ui_bindings(&self, server: &str) -> Option<adk_core::Result<Vec<McpUiBinding>>>;

    /// The contents of `server`'s resource `uri`.
    async fn read_resource(
        &self,
        server: &str,
        uri: &str,
    ) -> adk_core::Result<Vec<ResourceContents>>;

    /// Who may call each tool `server` publishes, by its name on the server: its
    /// `_meta.ui.visibility`, or the model and the app when it declares none; `None` when this
    /// set does not run `server`. The default reads [`ui_bindings`](Self::ui_bindings), which
    /// lists only the tools with an app.
    async fn tool_visibility(
        &self,
        server: &str,
    ) -> Option<adk_core::Result<Vec<(String, McpUiVisibility)>>> {
        let bindings = self.ui_bindings(server).await?;
        Some(bindings.map(|bindings| {
            bindings.into_iter().map(|binding| (binding.tool, binding.visibility)).collect()
        }))
    }

    /// The MCP definition of `server`'s tool `tool` (name, description, input schema,
    /// annotations), when this set can read it: what the opt-in consent policy decides on
    /// ([`AppConsent`]). `None` decides nothing: the call is held for the person.
    ///
    /// # Errors
    ///
    /// The set's error when it cannot list the server's tools.
    async fn tool_definition(&self, _server: &str, _tool: &str) -> adk_core::Result<Option<Value>> {
        Ok(None)
    }

    /// The name the agent's tools give `server`'s tool `tool`: prefixed with the server when
    /// another server publishes the same name, or `None` when the agent is not given that
    /// tool. The default is `tool` itself, for a set that renames nothing.
    ///
    /// # Errors
    ///
    /// The set's error when it cannot list the agent's tools.
    async fn agent_tool_name(&self, _server: &str, tool: &str) -> adk_core::Result<Option<String>> {
        Ok(Some(tool.to_string()))
    }
}

#[async_trait]
impl AppServers for McpServerManager {
    async fn ui_bindings(&self, server: &str) -> Option<adk_core::Result<Vec<McpUiBinding>>> {
        self.server_config(server).await.ok()?;
        Some(self.server_ui_bindings(server).await)
    }

    async fn tool_visibility(
        &self,
        server: &str,
    ) -> Option<adk_core::Result<Vec<(String, McpUiVisibility)>>> {
        self.server_config(server).await.ok()?;
        Some(self.server_tool_visibility(server).await)
    }

    async fn agent_tool_name(&self, server: &str, tool: &str) -> adk_core::Result<Option<String>> {
        McpServerManager::agent_tool_name(self, server, tool).await
    }

    async fn read_resource(
        &self,
        server: &str,
        uri: &str,
    ) -> adk_core::Result<Vec<ResourceContents>> {
        self.read_server_resource(server, uri).await
    }
}

/// One MCP server's toolset: its name is the server's.
#[async_trait]
impl<S> AppServers for adk_tool::mcp::McpToolset<S>
where
    S: adk_tool::mcp::rmcp::service::Service<adk_tool::mcp::rmcp::RoleClient>
        + Send
        + Sync
        + 'static,
{
    async fn ui_bindings(&self, server: &str) -> Option<adk_core::Result<Vec<McpUiBinding>>> {
        if adk_core::Toolset::name(self) != server {
            return None;
        }
        Some(adk_tool::mcp::McpToolset::ui_bindings(self).await)
    }

    async fn read_resource(
        &self,
        _server: &str,
        uri: &str,
    ) -> adk_core::Result<Vec<ResourceContents>> {
        adk_tool::mcp::McpToolset::read_resource(self, uri).await
    }

    async fn tool_visibility(
        &self,
        server: &str,
    ) -> Option<adk_core::Result<Vec<(String, McpUiVisibility)>>> {
        if adk_core::Toolset::name(self) != server {
            return None;
        }
        Some(adk_tool::mcp::McpToolset::tool_visibility(self).await)
    }

    async fn agent_tool_name(&self, server: &str, tool: &str) -> adk_core::Result<Option<String>> {
        if adk_core::Toolset::name(self) != server {
            return Ok(None);
        }
        let listing: Arc<dyn adk_core::ReadonlyContext> =
            Arc::new(adk_tool::SimpleToolContext::new("mcp-apps"));
        let tools = adk_core::Toolset::tools(self, listing).await?;
        Ok(tools.iter().any(|known| known.name() == tool).then(|| tool.to_string()))
    }

    async fn tool_definition(&self, server: &str, tool: &str) -> adk_core::Result<Option<Value>> {
        if adk_core::Toolset::name(self) != server {
            return Ok(None);
        }
        let listing: Arc<dyn adk_core::ReadonlyContext> =
            Arc::new(adk_tool::SimpleToolContext::new("mcp-apps"));
        let tools = adk_core::Toolset::tools(self, listing).await?;
        Ok(tools.iter().find(|known| known.name() == tool).map(|known| {
            // The annotations the server declared, as the tool's effect carries them.
            let read_only = matches!(known.effect(), adk_core::ToolEffect::ReadOnly);
            json!({
                "name": known.name(),
                "description": known.description(),
                "inputSchema": known.parameters_schema(),
                "annotations": { "readOnlyHint": read_only },
            })
        }))
    }
}

/// An app's page on the apps origin.
struct Frame {
    html: String,
    policy: String,
    /// The origin it is served on: the apps origin, or its server's own under it.
    origin: String,
    until: Instant,
}

/// When the home approves a call an app asks for without asking the person (spec 3.3 §6.1
/// `genui-apps`): never, unless a deployment opts in. A drawn form's Send is always held;
/// this is for apps only, and every approval it gives is recorded as
/// `surface.app.call_auto_approved` with `initiator: "app"`.
///
/// # Example
///
/// ```rust
/// use adk_surface::genui::{AppConsent, McpAppsHost};
/// use serde_json::json;
///
/// let consent = AppConsent { read_only: true, ..AppConsent::default() };
/// let definition = json!({ "name": "forecast", "annotations": { "readOnlyHint": true } });
/// assert!(consent.allows("weather", "forecast", &definition));
/// assert!(!AppConsent::default().allows("weather", "forecast", &definition), "off by default");
/// // A standing grant holds only while the tool's definition is the one granted.
/// let standing = AppConsent::default().standing("weather", "book", &json!({ "name": "book" }));
/// assert!(standing.allows("weather", "book", &json!({ "name": "book" })));
/// assert!(!standing.allows("weather", "book", &json!({ "name": "book", "description": "now pays too" })));
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct AppConsent {
    /// Approve a call to a tool whose definition says `readOnlyHint: true` and not
    /// `destructiveHint: true`.
    pub read_only: bool,
    /// Standing approvals: (server, tool, hash of the tool's definition) granted by the
    /// person; a changed definition asks again.
    pub standing: Vec<(String, String, String)>,
}

impl AppConsent {
    /// The SHA-256 of a tool's definition, as standing grants key it.
    pub fn definition_hash(definition: &Value) -> String {
        use sha2::Digest;
        let canonical = adk_core::tool_call_fingerprint("", definition);
        sha2::Sha256::digest(canonical.as_bytes())
            .iter()
            .map(|byte| format!("{byte:02x}"))
            .collect()
    }

    /// The same, with a standing approval of `server`'s tool `tool` as `definition` defines it.
    #[must_use]
    pub fn standing(mut self, server: &str, tool: &str, definition: &Value) -> Self {
        self.standing.push((
            server.to_string(),
            tool.to_string(),
            Self::definition_hash(definition),
        ));
        self
    }

    /// Whether a call to `server`'s tool `tool`, defined as `definition`, is approved without
    /// the person, and by which rule.
    pub fn allows(&self, server: &str, tool: &str, definition: &Value) -> bool {
        self.rule(server, tool, definition).is_some()
    }

    fn rule(&self, server: &str, tool: &str, definition: &Value) -> Option<&'static str> {
        let hash = Self::definition_hash(definition);
        if self.standing.iter().any(|(s, t, h)| s == server && t == tool && *h == hash) {
            return Some("app-consent:standing");
        }
        let annotations = definition.get("annotations");
        let flag = |name: &str| annotations.and_then(|a| a.get(name)).and_then(Value::as_bool);
        (self.read_only
            && flag("readOnlyHint") == Some(true)
            && flag("destructiveHint") != Some(true))
        .then_some("app-consent:read-only")
    }
}

/// The MCP Apps host (spec 3.3 §6.1 `genui-apps`). Mount its routes with
/// [`web::genui::apps_router`](crate::web::genui::apps_router), serve
/// [`sandbox_router`](Self::sandbox_router) on the apps origin, and wrap the home's API in
/// [`refuse_apps_origin`](crate::web::genui::refuse_apps_origin).
///
/// # Example
///
/// ```rust,no_run
/// use std::sync::Arc;
/// use adk_surface::genui::McpAppsHost;
///
/// # fn example(manager: Arc<adk_tool::mcp::McpServerManager>) {
/// let host = McpAppsHost::builder()
///     .servers(manager)
///     .apps_origin("http://127.0.0.1:41234")
///     .home_origin("http://127.0.0.1:8080")
///     .build();
/// assert_eq!(host.apps_origin(), Some("http://127.0.0.1:41234"));
/// # }
/// ```
#[derive(Clone)]
pub struct McpAppsHost {
    inner: Arc<Inner>,
}

struct Inner {
    servers: Vec<Arc<dyn AppServers>>,
    apps_origin: Option<String>,
    /// Whether each server's apps are framed on an origin of their own under the apps origin.
    per_server: bool,
    home_origins: Vec<String>,
    frames: Mutex<HashMap<String, Frame>>,
    /// What each open app's frame was granted (`hostCapabilities.sandbox`), by session and call.
    granted: Mutex<HashMap<String, Value>>,
    /// What each app asked the host to keep for it (`ui/set-view-state`), by session and call.
    view_states: Mutex<HashMap<String, Value>>,
    /// When an app's call is approved without the person: never unless opted in.
    consent: AppConsent,
}

impl std::fmt::Debug for McpAppsHost {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("McpAppsHost")
            .field("servers", &self.inner.servers.len())
            .field("apps_origin", &self.inner.apps_origin)
            .field("home_origins", &self.inner.home_origins)
            .finish()
    }
}

/// Builds an [`McpAppsHost`].
#[derive(Default)]
pub struct McpAppsHostBuilder {
    servers: Vec<Arc<dyn AppServers>>,
    apps_origin: Option<String>,
    per_server: Option<bool>,
    home_origins: Vec<String>,
    consent: AppConsent,
}

impl McpAppsHostBuilder {
    /// Shows the apps of the servers `servers` runs; may be called for several sets, which are
    /// asked in order.
    #[must_use]
    pub fn servers(mut self, servers: Arc<dyn AppServers>) -> Self {
        self.servers.push(servers);
        self
    }

    /// Frames apps on `origin`: `http://127.0.0.1:<port>` of the second loopback listener, or
    /// a remote home's `--apps-origin`. Without one, apps are their text result.
    #[must_use]
    pub fn apps_origin(mut self, origin: impl Into<String>) -> Self {
        self.apps_origin = Some(origin.into().trim_end_matches('/').to_string());
        self
    }

    /// Frames each server's apps on an origin of its own under the apps origin,
    /// `<sha256(server)[..16]>.<apps host>`, so two servers' apps never share one (spec 3.3
    /// §6.1 `genui-apps`). On by default when the apps origin's host is `localhost`, whose
    /// subdomains every browser resolves to this computer; a remote apps origin needs wildcard
    /// DNS and certificates first, and the home's page must name `*.<apps host>` in its
    /// `frame-src` ([`web::home_with_apps_origin`](crate::web::home_with_apps_origin) does for
    /// `localhost`).
    #[must_use]
    pub fn origin_per_server(mut self, per_server: bool) -> Self {
        self.per_server = Some(per_server);
        self
    }

    /// Approves some calls apps ask for without asking the person, by `consent` (off by
    /// default): calls to read-only tools, or tools the person gave a standing approval whose
    /// definition has not changed. A drawn form's Send is never affected.
    #[must_use]
    pub fn app_consent(mut self, consent: AppConsent) -> Self {
        self.consent = consent;
        self
    }

    /// An origin the home is served on: the only one that may frame apps, and one no app may
    /// declare.
    #[must_use]
    pub fn home_origin(mut self, origin: impl Into<String>) -> Self {
        self.home_origins.push(origin.into().trim_end_matches('/').to_string());
        self
    }

    /// The host.
    pub fn build(self) -> McpAppsHost {
        let per_server = self.per_server.unwrap_or_else(|| {
            self.apps_origin
                .as_deref()
                .and_then(origin_host)
                .is_some_and(|host| host == "localhost")
        });
        McpAppsHost {
            inner: Arc::new(Inner {
                servers: self.servers,
                apps_origin: self.apps_origin,
                per_server,
                home_origins: self.home_origins,
                frames: Mutex::new(HashMap::new()),
                granted: Mutex::new(HashMap::new()),
                view_states: Mutex::new(HashMap::new()),
                consent: self.consent,
            }),
        }
    }
}

/// The app a session's tool call showed, as its events record it.
pub(crate) struct ShownApp {
    record: McpAppRecord,
    args: Value,
    response: Value,
}

impl McpAppsHost {
    /// A builder.
    pub fn builder() -> McpAppsHostBuilder {
        McpAppsHostBuilder::default()
    }

    /// The origin apps are framed on, when there is one.
    pub fn apps_origin(&self) -> Option<&str> {
        self.inner.apps_origin.as_deref()
    }

    /// The origin `server`'s apps are framed on: its own under the apps origin when origins
    /// are per server ([`McpAppsHostBuilder::origin_per_server`]), else the apps origin.
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_surface::genui::McpAppsHost;
    ///
    /// let host = McpAppsHost::builder().apps_origin("http://localhost:41234").build();
    /// let weather = host.server_origin("weather").unwrap();
    /// assert!(weather.starts_with("http://") && weather.ends_with(".localhost:41234"));
    /// assert_ne!(weather, host.server_origin("calendar").unwrap());
    /// // An IP address has no subdomains: every app shares it.
    /// let loopback = McpAppsHost::builder().apps_origin("http://127.0.0.1:41234").build();
    /// assert_eq!(loopback.server_origin("weather").as_deref(), Some("http://127.0.0.1:41234"));
    /// ```
    pub fn server_origin(&self, server: &str) -> Option<String> {
        let origin = self.inner.apps_origin.clone()?;
        if !self.inner.per_server {
            return Some(origin);
        }
        let (scheme, rest) = origin.split_once("://")?;
        use sha2::Digest;
        let digest = sha2::Sha256::digest(server.as_bytes());
        let label: String = digest.iter().take(8).map(|byte| format!("{byte:02x}")).collect();
        Some(format!("{scheme}://{label}.{rest}"))
    }

    /// The routes of the apps origin (spec 3.3 §6.1 `genui-apps`): `GET /app/{token}` serves a
    /// checked app's page under its content security policy, sandboxed, framed only by the
    /// home; nothing else is served. Serve it on its own listener, never beside the home.
    pub fn sandbox_router(&self) -> Router {
        Router::new().route("/app/{token}", get(frame)).with_state(self.clone())
    }

    /// The app `call_id` showed in `session`, from its events.
    fn shown(events: &[Event], call_id: &str) -> Option<ShownApp> {
        let mut args = Value::Null;
        for event in events {
            let Some(content) = &event.llm_response.content else {
                continue;
            };
            for part in &content.parts {
                match part {
                    Part::FunctionCall { id: Some(id), args: called, .. } if id == call_id => {
                        args = called.clone();
                    }
                    Part::FunctionResponse { function_response, id: Some(id), .. }
                        if id == call_id =>
                    {
                        // A held call's placeholder answer carries no record; the answer of
                        // the call once allowed follows under the same id.
                        let Some(record) = record_of(event, call_id) else {
                            continue;
                        };
                        return Some(ShownApp {
                            record,
                            args,
                            response: function_response.response.clone(),
                        });
                    }
                    _ => {}
                }
            }
        }
        None
    }

    async fn app(
        &self,
        engine: &SurfaceEngine,
        caller: &Caller,
        session: &str,
        call_id: &str,
    ) -> Result<ShownApp> {
        let events = engine.app_transcript(caller, session).await?;
        Self::shown(&events, call_id).ok_or_else(|| {
            SurfaceError::not_found(format!("app '{call_id}' in session '{session}'"))
        })
    }

    /// The bindings `server` publishes, from the first server set that runs it.
    async fn bindings(&self, server: &str) -> adk_core::Result<Vec<McpUiBinding>> {
        for servers in &self.inner.servers {
            if let Some(bindings) = servers.ui_bindings(server).await {
                return bindings;
            }
        }
        Err(adk_core::AdkError::tool(format!("no MCP server named '{server}' runs on this home")))
    }

    async fn resource(&self, server: &str, uri: &str) -> adk_core::Result<Vec<ResourceContents>> {
        for servers in &self.inner.servers {
            if servers.ui_bindings(server).await.is_some() {
                return servers.read_resource(server, uri).await;
            }
        }
        Err(adk_core::AdkError::tool(format!("no MCP server named '{server}' runs on this home")))
    }

    /// The origin of the home's page that asked, from the request's `Host` and the apps
    /// origin's scheme: a remote home's own origin, or the address a person opened a home
    /// listening on every interface at.
    fn page_origin(&self, host: Option<&str>) -> Option<String> {
        let (scheme, _) = self.inner.apps_origin.as_deref()?.split_once("://")?;
        let origin = format!("{scheme}://{}", host?.trim().to_ascii_lowercase());
        sites::parse(&origin, false).filter(sites::Parsed::is_plain)?;
        Some(origin)
    }

    /// Who may call `server`'s tool `tool`, and the name the agent knows it by when the model
    /// and the app may both call it, from the first set that runs `server`; `None` when the
    /// server does not publish it.
    async fn app_tool(
        &self,
        server: &str,
        tool: &str,
    ) -> adk_core::Result<Option<(McpUiVisibility, Option<String>)>> {
        for servers in &self.inner.servers {
            let Some(listed) = servers.tool_visibility(server).await else {
                continue;
            };
            let Some((_, visibility)) = listed?.into_iter().find(|(name, _)| name == tool) else {
                return Ok(None);
            };
            let name = if visibility.model && visibility.app {
                servers.agent_tool_name(server, tool).await?
            } else {
                None
            };
            return Ok(Some((visibility, name)));
        }
        Err(adk_core::AdkError::tool(format!("no MCP server named '{server}' runs on this home")))
    }

    /// The consent rule that approves a call to `server`'s tool `tool` without the person, if
    /// any: read from the tool's definition, which the first set that runs `server` gives.
    async fn consent_rule(&self, server: &str, tool: &str) -> Option<&'static str> {
        let consent = &self.inner.consent;
        if !consent.read_only && consent.standing.is_empty() {
            return None;
        }
        for servers in &self.inner.servers {
            if servers.ui_bindings(server).await.is_some() {
                let definition = servers.tool_definition(server, tool).await.ok().flatten()?;
                return consent.rule(server, tool, &definition);
            }
        }
        None
    }

    /// The open decisions of the calls the app `call_id` asked for in `session`.
    async fn waiting_calls(
        engine: &SurfaceEngine,
        caller: &Caller,
        session: &str,
        call_id: &str,
    ) -> Result<Vec<DecisionCard>> {
        let asked: HashSet<String> = engine
            .thread(caller, session)
            .await?
            .into_iter()
            .flat_map(|entry| entry.items)
            .filter_map(|item| match item {
                ThreadItem::DrawnSent { sent } if sent.surface_id == call_id => sent.decision_id,
                _ => None,
            })
            .collect();
        Ok(engine
            .session_decisions(caller, session)
            .await?
            .into_iter()
            .filter(|card| card.decided.is_none() && asked.contains(&card.id))
            .collect())
    }

    /// Whether the agent held the requested call `requested` (`true`) or refused it, with the
    /// thread's words for it, once its run took it; `None` when it has not within
    /// [`OUTCOME_WAIT`].
    async fn outcome(
        engine: &SurfaceEngine,
        caller: &Caller,
        session: &str,
        requested: &str,
    ) -> Result<Option<(bool, String)>> {
        let until = Instant::now() + OUTCOME_WAIT;
        loop {
            let sent = engine
                .thread(caller, session)
                .await?
                .into_iter()
                .flat_map(|entry| entry.items)
                .find_map(|item| match item {
                    ThreadItem::DrawnSent { sent } if sent.call_id == requested => Some(sent),
                    _ => None,
                });
            if let Some(sent) = sent {
                return Ok(Some((sent.held, sent.text)));
            }
            if Instant::now() >= until {
                return Ok(None);
            }
            tokio::time::sleep(OUTCOME_POLL).await;
        }
    }

    /// Opens the app `call_id` showed in `session` for `caller` (spec 3.3 §6.1 `genui-apps`):
    /// framed when every check passes, blocked with its reason, or not shown on this surface.
    /// `page_host` is the request's `Host`: the page that frames the app, which its policy
    /// names beside the home's origins and no app may declare.
    ///
    /// # Errors
    ///
    /// `surface.not_found` when the session is not the caller's or holds no such app (S-I8).
    pub(crate) async fn open(
        &self,
        engine: &SurfaceEngine,
        caller: &Caller,
        session: &str,
        call_id: &str,
        page_host: Option<&str>,
    ) -> Result<AppOpen> {
        let app = self.app(engine, caller, session, call_id).await?;
        let result_text = result_text(&app.response);
        let elsewhere = |text: &str| AppOpen::Elsewhere {
            text: text.to_string(),
            result_text: result_text.clone(),
        };
        // Phones never render apps: the apps origin is a loopback listener (decision 17).
        let Some(origin) =
            self.inner.apps_origin.clone().filter(|_| caller.surface == SurfaceKind::Web)
        else {
            return Ok(elsewhere(words::OPEN_THE_HOME_TO_SEE_THIS_APP));
        };
        let record = &app.record;
        let mut home_origins = self.inner.home_origins.clone();
        if let Some(page) = self.page_origin(page_host)
            && !home_origins.contains(&page)
        {
            home_origins.push(page);
        }
        // Origins no app may declare or load from: the home's, the page's and the apps origin.
        let refused: Vec<String> =
            home_origins.iter().chain(&self.inner.apps_origin).cloned().collect();
        let published = match self.bindings(&record.server).await {
            Ok(bindings) => bindings,
            Err(error) => {
                tracing::warn!(app.server = %record.server, error = %error, "an app's server did not list its tools");
                return Ok(elsewhere(words::APP_CANT_BE_OPENED));
            }
        };
        if !published.iter().any(|binding| {
            binding.tool == record.tool && binding.resource_uri == record.resource_uri
        }) {
            return Ok(elsewhere(words::APP_CANT_BE_OPENED));
        }
        let contents = match self.resource(&record.server, &record.resource_uri).await {
            Ok(contents) => contents,
            Err(error) => {
                tracing::warn!(app.server = %record.server, error = %error, "an app's resource could not be read");
                return Ok(elsewhere(words::APP_CANT_BE_OPENED));
            }
        };
        let checked = McpAppResource::from_contents(&record.resource_uri, &contents)
            .map_err(|error| (AppBlockReason::NotAnApp, error.message.clone()))
            .and_then(|resource| {
                let declared = sites::checked(&resource.csp, &refused)?;
                let refused_hosts: Vec<String> = refused
                    .iter()
                    .filter_map(|origin| origin.split_once("://").map(|(_, rest)| rest))
                    .map(|rest| rest.rsplit_once(':').map_or(rest, |(host, _)| host).to_string())
                    .collect();
                match scan::scan(&resource.html, &declared, &refused_hosts) {
                    Some(refusal) => Err(refusal),
                    None => Ok((resource, declared)),
                }
            });
        let (resource, declared) = match checked {
            Ok(passed) => passed,
            Err((reason, detail)) => {
                let text = match reason {
                    AppBlockReason::ApprovalButtons => words::APP_BLOCKED_APPROVAL_BUTTONS,
                    AppBlockReason::UndeclaredSites => words::APP_BLOCKED_UNDECLARED_SITES,
                    AppBlockReason::ThisComputer => words::APP_BLOCKED_THIS_COMPUTER,
                    AppBlockReason::NotAnApp => words::APP_BLOCKED_NOT_AN_APP,
                };
                engine
                    .record(
                        caller,
                        SURFACE_AUDIT_APP_BLOCKED,
                        json!({
                            "sessionId": session,
                            "callId": call_id,
                            "server": record.server,
                            "tool": record.tool,
                            "resourceUri": record.resource_uri,
                            "reason": reason,
                            "detail": detail,
                        }),
                    )
                    .await?;
                return Ok(AppOpen::Blocked { reason, text: text.to_string(), detail });
            }
        };
        let token = uuid::Uuid::new_v4().simple().to_string();
        // Each server's apps on an origin of their own, when origins are per server.
        let origin = self.server_origin(&record.server).unwrap_or(origin);
        let frame = Frame {
            html: resource.html.clone(),
            policy: declared.policy(&home_origins),
            origin: origin.clone(),
            until: Instant::now() + FRAME_LIFETIME,
        };
        // What the frame is granted, reported to the app at ui/initialize: the sites that
        // passed, and no permission (camera, microphone, geolocation, clipboard) at all.
        {
            let mut granted =
                self.inner.granted.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
            if granted.len() > MAX_GRANTS_KEPT {
                granted.clear();
            }
            granted.insert(
                format!("{session}/{call_id}"),
                json!({ "permissions": {}, "csp": declared.granted() }),
            );
        }
        {
            let mut frames =
                self.inner.frames.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
            frames.retain(|_, frame| frame.until > Instant::now());
            frames.insert(token.clone(), frame);
        }
        Ok(AppOpen::Shown {
            frame_url: format!("{origin}/app/{token}"),
            frame_origin: origin,
            max_height: MAX_HEIGHT,
            prefers_border: resource.prefers_border.unwrap_or(true),
            tool_input: app.args.clone(),
            tool_result: call_tool_result(&app.response),
            refused_permissions: resource.permissions,
        })
    }

    /// Answers a request the app `call_id` in `session` made (spec 3.3 §6.1 `genui-apps`).
    ///
    /// # Errors
    ///
    /// `surface.not_found` when the session is not the caller's or holds no such app (S-I8);
    /// the source's error when a held call could not be asked for.
    pub(crate) async fn request(
        &self,
        engine: &SurfaceEngine,
        caller: &Caller,
        session: &str,
        call_id: &str,
        request: AppRequest,
    ) -> Result<AppAnswer> {
        let app = self.app(engine, caller, session, call_id).await?;
        let label = app_name(&app.record.server);
        let ok = |result: Value| AppAnswer { result: Some(result), error: None, prompt: None };
        let error = |code: i64, message: String, prompt: Option<AppPrompt>| AppAnswer {
            result: None,
            error: Some(AppError { code, message }),
            prompt,
        };
        match request.method.as_str() {
            "ui/initialize" => Ok(ok(json!({
                "protocolVersion": MCP_APPS_VERSION,
                "hostInfo": { "name": "adk-rust", "version": env!("CARGO_PKG_VERSION") },
                "hostCapabilities": {
                    "openLinks": {},
                    "downloadFile": {},
                    "serverTools": {},
                    "logging": {},
                    // What the frame was actually granted; never same-origin storage.
                    "sandbox": self
                        .inner
                        .granted
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner)
                        .get(&format!("{session}/{call_id}"))
                        .cloned()
                        .unwrap_or_else(|| json!({ "permissions": {}, "csp": {} })),
                },
                "hostContext": {
                    "displayMode": "inline",
                    "availableDisplayModes": ["inline"],
                    "platform": "web",
                    "containerDimensions": { "maxHeight": MAX_HEIGHT },
                    // What the app asked the host to keep, for an app opened again.
                    "viewState": self
                        .inner
                        .view_states
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner)
                        .get(&format!("{session}/{call_id}"))
                        .cloned()
                        .unwrap_or(Value::Null),
                },
            }))),
            "ping" => Ok(ok(json!({}))),
            // The home frames apps inline only: a request for another mode gets inline back,
            // and never without the person's gesture.
            "ui/request-display-mode" => Ok(ok(json!({ "mode": "inline" }))),
            // The host keeps an app's view state for the next time it opens (MCP Apps draft
            // `ui/set-view-state`), at most `MAX_VIEW_STATE_BYTES`.
            "ui/set-view-state" => {
                let state = request.params.get("state").cloned().unwrap_or(Value::Null);
                if state.to_string().len() > MAX_VIEW_STATE_BYTES {
                    return Ok(error(
                        INVALID_PARAMS,
                        format!("a view state is at most {MAX_VIEW_STATE_BYTES} bytes"),
                        None,
                    ));
                }
                let mut states = self
                    .inner
                    .view_states
                    .lock()
                    .unwrap_or_else(std::sync::PoisonError::into_inner);
                if states.len() > MAX_GRANTS_KEPT {
                    states.clear();
                }
                states.insert(format!("{session}/{call_id}"), state);
                Ok(ok(json!({})))
            }
            "tools/call" => {
                // Only a tool's name, so nothing else the app sends reaches the home's words
                // or the audit trail in its place.
                let Some(tool) = request
                    .params
                    .get("name")
                    .and_then(Value::as_str)
                    .filter(|name| is_tool_name(name))
                else {
                    return Ok(error(
                        INVALID_PARAMS,
                        "tools/call needs the name of one of its server's tools".into(),
                        None,
                    ));
                };
                let args = request.params.get("arguments").cloned().unwrap_or_else(|| json!({}));
                let server = app.record.server.as_str();
                let found = match self.app_tool(server, tool).await {
                    Ok(found) => found,
                    Err(failure) => {
                        tracing::warn!(app.server = %server, error = %failure, "an app's server did not list its tools");
                        return Ok(error(REFUSED, words::APP_SERVER_DIDNT_ANSWER.into(), None));
                    }
                };
                let record = |reason: &str, extra: Value| {
                    let mut fields = json!({
                        "sessionId": session,
                        "callId": call_id,
                        "server": server,
                        "tool": tool,
                        "via": "app",
                        "reason": reason,
                    });
                    if let (Some(fields), Value::Object(extra)) = (fields.as_object_mut(), extra) {
                        fields.extend(extra);
                    }
                    fields
                };
                let refused =
                    |text: String| error(REFUSED, text.clone(), Some(AppPrompt::Notice { text }));
                let tool_name = match found {
                    Some((visibility, Some(name))) if visibility.model && visibility.app => name,
                    Some((visibility, _)) if visibility.app && !visibility.model => {
                        engine
                            .record(
                                caller,
                                SURFACE_AUDIT_APP_CALL_REFUSED,
                                record("appOnly", json!({})),
                            )
                            .await?;
                        return Ok(refused(words::APP_NEEDS_A_TOOL.to_string()));
                    }
                    // A model-only tool, one the app's own server does not publish, or one the
                    // agent is not given.
                    _ => {
                        engine
                            .record(
                                caller,
                                SURFACE_AUDIT_APP_CALL_REFUSED,
                                record("notTheApps", json!({})),
                            )
                            .await?;
                        return Ok(refused(words::app_cant_use(tool)));
                    }
                };
                // The same call again, as an app asks on every load, is the decision already
                // waiting; an app with enough waiting asks for no more.
                let waiting = Self::waiting_calls(engine, caller, session, call_id).await?;
                if waiting.iter().any(|card| card.call.tool == tool_name && card.call.args == args)
                {
                    return Ok(ok(sent_text(words::APP_CALL_WAITS_FOR_YOU)));
                }
                if waiting.len() >= MAX_WAITING {
                    engine
                        .record(
                            caller,
                            SURFACE_AUDIT_APP_CALL_REFUSED,
                            record("tooManyWaiting", json!({})),
                        )
                        .await?;
                    return Ok(refused(words::app_asked_enough(MAX_WAITING)));
                }
                let (called_tool, called_args) = (tool_name.clone(), args.clone());
                let call = RequestedCall::new(
                    tool_name,
                    args,
                    caller.subject.clone(),
                    RequestedVia {
                        kind: RequestedViaKind::App,
                        surface_id: call_id.to_string(),
                        version: 1,
                        action_id: tool.to_string(),
                        digest: None,
                    },
                );
                let requested = call.function_call_id.clone();
                let fingerprint = adk_core::ToolConfirmationRequest::new(
                    call.tool_name.clone(),
                    call.args.clone(),
                )
                .fingerprint();
                let asked = json!({ "requestedCallId": requested, "fingerprint": fingerprint });
                engine
                    .record(caller, SURFACE_AUDIT_APP_CALL_ASKED, record("asked", asked.clone()))
                    .await?;
                if let Err(failure) = engine.ask_for_app_call(caller, session, call, &label).await {
                    let reason =
                        json!({ "requestedCallId": requested, "detail": failure.to_string() });
                    engine
                        .record(caller, SURFACE_AUDIT_APP_CALL_REFUSED, record("notAsked", reason))
                        .await?;
                    return Err(failure);
                }
                // Answered for what the agent did with the call, never before it did.
                match Self::outcome(engine, caller, session, &requested).await? {
                    Some((true, _)) => {
                        engine
                            .record(caller, SURFACE_AUDIT_APP_CALL_HELD, record("held", asked))
                            .await?;
                        // Opt-in consent: a call the policy allows is approved now, recorded
                        // with the rule and `initiator: "app"`; the rest waits for the person.
                        if let Some(rule) = self.consent_rule(server, tool).await {
                            let card = Self::waiting_calls(engine, caller, session, call_id)
                                .await?
                                .into_iter()
                                .find(|card| {
                                    card.call.tool == called_tool && card.call.args == called_args
                                });
                            if let Some(card) = card {
                                engine
                                    .approve_by_policy(
                                        caller,
                                        &card.id,
                                        card.version,
                                        rule,
                                        json!({ "server": server, "tool": tool, "callId": call_id }),
                                    )
                                    .await?;
                                return Ok(ok(sent_text(words::APP_CALL_SENT)));
                            }
                        }
                        Ok(ok(sent_text(words::APP_CALL_WAITS_FOR_YOU)))
                    }
                    Some((false, text)) => {
                        let reason = json!({ "requestedCallId": requested, "detail": text });
                        engine
                            .record(
                                caller,
                                SURFACE_AUDIT_APP_CALL_REFUSED,
                                record("byTheAgent", reason),
                            )
                            .await?;
                        Ok(error(REFUSED, text, None))
                    }
                    None => Ok(ok(sent_text(words::APP_CALL_SENT))),
                }
            }
            "ui/message" => {
                let message = message_text(&request.params);
                if message.trim().is_empty() {
                    return Ok(error(INVALID_PARAMS, "ui/message needs text content".into(), None));
                }
                let message: String = message.chars().take(MAX_MESSAGE_CHARS).collect();
                Ok(AppAnswer {
                    result: Some(json!({})),
                    error: None,
                    prompt: Some(AppPrompt::Message {
                        text: words::app_wants_to_send(&message),
                        message,
                        send_label: words::SEND_IT.to_string(),
                        dismiss_label: words::NOT_NOW.to_string(),
                        note: words::MARKED_AS_SUGGESTED_BY_THE_APP.to_string(),
                    }),
                })
            }
            "ui/open-link" => {
                let url = request.params.get("url").and_then(Value::as_str).unwrap_or_default();
                let parsed = sites::parse(url, true).filter(|parsed| {
                    parsed.is_plain() && (url.starts_with("https://") || url.starts_with("http://"))
                });
                match parsed {
                    Some(parsed) if sites::is_this_computer(parsed.host()) => Ok(error(
                        REFUSED,
                        "ui/open-link opens sites on the web, never this computer or its network"
                            .into(),
                        None,
                    )),
                    Some(_) => Ok(AppAnswer {
                        result: Some(json!({})),
                        error: None,
                        prompt: Some(AppPrompt::Link {
                            text: words::open_site(&link_host(url)),
                            url: url.to_string(),
                            open_label: words::OPEN.to_string(),
                            dismiss_label: words::CANCEL.to_string(),
                        }),
                    }),
                    None => Ok(error(
                        INVALID_PARAMS,
                        "ui/open-link opens http and https links only".into(),
                        None,
                    )),
                }
            }
            "ui/download-file" => Ok(match download(&request.params, &label) {
                Ok(prompt) => {
                    AppAnswer { result: Some(json!({})), error: None, prompt: Some(prompt) }
                }
                Err(why) => error(INVALID_PARAMS, why, None),
            }),
            "ui/update-model-context" => {
                // Logged, never forwarded: an app does not steer the model (decision 15).
                tracing::info!(
                    app.server = %app.record.server,
                    app.call_id = %call_id,
                    context.bytes = request.params.to_string().len(),
                    "an app's model context update was logged and not forwarded"
                );
                Ok(ok(json!({})))
            }
            other => Ok(error(METHOD_NOT_FOUND, format!("{other} is not supported here"), None)),
        }
    }

    /// Sends `message`, which the app `call_id` suggested, as `caller`'s message in `session`,
    /// inside an untrusted-content block labelled as the app's suggestion (spec 3.3 §6.1
    /// `genui-apps`). It never answers a decision: it is the session's next message.
    ///
    /// # Errors
    ///
    /// `surface.not_found` when the session is not the caller's or holds no such app;
    /// `surface.invalid` for an empty or overlong message; the engine's error for the send.
    pub(crate) async fn send_message(
        &self,
        engine: &SurfaceEngine,
        caller: &Caller,
        session: &str,
        call_id: &str,
        message: &str,
    ) -> Result<CommandOutcome> {
        let app = self.app(engine, caller, session, call_id).await?;
        if message.trim().is_empty() || message.chars().count() > MAX_MESSAGE_CHARS {
            return Err(SurfaceError::Invalid(format!(
                "a suggested message has 1 to {MAX_MESSAGE_CHARS} characters"
            )));
        }
        let label = app_name(&app.record.server);
        let suggested = words::suggested_by_the_app(&label);
        let text = adk_harness::web::untrusted_block(
            "mcp-app",
            &[("app", app.record.server.as_str()), ("label", suggested.as_str())],
            message,
        );
        engine
            .record(
                caller,
                SURFACE_AUDIT_APP_MESSAGE_SENT,
                json!({ "sessionId": session, "callId": call_id, "server": app.record.server }),
            )
            .await?;
        engine
            .execute(
                caller,
                Command::Send { session_id: session.to_string(), text, images: Vec::new() },
            )
            .await
    }
}

/// `GET /app/{token}` on the apps origin.
async fn frame(
    State(host): State<McpAppsHost>,
    Path(token): Path<String>,
    headers: axum::http::HeaderMap,
) -> Response {
    let asked_host = headers
        .get(header::HOST)
        .and_then(|value| value.to_str().ok())
        .map(|value| value.trim().to_ascii_lowercase());
    let page = {
        let frames = host.inner.frames.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
        frames
            .get(&token)
            .filter(|frame| frame.until > Instant::now())
            // Served only on its own server's origin, never another server's.
            .filter(|frame| {
                asked_host.is_none()
                    || frame.origin.split_once("://").map(|(_, rest)| rest.to_ascii_lowercase())
                        == asked_host
            })
            .map(|frame| (frame.html.clone(), frame.policy.clone()))
    };
    let Some((html, policy)) = page else {
        return StatusCode::NOT_FOUND.into_response();
    };
    // Never served without its policy.
    let Ok(policy) = HeaderValue::from_str(&policy) else {
        tracing::warn!(
            "an app's content security policy is not a header value; its page was not served"
        );
        return StatusCode::INTERNAL_SERVER_ERROR.into_response();
    };
    let mut response = html.into_response();
    let headers = response.headers_mut();
    headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html; charset=utf-8"));
    headers.insert(header::CONTENT_SECURITY_POLICY, policy);
    headers.insert(header::X_CONTENT_TYPE_OPTIONS, HeaderValue::from_static("nosniff"));
    headers.insert(header::REFERRER_POLICY, HeaderValue::from_static("no-referrer"));
    headers.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
    headers.insert(
        "permissions-policy",
        HeaderValue::from_static("camera=(), microphone=(), geolocation=(), display-capture=()"),
    );
    headers.insert("cross-origin-resource-policy", HeaderValue::from_static("cross-origin"));
    response
}

/// The host of `origin`, `scheme://host[:port]`, lowercased.
fn origin_host(origin: &str) -> Option<String> {
    let (_, rest) = origin.split_once("://")?;
    let host = rest.split('/').next()?;
    let host = host.rsplit_once(':').map_or(host, |(host, port)| {
        if port.bytes().all(|byte| byte.is_ascii_digit()) { host } else { rest }
    });
    Some(host.to_ascii_lowercase())
}

/// The largest file an app may offer to save (`ui/download-file`).
const MAX_DOWNLOAD_BYTES: usize = 5 * 1024 * 1024;

/// Extensions a saved file may not have: ones that run, or that a browser opens as a page.
const UNSAFE_EXTENSIONS: &[&str] = &[
    "exe", "msi", "bat", "cmd", "com", "scr", "ps1", "vbs", "js", "mjs", "jar", "sh", "app", "dmg",
    "pkg", "deb", "rpm", "html", "htm", "xhtml", "svg", "mht", "url", "lnk", "desktop",
];

/// `ui/download-file`'s first embedded resource as a host prompt the person answers (spec
/// 3.3 §6.1 `genui-apps`): its name sanitized, its size capped, a link to fetch or a file
/// that runs refused.
fn download(params: &Value, app: &str) -> std::result::Result<AppPrompt, String> {
    let item = params
        .get("contents")
        .and_then(Value::as_array)
        .and_then(|contents| contents.first())
        .ok_or("ui/download-file needs contents: one embedded resource")?;
    if item.get("type").and_then(Value::as_str) != Some("resource") {
        return Err("ui/download-file saves an embedded resource; this home fetches no link".into());
    }
    let resource = item.get("resource").ok_or("an embedded resource needs its resource")?;
    let uri = resource.get("uri").and_then(Value::as_str).unwrap_or_default();
    let raw = uri.rsplit(['/', '\\', ':']).next().unwrap_or_default();
    let name: String = raw
        .chars()
        .filter(|c| !c.is_control() && !"<>:\"/\\|?*".contains(*c))
        .take(100)
        .collect::<String>()
        .trim_matches(['.', ' '])
        .to_string();
    let name = if name.is_empty() { "download".to_string() } else { name };
    let extension = name.rsplit_once('.').map(|(_, ext)| ext.to_ascii_lowercase());
    if extension.as_deref().is_some_and(|ext| UNSAFE_EXTENSIONS.contains(&ext)) {
        return Err(format!("this home does not save .{} files", extension.unwrap_or_default()));
    }
    let mime_type = resource
        .get("mimeType")
        .and_then(Value::as_str)
        .filter(|mime| {
            !mime.contains("html") && !mime.contains("svg") && !mime.contains("javascript")
        })
        .unwrap_or("application/octet-stream")
        .to_string();
    let (text, blob) = (
        resource.get("text").and_then(Value::as_str),
        resource.get("blob").and_then(Value::as_str),
    );
    let size = match (text, blob) {
        (Some(text), _) => text.len(),
        (None, Some(blob)) => blob.len() / 4 * 3,
        (None, None) => return Err("an embedded resource needs text or blob".into()),
    };
    if size > MAX_DOWNLOAD_BYTES {
        return Err(format!("this home saves files up to {} MB", MAX_DOWNLOAD_BYTES / 1024 / 1024));
    }
    Ok(AppPrompt::Download {
        text: words::app_wants_to_save(app, &name, size),
        name,
        mime_type,
        text_content: text.map(str::to_string),
        blob: if text.is_none() { blob.map(str::to_string) } else { None },
        save_label: words::SAVE.to_string(),
        dismiss_label: words::CANCEL.to_string(),
    })
}

/// Whether `name` can be an MCP tool's name: 1 to 128 letters, digits, `_`, `-`, `.` and `/`.
fn is_tool_name(name: &str) -> bool {
    (1..=128).contains(&name.len())
        && name
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-' | b'.' | b'/'))
}

/// The answer to an app's `tools/call` that ran nothing: `text` for the app.
fn sent_text(text: &str) -> Value {
    json!({ "content": [{ "type": "text", "text": text }], "isError": false })
}

/// The app record `event` carries for `call_id`.
fn record_of(event: &Event, call_id: &str) -> Option<McpAppRecord> {
    let record = event.actions.state_delta.get(MCP_APP_STATE_KEY)?;
    let record: McpAppRecord = serde_json::from_value(record.clone()).ok()?;
    (record.call_id == call_id).then_some(record)
}

/// The text of an MCP tool's result as `McpToolset` records it: `{"output": ...}`.
fn result_text(response: &Value) -> String {
    let output = response.get("response").unwrap_or(response);
    if let Some(text) = output.get("text").and_then(Value::as_array) {
        return text.iter().filter_map(Value::as_str).collect::<Vec<_>>().join("\n");
    }
    match output.get("output") {
        Some(Value::String(text)) => text.clone(),
        Some(other) => other.to_string(),
        None => String::new(),
    }
}

/// The MCP `CallToolResult` an app's `ui/notifications/tool-result` carries.
fn call_tool_result(response: &Value) -> Value {
    let output = response.get("response").unwrap_or(response);
    let mut result = json!({
        "content": [{ "type": "text", "text": result_text(response) }],
        "isError": false,
    });
    if let Some(structured) = output.get("output").filter(|output| output.is_object()) {
        result["structuredContent"] = structured.clone();
    }
    result
}

/// The text of a `ui/message`: its content's text blocks.
fn message_text(params: &Value) -> String {
    let content = params.get("content").unwrap_or(params);
    let blocks: Vec<&Value> = match content {
        Value::Array(blocks) => blocks.iter().collect(),
        other => vec![other],
    };
    blocks
        .iter()
        .filter_map(|block| match block {
            Value::String(text) => Some(text.as_str()),
            block => block.get("text").and_then(Value::as_str),
        })
        .collect::<Vec<_>>()
        .join("\n")
}

/// The host a link opens, without `www.`, as "Open example.com?" names it.
fn link_host(url: &str) -> String {
    let rest = url.split_once("://").map_or(url, |(_, rest)| rest);
    let authority = &rest[..rest.find(['/', '?', '#']).unwrap_or(rest.len())];
    authority.trim_start_matches("www.").to_string()
}

/// The thread item of `event`'s response to `call_id` when an MCP server's tool with an app
/// made it (spec 3.3 §6.1 `genui-apps`): the event carries the tool's own record for that
/// call. A same-named tool without it is an ordinary result.
pub(crate) fn project(
    projector: &Projector,
    event: &Event,
    response: &Value,
    call_id: Option<&String>,
) -> Option<ThreadItem> {
    let record = record_of(event, call_id?)?;
    let label = app_name(&record.server);
    Some(ThreadItem::App {
        app: Box::new(AppView {
            call_id: record.call_id.clone(),
            server: record.server.clone(),
            app_name: label.clone(),
            tool: record.tool.clone(),
            agent: projector.agent(&event.author),
            at: event.timestamp,
            header_text: words::app_from(&label),
            result_text: result_text(response),
            elsewhere_text: words::OPEN_THE_HOME_TO_SEE_THIS_APP.to_string(),
            terminal_text: words::app_in_the_terminal(&label),
        }),
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_suggested_message_reads_its_text_blocks() {
        assert_eq!(
            message_text(
                &json!({ "role": "user", "content": [{ "type": "text", "text": "Approved" }] })
            ),
            "Approved"
        );
        assert_eq!(message_text(&json!({ "content": { "type": "text", "text": "hi" } })), "hi");
    }

    #[tokio::test]
    async fn a_page_whose_policy_cannot_be_sent_is_not_served() {
        use axum::body::Body;
        use axum::http::Request;
        use tower::ServiceExt as _;

        let host = McpAppsHost::builder().apps_origin("http://127.0.0.1:41234").build();
        host.inner.frames.lock().unwrap().insert(
            "token".into(),
            Frame {
                html: "<p>Nairobi</p>".into(),
                policy: "default-src 'none'; connect-src https://[a.b\u{1}]".into(),
                origin: "http://127.0.0.1:41234".into(),
                until: Instant::now() + FRAME_LIFETIME,
            },
        );
        let response = host
            .sandbox_router()
            .oneshot(Request::builder().uri("/app/token").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
        let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
        assert!(!String::from_utf8_lossy(&body).contains("Nairobi"));
    }

    #[tokio::test]
    async fn each_server_has_its_own_origin_and_a_frame_is_served_only_on_its_own() {
        use axum::body::Body;
        use axum::http::Request;
        use tower::ServiceExt as _;

        let host = McpAppsHost::builder().apps_origin("http://localhost:41234").build();
        let weather = host.server_origin("weather").unwrap();
        let calendar = host.server_origin("calendar").unwrap();
        assert_ne!(weather, calendar);
        assert_eq!(weather, host.server_origin("weather").unwrap(), "stable");
        host.inner.frames.lock().unwrap().insert(
            "token".into(),
            Frame {
                html: "<p>Nairobi</p>".into(),
                policy: "default-src 'none'".into(),
                origin: weather.clone(),
                until: Instant::now() + FRAME_LIFETIME,
            },
        );
        let at = |origin: &str| {
            let host_header = origin.split_once("://").unwrap().1.to_string();
            Request::builder()
                .uri("/app/token")
                .header("host", host_header)
                .body(Body::empty())
                .unwrap()
        };
        let served = host.sandbox_router().oneshot(at(&weather)).await.unwrap();
        assert_eq!(served.status(), StatusCode::OK);
        let elsewhere = host.sandbox_router().oneshot(at(&calendar)).await.unwrap();
        assert_eq!(elsewhere.status(), StatusCode::NOT_FOUND, "never on another server's origin");
        // Per server only where subdomains resolve, unless asked for.
        let remote = McpAppsHost::builder().apps_origin("https://apps.example.net").build();
        assert_eq!(remote.server_origin("weather").as_deref(), Some("https://apps.example.net"));
        let opted = McpAppsHost::builder()
            .apps_origin("https://apps.example.net")
            .origin_per_server(true)
            .build();
        assert!(opted.server_origin("weather").unwrap().ends_with(".apps.example.net"));
    }

    #[test]
    fn a_file_an_app_offers_is_a_prompt_with_a_safe_name_and_size() {
        let offered = |name: &str, mime: &str, text: &str| {
            download(
                &json!({ "contents": [{ "type": "resource",
                    "resource": { "uri": format!("file:///tmp/{name}"), "mimeType": mime, "text": text } }] }),
                "Weather",
            )
        };
        match offered("forecast.csv", "text/csv", "day,rain\nmon,4").unwrap() {
            AppPrompt::Download { text, name, mime_type, text_content, .. } => {
                assert_eq!(name, "forecast.csv");
                assert_eq!(mime_type, "text/csv");
                assert_eq!(text, "The Weather app wants to save forecast.csv (14 bytes)");
                assert_eq!(text_content.as_deref(), Some("day,rain\nmon,4"));
            }
            other => panic!("{other:?}"),
        }
        // A page or a program is never saved; a link is never fetched; a huge file is refused.
        assert!(offered("report.html", "text/html", "<p>").is_err());
        assert!(offered("setup.exe", "application/octet-stream", "MZ").is_err());
        assert!(download(&json!({ "contents": [{ "type": "resource_link", "uri": "https://x.test/a.csv" }] }), "W").is_err());
        let huge = "x".repeat(MAX_DOWNLOAD_BYTES + 1);
        assert!(offered("big.txt", "text/plain", &huge).is_err());
        // A name made of path tricks keeps only its last part.
        match offered("..%2F..%2Fpasswd", "text/plain", "x").unwrap() {
            AppPrompt::Download { name, .. } => {
                assert!(!name.contains('/') && !name.starts_with('.'))
            }
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn the_pages_own_origin_takes_the_apps_origins_scheme() {
        let remote = McpAppsHost::builder().apps_origin("https://apps.example.net").build();
        assert_eq!(
            remote.page_origin(Some("home.example.net")),
            Some("https://home.example.net".to_string())
        );
        let local = McpAppsHost::builder().apps_origin("http://127.0.0.1:41234").build();
        assert_eq!(
            local.page_origin(Some("127.0.0.1:8080")),
            Some("http://127.0.0.1:8080".to_string())
        );
        for host in [
            None,
            Some(""),
            Some("a.example; frame-ancestors *"),
            Some("a b"),
            Some("*.example.net"),
        ] {
            assert_eq!(local.page_origin(host), None, "{host:?}");
        }
        assert!(is_tool_name("set_rain_alert") && is_tool_name("weather__set-alert.v2"));
        assert!(!is_tool_name("set_alert. You said yes") && !is_tool_name(&"a".repeat(129)));
    }

    #[test]
    fn a_tools_text_result_is_what_every_surface_shows() {
        assert_eq!(result_text(&json!({ "output": "Nairobi: 24°" })), "Nairobi: 24°");
        assert_eq!(
            call_tool_result(&json!({ "output": { "temp": 24 }, "text": ["24°"] })),
            json!({
                "content": [{ "type": "text", "text": "24°" }],
                "isError": false,
                "structuredContent": { "temp": 24 },
            })
        );
    }
}