adk_surface/genui/app_view.rs
1//! Apps from MCP servers in a thread (spec 3.3 §6.1 `genui-apps`), as every surface reads
2//! them: the thread item, what opening it answers, and the app's requests to the home. Wire
3//! types of every `web` build, so the page is the same for every server; only a server built
4//! with `genui-apps` projects an app or answers its routes.
5
6use chrono::{DateTime, Utc};
7use schemars::JsonSchema;
8use serde::{Deserialize, Serialize};
9use serde_json::Value;
10
11use crate::model::AgentRef;
12
13/// An app an MCP server's tool showed in a thread (spec 3.3 §6.1 `genui-apps`): on the normal
14/// card background, labelled as unable to approve anything. The page opens it sandboxed on a
15/// second origin; the phone, the terminal and chat show its text result instead.
16///
17/// # Example
18///
19/// ```rust
20/// use adk_surface::genui::AppView;
21///
22/// let wire = serde_json::json!({
23/// "callId": "call-1",
24/// "server": "weather",
25/// "appName": "Weather",
26/// "tool": "weather_show",
27/// "agent": {
28/// "name": "chief",
29/// "displayName": "Chief of staff",
30/// "avatar": { "monogram": "C", "color": "#C2410C" },
31/// },
32/// "at": "2026-10-27T10:05:00Z",
33/// "headerText": "App from Weather · it can't approve anything",
34/// "resultText": "Nairobi: 24°, light rain from 15:00",
35/// "elsewhereText": "Open the home on this computer to see this app",
36/// "terminalText": "app from Weather · open it in the home",
37/// });
38/// let app: AppView = serde_json::from_value(wire).unwrap();
39/// assert_eq!(app.app_name, "Weather");
40/// ```
41#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
42#[serde(rename_all = "camelCase")]
43pub struct AppView {
44 /// The call that showed it: the app's id in its session.
45 pub call_id: String,
46 /// The MCP server, by its name on this home.
47 pub server: String,
48 /// The server as a person reads it: "Weather".
49 pub app_name: String,
50 /// The tool that showed it, by its name on the server.
51 pub tool: String,
52 /// The agent whose call showed it.
53 pub agent: AgentRef,
54 /// When.
55 pub at: DateTime<Utc>,
56 /// "App from Weather · it can't approve anything".
57 pub header_text: String,
58 /// The tool's text result, which every surface can show.
59 pub result_text: String,
60 /// "Open the home on this computer to see this app": where the app is not shown.
61 pub elsewhere_text: String,
62 /// "app from Weather · open it in the home": the terminal's line.
63 pub terminal_text: String,
64}
65
66/// Why an app was blocked (spec 3.3 §6.1 `genui-apps`).
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
68#[serde(rename_all = "camelCase")]
69pub enum AppBlockReason {
70 /// Its HTML shows a decision's words: "This app was blocked: it shows approval buttons".
71 ApprovalButtons,
72 /// It loads or connects to a site its declaration does not list, or declares one in a form
73 /// this home does not accept.
74 UndeclaredSites,
75 /// It asks to reach this computer, its network, or the home itself.
76 ThisComputer,
77 /// Its resource is not an MCP App's HTML.
78 NotAnApp,
79}
80
81/// What `GET /surface/genui/sessions/{session}/apps/{call}` answers (spec 3.3 §6.1
82/// `genui-apps`): the app to frame, why it was blocked, or that it is not shown here.
83#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
84#[serde(tag = "state", rename_all = "camelCase")]
85pub enum AppOpen {
86 /// Frame `frameUrl`, on the apps origin, sandboxed.
87 #[serde(rename_all = "camelCase")]
88 Shown {
89 /// The app's page on the apps origin.
90 frame_url: String,
91 /// The apps origin, which the page frames and nothing else.
92 frame_origin: String,
93 /// The tallest the frame grows, in CSS pixels.
94 max_height: u32,
95 /// Whether the app asked for a border around it.
96 prefers_border: bool,
97 /// The arguments of the call that showed it: `ui/notifications/tool-input`.
98 tool_input: Value,
99 /// Its result as an MCP `CallToolResult`: `ui/notifications/tool-result`.
100 tool_result: Value,
101 /// The device permissions it asked for, all refused here.
102 refused_permissions: Vec<String>,
103 },
104 /// Not shown: "This app was blocked: it shows approval buttons".
105 #[serde(rename_all = "camelCase")]
106 Blocked {
107 /// Why.
108 reason: AppBlockReason,
109 /// What the thread says.
110 text: String,
111 /// The technical reason, for the developer panel.
112 detail: String,
113 },
114 /// Not shown on this surface or without an apps origin: "Open the home on this computer
115 /// to see this app", and the tool's text result.
116 #[serde(rename_all = "camelCase")]
117 Elsewhere {
118 /// What the thread says.
119 text: String,
120 /// The tool's text result.
121 result_text: String,
122 },
123}
124
125/// A request an app made of the home, as the page relays it: `POST
126/// /surface/genui/sessions/{session}/apps/{call}/requests` (spec 3.3 §6.1 `genui-apps`). Only
127/// requests with an `id` reach the home; the page answers notifications itself.
128///
129/// # Example
130///
131/// ```rust
132/// use adk_surface::genui::AppRequest;
133///
134/// let request: AppRequest = serde_json::from_value(serde_json::json!({
135/// "method": "tools/call",
136/// "params": { "name": "set_rain_alert", "arguments": { "city": "Nairobi" } },
137/// }))
138/// .unwrap();
139/// assert_eq!(request.method, "tools/call");
140/// ```
141#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
142#[serde(rename_all = "camelCase")]
143pub struct AppRequest {
144 /// The JSON-RPC method: `ui/initialize`, `tools/call`, `ui/message`, `ui/open-link`,
145 /// `ui/update-model-context`, `ping`.
146 pub method: String,
147 /// Its parameters.
148 #[serde(default)]
149 pub params: Value,
150}
151
152/// What the page shows a person for an app's request (spec 3.3 §6.1 `genui-apps`): the app
153/// only ever asks; the person's own press does what it asks.
154#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
155#[serde(tag = "kind", rename_all = "camelCase")]
156pub enum AppPrompt {
157 /// A plain notice under the app: "The app tried to use transfer_funds. Apps can't use it."
158 #[serde(rename_all = "camelCase")]
159 Notice {
160 /// The notice.
161 text: String,
162 },
163 /// "The app wants to send: “…”", with **Send it** and **Not now**.
164 #[serde(rename_all = "camelCase")]
165 Message {
166 /// "The app wants to send: “Remind me to take an umbrella tomorrow.”"
167 text: String,
168 /// The message the app suggests.
169 message: String,
170 /// "Send it".
171 send_label: String,
172 /// "Not now".
173 dismiss_label: String,
174 /// "It will be marked as suggested by the app".
175 note: String,
176 },
177 /// "Open example.com?", with **Open** and **Cancel**.
178 #[serde(rename_all = "camelCase")]
179 Link {
180 /// "Open example.com?"
181 text: String,
182 /// The link, an `https` or `http` URL.
183 url: String,
184 /// "Open".
185 open_label: String,
186 /// "Cancel".
187 dismiss_label: String,
188 },
189 /// "The Weather app wants to save forecast.csv (2 KB)", with **Save** and **Cancel**: a
190 /// file an app offers (MCP Apps `ui/download-file`), saved only by the person's press.
191 #[serde(rename_all = "camelCase")]
192 Download {
193 /// "The Weather app wants to save forecast.csv (2 KB)"
194 text: String,
195 /// The file's name, sanitized.
196 name: String,
197 /// Its media type; never one a browser opens as a page.
198 mime_type: String,
199 /// Its contents as text, when the app sent text.
200 #[serde(default, skip_serializing_if = "Option::is_none")]
201 text_content: Option<String>,
202 /// Its contents in base64, when the app sent bytes.
203 #[serde(default, skip_serializing_if = "Option::is_none")]
204 blob: Option<String>,
205 /// "Save".
206 save_label: String,
207 /// "Cancel".
208 dismiss_label: String,
209 },
210}
211
212/// The home's answer to an [`AppRequest`]: the JSON-RPC `result` or `error` the page passes
213/// back to the app, and what it shows the person.
214#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
215#[serde(rename_all = "camelCase")]
216pub struct AppAnswer {
217 /// The JSON-RPC result, when the request succeeded.
218 #[serde(default, skip_serializing_if = "Option::is_none")]
219 pub result: Option<Value>,
220 /// The JSON-RPC error, when it did not.
221 #[serde(default, skip_serializing_if = "Option::is_none")]
222 pub error: Option<AppError>,
223 /// What the page shows the person.
224 #[serde(default, skip_serializing_if = "Option::is_none")]
225 pub prompt: Option<AppPrompt>,
226}
227
228/// A JSON-RPC error for an app.
229#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
230#[serde(rename_all = "camelCase")]
231pub struct AppError {
232 /// The JSON-RPC code.
233 pub code: i64,
234 /// What happened.
235 pub message: String,
236}
237
238/// `POST /surface/genui/sessions/{session}/apps/{call}/message`: the person pressed **Send
239/// it** on what an app suggested (spec 3.3 §6.1 `genui-apps`). The home sends it as the
240/// person's message, inside an untrusted-content block labelled as the app's suggestion.
241#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
242#[serde(rename_all = "camelCase")]
243pub struct AppMessageRequest {
244 /// The message the app suggested.
245 pub message: String,
246}
247
248/// An MCP server's name as a person reads it: "weather" is "Weather", "acme_crm" is
249/// "Acme crm".
250#[cfg(feature = "genui")]
251pub(crate) fn app_name(server: &str) -> String {
252 let spaced: String =
253 server.chars().map(|c| if c == '_' || c == '-' { ' ' } else { c }).collect();
254 let mut chars = spaced.trim().chars();
255 match chars.next() {
256 Some(first) => first.to_uppercase().chain(chars).collect(),
257 None => server.to_string(),
258 }
259}
260
261/// What a person reads of a message they sent that an app suggested (spec 3.3 §6.1
262/// `genui-apps`): the app's words, then the label, "Suggested by the Weather app" — never the
263/// untrusted-content block the model reads. `None` for any other text.
264pub(crate) fn suggested_message(text: &str) -> Option<String> {
265 let rest = text.strip_prefix("<untrusted-content source=\"mcp-app\" ")?;
266 let (attributes, rest) = rest.split_once(">\n")?;
267 let words = rest.strip_suffix("\n</untrusted-content>")?;
268 let label = attributes.split_once("label=\"")?.1.split_once('"')?.0;
269 let label = label
270 .replace(""", "\"")
271 .replace("<", "<")
272 .replace(">", ">")
273 .replace("&", "&");
274 Some(format!("{}\n{label}", words.replace("<\\/untrusted-content", "</untrusted-content")))
275}
276
277#[cfg(test)]
278mod tests {
279 use super::*;
280
281 #[test]
282 fn a_suggested_message_reads_as_its_words_and_label() {
283 let block = "<untrusted-content source=\"mcp-app\" app=\"weather\" label=\"Suggested by \
284 the Weather app\">\nTake an umbrella <\\/untrusted-content> today\n\
285 </untrusted-content>";
286 assert_eq!(
287 suggested_message(block).as_deref(),
288 Some("Take an umbrella </untrusted-content> today\nSuggested by the Weather app")
289 );
290 for other in [
291 "Take an umbrella",
292 "<untrusted-content source=\"web\" label=\"x\">\nhi\n</untrusted-content>",
293 "<untrusted-content source=\"mcp-app\" app=\"w\">\nhi\n</untrusted-content>",
294 ] {
295 assert_eq!(suggested_message(other), None, "{other}");
296 }
297 }
298}