|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | +use serde_json::Value; |
| 3 | +use std::collections::BTreeMap; |
| 4 | + |
| 5 | +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] |
| 6 | +pub enum McpAuthorizationMode { |
| 7 | + #[serde(rename = "none")] |
| 8 | + None, |
| 9 | + #[serde(rename = "agent-jwt")] |
| 10 | + AgentJwt, |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] |
| 14 | +pub struct McpAuthorizationMetadata { |
| 15 | + pub mode: McpAuthorizationMode, |
| 16 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 17 | + pub agent_jwt_audience: Option<String>, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] |
| 21 | +pub struct McpServiceInterfaceDiscovery { |
| 22 | + pub spec_version: String, |
| 23 | + pub endpoint: String, |
| 24 | + pub transport: String, |
| 25 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 26 | + pub authorization: Option<McpAuthorizationMetadata>, |
| 27 | + #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 28 | + pub tools: Vec<String>, |
| 29 | + #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 30 | + pub resources: Vec<String>, |
| 31 | + #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 32 | + pub prompts: Vec<String>, |
| 33 | + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] |
| 34 | + pub extra: BTreeMap<String, Value>, |
| 35 | +} |
| 36 | + |
| 37 | +#[cfg(test)] |
| 38 | +mod tests { |
| 39 | + use super::*; |
| 40 | + |
| 41 | + #[test] |
| 42 | + fn mcp_service_interface_discovery_round_trips() { |
| 43 | + let discovery = McpServiceInterfaceDiscovery { |
| 44 | + spec_version: "2025-11-25".to_owned(), |
| 45 | + endpoint: "https://profiles.example.com/mcp".to_owned(), |
| 46 | + transport: "streamable-http".to_owned(), |
| 47 | + authorization: Some(McpAuthorizationMetadata { |
| 48 | + mode: McpAuthorizationMode::AgentJwt, |
| 49 | + agent_jwt_audience: Some("https://profiles.example.com".to_owned()), |
| 50 | + }), |
| 51 | + tools: vec!["agent_profile.get".to_owned()], |
| 52 | + resources: vec!["agent-profile://profiles/{agent_id}".to_owned()], |
| 53 | + prompts: Vec::new(), |
| 54 | + extra: BTreeMap::new(), |
| 55 | + }; |
| 56 | + |
| 57 | + let encoded = serde_json::to_value(&discovery).unwrap(); |
| 58 | + assert_eq!(encoded["spec_version"], "2025-11-25"); |
| 59 | + assert_eq!(encoded["authorization"]["mode"], "agent-jwt"); |
| 60 | + assert!(encoded.get("prompts").is_none()); |
| 61 | + |
| 62 | + let decoded: McpServiceInterfaceDiscovery = serde_json::from_value(encoded).unwrap(); |
| 63 | + assert_eq!(decoded, discovery); |
| 64 | + } |
| 65 | +} |
0 commit comments