Skip to main content

adk_server/
ui_protocol.rs

1//! UI protocol constants, capability specs, and normalization.
2//!
3//! These were originally in `adk-ui` but are inlined here so `adk-server`
4//! can be published independently without a UI-toolkit dependency.
5
6use serde::Serialize;
7
8/// Default runtime protocol profile for server integrations.
9pub const UI_DEFAULT_PROTOCOL: &str = "adk_ui";
10
11/// Tool envelope version used by protocol-aware legacy tool responses.
12pub const TOOL_ENVELOPE_VERSION: &str = "1.0";
13
14/// Supported runtime protocol profile values.
15pub const SUPPORTED_UI_PROTOCOLS: &[&str] = &["adk_ui", "a2ui", "ag_ui", "mcp_apps"];
16
17/// Planned deprecation metadata for runtime/profile consumers.
18#[derive(Debug, Clone, Serialize)]
19#[serde(rename_all = "camelCase")]
20pub struct UiProtocolDeprecationSpec {
21    pub stage: &'static str,
22    pub announced_on: &'static str,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub sunset_target_on: Option<&'static str>,
25    pub replacement_protocols: &'static [&'static str],
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub note: Option<&'static str>,
28}
29
30/// Implementation maturity for protocol support exposed to clients.
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
32#[serde(rename_all = "snake_case")]
33pub enum UiProtocolImplementationTier {
34    Legacy,
35    NativeSubset,
36    HybridSubset,
37    CompatibilitySubset,
38}
39
40/// Upstream specification track referenced by the runtime capability signal.
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
42#[serde(rename_all = "snake_case")]
43pub enum UiProtocolSpecTrack {
44    Internal,
45    Stable,
46    Draft,
47}
48
49/// Static capability contract for each supported UI protocol.
50#[derive(Debug, Clone, Serialize)]
51#[serde(rename_all = "camelCase")]
52pub struct UiProtocolCapabilitySpec {
53    pub protocol: &'static str,
54    pub versions: &'static [&'static str],
55    pub implementation_tier: UiProtocolImplementationTier,
56    pub spec_track: UiProtocolSpecTrack,
57    pub summary: &'static str,
58    pub features: &'static [&'static str],
59    pub limitations: &'static [&'static str],
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub deprecation: Option<&'static UiProtocolDeprecationSpec>,
62}
63
64pub const ADK_UI_LEGACY_DEPRECATION: UiProtocolDeprecationSpec = UiProtocolDeprecationSpec {
65    stage: "planned",
66    announced_on: "2026-02-07",
67    sunset_target_on: Some("2026-12-31"),
68    replacement_protocols: &["a2ui", "ag_ui", "mcp_apps"],
69    note: Some("Legacy adk_ui profile remains supported during migration."),
70};
71
72pub const UI_PROTOCOL_CAPABILITIES: &[UiProtocolCapabilitySpec] = &[
73    UiProtocolCapabilitySpec {
74        protocol: "adk_ui",
75        versions: &["1.0"],
76        implementation_tier: UiProtocolImplementationTier::Legacy,
77        spec_track: UiProtocolSpecTrack::Internal,
78        summary: "Legacy internal runtime profile retained for backward compatibility during migration.",
79        features: &["legacy_components", "theme", "events"],
80        limitations: &[
81            "Deprecated for new integrations.",
82            "Does not represent a standard external protocol contract.",
83        ],
84        deprecation: Some(&ADK_UI_LEGACY_DEPRECATION),
85    },
86    UiProtocolCapabilitySpec {
87        protocol: "a2ui",
88        versions: &["0.9"],
89        implementation_tier: UiProtocolImplementationTier::HybridSubset,
90        spec_track: UiProtocolSpecTrack::Draft,
91        summary: "A2UI-aligned JSONL surface output through protocol-aware UI tools and tool envelopes.",
92        features: &["jsonl", "createSurface", "updateComponents", "updateDataModel"],
93        limitations: &[
94            "The runtime stream is still the generic ADK event envelope rather than a dedicated A2UI transport.",
95            "The advertised shape is aligned to the draft v0.9 specification and should be treated as draft-compatible support.",
96        ],
97        deprecation: None,
98    },
99    UiProtocolCapabilitySpec {
100        protocol: "ag_ui",
101        versions: &["0.1", "1.0"],
102        implementation_tier: UiProtocolImplementationTier::HybridSubset,
103        spec_track: UiProtocolSpecTrack::Stable,
104        summary: "Hybrid AG-UI subset with additive protocol-native runtime transport, dual-path AG-UI run input support, and compatibility wrappers for existing SSE consumers.",
105        features: &[
106            "run_lifecycle",
107            "custom_events",
108            "event_stream",
109            "native_runtime_transport",
110            "native_run_input_subset",
111            "state_snapshot",
112            "messages_snapshot",
113            "stable_text_events",
114            "stable_tool_events",
115            // Spec 3.3 §10 S2: held calls end the run as `tool_call` interrupts (AG-UI 1.0
116            // lifecycle). `interrupt_resume` is added by a server that can decide them.
117            "tool_call_interrupts",
118            // A client that sends `protocolVersion: "1.0"` gets AG-UI 1.0's run shape: a
119            // success outcome, snapshots before an interrupt, server-executed tool calls
120            // marked, strict event fields, and resumes that answer every open interrupt.
121            "protocol_version_negotiation",
122        ],
123        limitations: &[
124            "Protocol-native AG-UI transport is opt-in and currently scoped to `adk-server`; the default runtime stream remains the generic ADK wrapper for compatibility.",
125            "The framework translates generic ADK runtime events into the stable AG-UI subset at the server boundary, but it does not yet emit the full activity, reasoning, or agent-native AG-UI event families end-to-end from `adk-agent`.",
126            "Held calls end a run as `tool_call` interrupts only on the protocol-native transport. Interrupts are answered with `resume` (feature `interrupt_resume`) only on a server with an approval service and a request-context extractor; elsewhere an interrupt has no `responseSchema` and is decided where the call is held.",
127        ],
128        deprecation: None,
129    },
130    UiProtocolCapabilitySpec {
131        protocol: "mcp_apps",
132        versions: &["sep-1865"],
133        implementation_tier: UiProtocolImplementationTier::CompatibilitySubset,
134        spec_track: UiProtocolSpecTrack::Stable,
135        summary: "Compatibility-oriented MCP Apps profile with a ui:// resource registry, additive initialize/message/model-context bridge endpoints, and HTML/resource adapters.",
136        features: &[
137            "ui_resource_uri",
138            "tool_meta",
139            "html_resource",
140            "initialize_bridge_endpoint",
141            "message_bridge_endpoint",
142            "update_model_context_bridge_endpoint",
143            "notification_poll_endpoint",
144            "jsonrpc_bridge_envelope",
145            "runtime_bridge_request_fields",
146            "initialized_notification",
147            "resource_list_changed_notification",
148            "tool_list_changed_notification",
149        ],
150        limitations: &[
151            "The framework now exposes additive HTTP helpers plus notification polling for initialize, message, update-model-context, initialized, and list-changed host flows, but it does not yet provide a full browser postMessage transport.",
152            "Current MCP Apps support is still adapter-driven, and bridge session state remains in-memory rather than durably coupled to the runtime/session lifecycle.",
153        ],
154        deprecation: None,
155    },
156];
157
158/// The `ag_ui` feature a server lists when it answers interrupts with `resume`
159/// (spec 3.3 §10 S2): it has an approval service and authenticates callers.
160pub(crate) const AG_UI_RESUME_FEATURE: &str = "interrupt_resume";
161
162/// The `ag_ui` feature a server lists when its AG-UI extension sends agent-drawn surfaces as
163/// `a2ui-surface` activities (spec 3.3 §6.1 `genui`); only a server with that extension wired
164/// lists it.
165pub const AG_UI_A2UI_ACTIVITY_FEATURE: &str = "a2ui_activity";
166
167/// The `activityType` of an agent-drawn surface in an AG-UI `ACTIVITY_SNAPSHOT` (spec 3.3
168/// §6.1 `genui`): a community convention, not part of the AG-UI specification.
169pub const A2UI_SURFACE_ACTIVITY_TYPE: &str = "a2ui-surface";
170
171/// Normalize runtime UI profile aliases to canonical values.
172pub fn normalize_runtime_ui_protocol(raw: &str) -> Option<&'static str> {
173    match raw.trim().to_ascii_lowercase().as_str() {
174        "adk_ui" => Some("adk_ui"),
175        "a2ui" => Some("a2ui"),
176        "ag_ui" | "ag-ui" => Some("ag_ui"),
177        "mcp_apps" | "mcp-apps" => Some("mcp_apps"),
178        _ => None,
179    }
180}
181
182#[cfg(test)]
183mod tests {
184    use super::*;
185
186    #[test]
187    fn normalize_runtime_protocol_accepts_aliases() {
188        assert_eq!(normalize_runtime_ui_protocol("adk_ui"), Some("adk_ui"));
189        assert_eq!(normalize_runtime_ui_protocol("A2UI"), Some("a2ui"));
190        assert_eq!(normalize_runtime_ui_protocol("ag-ui"), Some("ag_ui"));
191        assert_eq!(normalize_runtime_ui_protocol("mcp-apps"), Some("mcp_apps"));
192        assert_eq!(normalize_runtime_ui_protocol("unknown"), None);
193    }
194
195    #[test]
196    fn capability_specs_cover_supported_protocols() {
197        let protocols: Vec<&str> =
198            UI_PROTOCOL_CAPABILITIES.iter().map(|spec| spec.protocol).collect();
199        assert_eq!(protocols, SUPPORTED_UI_PROTOCOLS);
200    }
201
202    #[test]
203    fn capability_specs_include_versions() {
204        for spec in UI_PROTOCOL_CAPABILITIES {
205            assert!(!spec.versions.is_empty(), "missing versions for {}", spec.protocol);
206            assert!(!spec.features.is_empty(), "missing features for {}", spec.protocol);
207            assert!(!spec.summary.trim().is_empty(), "missing summary for {}", spec.protocol);
208            assert!(!spec.limitations.is_empty(), "missing limitations for {}", spec.protocol);
209        }
210    }
211
212    #[test]
213    fn legacy_profile_has_deprecation_metadata() {
214        let legacy = UI_PROTOCOL_CAPABILITIES
215            .iter()
216            .find(|spec| spec.protocol == "adk_ui")
217            .expect("adk_ui capability");
218        let deprecation = legacy.deprecation.expect("adk_ui deprecation metadata");
219        assert_eq!(deprecation.announced_on, "2026-02-07");
220        assert_eq!(deprecation.sunset_target_on, Some("2026-12-31"));
221    }
222
223    #[test]
224    fn capability_specs_capture_support_boundaries() {
225        let a2ui = UI_PROTOCOL_CAPABILITIES
226            .iter()
227            .find(|spec| spec.protocol == "a2ui")
228            .expect("a2ui capability");
229        assert_eq!(a2ui.implementation_tier, UiProtocolImplementationTier::HybridSubset);
230        assert_eq!(a2ui.spec_track, UiProtocolSpecTrack::Draft);
231
232        let ag_ui = UI_PROTOCOL_CAPABILITIES
233            .iter()
234            .find(|spec| spec.protocol == "ag_ui")
235            .expect("ag_ui capability");
236        assert_eq!(ag_ui.implementation_tier, UiProtocolImplementationTier::HybridSubset);
237        assert_eq!(ag_ui.spec_track, UiProtocolSpecTrack::Stable);
238
239        let mcp_apps = UI_PROTOCOL_CAPABILITIES
240            .iter()
241            .find(|spec| spec.protocol == "mcp_apps")
242            .expect("mcp_apps capability");
243        assert_eq!(mcp_apps.implementation_tier, UiProtocolImplementationTier::CompatibilitySubset);
244        assert_eq!(mcp_apps.spec_track, UiProtocolSpecTrack::Stable);
245    }
246
247    #[test]
248    fn capability_specs_serialize_support_metadata_in_camel_case() {
249        let value = serde_json::to_value(UI_PROTOCOL_CAPABILITIES).expect("serialize capabilities");
250        let protocols = value.as_array().expect("capabilities array");
251        let ag_ui =
252            protocols.iter().find(|spec| spec["protocol"] == "ag_ui").expect("ag_ui capability");
253
254        assert_eq!(ag_ui["implementationTier"], "hybrid_subset");
255        assert_eq!(ag_ui["specTrack"], "stable");
256        assert!(ag_ui["summary"].as_str().is_some());
257        assert!(ag_ui["limitations"].as_array().is_some_and(|limitations| !limitations.is_empty()));
258    }
259}