Skip to main content

adk_server/registry/
types.rs

1//! Agent card types for the registry.
2//!
3//! Defines [`AgentCard`] and related metadata types following the A2A protocol
4//! agent card schema with camelCase JSON serialization.
5
6use serde::{Deserialize, Serialize};
7
8/// Metadata document describing a registered agent's capabilities and endpoint.
9///
10/// Follows the A2A protocol agent card schema with camelCase JSON serialization.
11///
12/// `owner` is the user who registered the card, set by the registry routes from the
13/// authenticated caller; a value sent in a request body is ignored. A card without an owner
14/// — one stored before cards had owners, or inserted through an [`AgentRegistryStore`]
15/// directly — is readable by every authenticated caller and changed by no one through the
16/// routes; remove or replace it through the store.
17///
18/// [`AgentRegistryStore`]: super::store::AgentRegistryStore
19///
20/// # Example
21///
22/// ```rust
23/// use adk_server::registry::types::AgentCard;
24///
25/// let card = AgentCard {
26///     name: "my-agent".to_string(),
27///     version: "1.0.0".to_string(),
28///     description: Some("A helpful agent".to_string()),
29///     tags: vec!["search".to_string(), "qa".to_string()],
30///     endpoint_url: Some("https://example.com/agents/my-agent".to_string()),
31///     capabilities: vec!["text".to_string(), "tool-calling".to_string()],
32///     input_modes: vec!["text".to_string()],
33///     output_modes: vec!["text".to_string()],
34///     created_at: "2025-01-15T10:30:00Z".to_string(),
35///     updated_at: None,
36///     owner: None,
37/// };
38///
39/// let json = serde_json::to_string(&card).unwrap();
40/// assert!(json.contains("endpointUrl"));
41/// ```
42#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
43#[serde(rename_all = "camelCase")]
44pub struct AgentCard {
45    /// Unique agent name used as the primary identifier.
46    pub name: String,
47    /// Semantic version string (e.g. `"1.0.0"`).
48    pub version: String,
49    /// Optional human-readable description of the agent.
50    #[serde(default, skip_serializing_if = "Option::is_none")]
51    pub description: Option<String>,
52    /// Tags for categorization and filtering.
53    #[serde(default, skip_serializing_if = "Vec::is_empty")]
54    pub tags: Vec<String>,
55    /// URL where the agent can be reached.
56    #[serde(default, skip_serializing_if = "Option::is_none")]
57    pub endpoint_url: Option<String>,
58    /// Capabilities the agent supports (e.g. `"text"`, `"tool-calling"`).
59    #[serde(default, skip_serializing_if = "Vec::is_empty")]
60    pub capabilities: Vec<String>,
61    /// Supported input modes (e.g. `"text"`, `"audio"`).
62    #[serde(default, skip_serializing_if = "Vec::is_empty")]
63    pub input_modes: Vec<String>,
64    /// Supported output modes (e.g. `"text"`, `"audio"`).
65    #[serde(default, skip_serializing_if = "Vec::is_empty")]
66    pub output_modes: Vec<String>,
67    /// ISO 8601 timestamp of when the card was created.
68    pub created_at: String,
69    /// ISO 8601 timestamp of the last update, if any.
70    #[serde(default, skip_serializing_if = "Option::is_none")]
71    pub updated_at: Option<String>,
72    /// The user ID of the caller who registered the card; `None` on a card no one owns.
73    #[serde(default, skip_serializing_if = "Option::is_none")]
74    pub owner: Option<String>,
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn test_agent_card_serialization_camel_case() {
83        let card = AgentCard {
84            name: "test-agent".to_string(),
85            version: "1.0.0".to_string(),
86            description: Some("A test agent".to_string()),
87            tags: vec!["test".to_string()],
88            endpoint_url: Some("https://example.com".to_string()),
89            capabilities: vec!["text".to_string()],
90            input_modes: vec!["text".to_string()],
91            output_modes: vec!["text".to_string()],
92            created_at: "2025-01-15T10:30:00Z".to_string(),
93            updated_at: Some("2025-01-15T11:00:00Z".to_string()),
94            owner: Some("alice".to_string()),
95        };
96
97        let json = serde_json::to_string(&card).unwrap();
98        assert!(json.contains("\"endpointUrl\""));
99        assert!(json.contains("\"createdAt\""));
100        assert!(json.contains("\"updatedAt\""));
101        assert!(json.contains("\"inputModes\""));
102        assert!(json.contains("\"outputModes\""));
103        assert!(!json.contains("endpoint_url"));
104        assert!(!json.contains("created_at"));
105    }
106
107    #[test]
108    fn test_agent_card_round_trip() {
109        let card = AgentCard {
110            name: "my-agent".to_string(),
111            version: "2.0.0".to_string(),
112            description: None,
113            tags: vec![],
114            endpoint_url: None,
115            capabilities: vec![],
116            input_modes: vec![],
117            output_modes: vec![],
118            created_at: "2025-06-01T00:00:00Z".to_string(),
119            updated_at: None,
120            owner: None,
121        };
122
123        let json = serde_json::to_string(&card).unwrap();
124        let deserialized: AgentCard = serde_json::from_str(&json).unwrap();
125        assert_eq!(card, deserialized);
126    }
127
128    #[test]
129    fn test_agent_card_skips_empty_optional_fields() {
130        let card = AgentCard {
131            name: "minimal".to_string(),
132            version: "0.1.0".to_string(),
133            description: None,
134            tags: vec![],
135            endpoint_url: None,
136            capabilities: vec![],
137            input_modes: vec![],
138            output_modes: vec![],
139            created_at: "2025-01-01T00:00:00Z".to_string(),
140            updated_at: None,
141            owner: None,
142        };
143
144        let json = serde_json::to_string(&card).unwrap();
145        assert!(!json.contains("description"));
146        assert!(!json.contains("tags"));
147        assert!(!json.contains("endpointUrl"));
148        assert!(!json.contains("capabilities"));
149        assert!(!json.contains("inputModes"));
150        assert!(!json.contains("outputModes"));
151        assert!(!json.contains("updatedAt"));
152        assert!(!json.contains("owner"));
153    }
154
155    #[test]
156    fn test_agent_card_deserialize_from_camel_case_json() {
157        let json = r#"{
158            "name": "from-json",
159            "version": "1.0.0",
160            "description": "Deserialized agent",
161            "tags": ["search", "qa"],
162            "endpointUrl": "https://example.com/agents/from-json",
163            "capabilities": ["text", "tool-calling"],
164            "inputModes": ["text"],
165            "outputModes": ["text"],
166            "createdAt": "2025-01-15T10:30:00Z",
167            "updatedAt": "2025-01-15T10:30:00Z"
168        }"#;
169
170        let card: AgentCard = serde_json::from_str(json).unwrap();
171        assert_eq!(card.name, "from-json");
172        assert_eq!(card.endpoint_url, Some("https://example.com/agents/from-json".to_string()));
173        assert_eq!(card.tags, vec!["search", "qa"]);
174        assert_eq!(card.capabilities, vec!["text", "tool-calling"]);
175        assert_eq!(card.owner, None);
176    }
177
178    #[test]
179    fn test_agent_card_owner_round_trips() {
180        let json = r#"{"name": "owned", "version": "1.0.0", "createdAt": "2025-01-15T10:30:00Z",
181            "owner": "alice"}"#;
182
183        let card: AgentCard = serde_json::from_str(json).unwrap();
184        assert_eq!(card.owner.as_deref(), Some("alice"));
185        assert_eq!(serde_json::to_value(&card).unwrap()["owner"], "alice");
186    }
187}