use adk_core::{AdkError, Result};
use rmcp::model::{ResourceContents, Tool};
use serde::{Deserialize, Serialize};
use serde_json::Value;
pub const MCP_APPS_EXTENSION_ID: &str = "io.modelcontextprotocol/ui";
pub const MCP_APP_MIME_TYPE: &str = "text/html;profile=mcp-app";
pub const MCP_APPS_VERSION: &str = "2026-01-26";
pub const MCP_APP_STATE_KEY: &str = "adk.mcp.app";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct McpUiVisibility {
pub model: bool,
pub app: bool,
}
impl Default for McpUiVisibility {
fn default() -> Self {
Self { model: true, app: true }
}
}
impl McpUiVisibility {
pub fn of_tool(tool: &Tool) -> Self {
let listed = tool
.meta
.as_ref()
.and_then(|meta| meta.0.get("ui"))
.and_then(|ui| ui.get("visibility"))
.and_then(Value::as_array);
match listed {
Some(listed) => {
let has = |name: &str| listed.iter().any(|entry| entry.as_str() == Some(name));
Self { model: has("model"), app: has("app") }
}
None => Self::default(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct McpUiBinding {
pub tool: String,
pub resource_uri: String,
pub visibility: McpUiVisibility,
}
impl McpUiBinding {
pub fn of_tool(tool: &Tool) -> Option<Self> {
let ui = tool.meta.as_ref()?.0.get("ui")?;
let resource_uri = ui.get("resourceUri")?.as_str()?;
if !resource_uri.starts_with("ui://") {
return None;
}
Some(Self {
tool: tool.name.to_string(),
resource_uri: resource_uri.to_string(),
visibility: McpUiVisibility::of_tool(tool),
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct McpUiCsp {
#[serde(default)]
pub connect_domains: Vec<String>,
#[serde(default)]
pub resource_domains: Vec<String>,
#[serde(default)]
pub frame_domains: Vec<String>,
#[serde(default)]
pub base_uri_domains: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct McpAppResource {
pub uri: String,
pub mime_type: String,
pub html: String,
pub csp: McpUiCsp,
pub permissions: Vec<String>,
pub prefers_border: Option<bool>,
}
impl McpAppResource {
pub fn from_contents(uri: &str, contents: &[ResourceContents]) -> Result<Self> {
let found = contents.iter().find_map(|content| match content {
ResourceContents::TextResourceContents { uri: at, mime_type, text, meta }
if at == uri && mime_type.as_deref().is_some_and(is_app_mime) =>
{
Some((mime_type.clone().unwrap_or_default(), text, meta))
}
_ => None,
});
let Some((mime_type, html, meta)) = found else {
let found: Vec<String> = contents
.iter()
.map(|content| match content {
ResourceContents::TextResourceContents { mime_type, .. }
| ResourceContents::BlobResourceContents { mime_type, .. } => {
mime_type.clone().unwrap_or_else(|| "no MIME type".to_string())
}
_ => "unknown content".to_string(),
})
.collect();
return Err(AdkError::tool(format!(
"resource '{uri}' is not an MCP App: expected text with MIME type \
{MCP_APP_MIME_TYPE}, found {}",
if found.is_empty() { "nothing".to_string() } else { found.join(", ") }
)));
};
let ui = meta.as_ref().and_then(|meta| meta.0.get("ui"));
let csp = match ui.and_then(|ui| ui.get("csp")) {
Some(csp) => serde_json::from_value(csp.clone()).map_err(|error| {
AdkError::tool(format!(
"resource '{uri}' declares a _meta.ui.csp that is not lists of sites: {error}"
))
})?,
None => McpUiCsp::default(),
};
let mut permissions: Vec<String> = ui
.and_then(|ui| ui.get("permissions"))
.and_then(Value::as_object)
.map(|asked| asked.keys().cloned().collect())
.unwrap_or_default();
permissions.sort();
Ok(Self {
uri: uri.to_string(),
mime_type,
html: html.clone(),
csp,
permissions,
prefers_border: ui.and_then(|ui| ui.get("prefersBorder")).and_then(Value::as_bool),
})
}
}
fn is_app_mime(mime: &str) -> bool {
let normalized: String = mime.chars().filter(|c| !c.is_whitespace()).collect();
normalized.eq_ignore_ascii_case(MCP_APP_MIME_TYPE)
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct McpAppRecord {
pub server: String,
pub tool: String,
pub resource_uri: String,
pub call_id: String,
}