a2a_protocol_server/agent_card/
per_caller.rs1use std::future::Future;
11use std::pin::Pin;
12
13use a2a_protocol_types::agent_card::AgentCard;
14use a2a_protocol_types::extensions::AgentExtension;
15
16use crate::call_context::CallContext;
17use crate::error::{ServerError, ServerResult};
18use crate::handler::RequestHandler;
19
20pub trait ExtendedCardProvider: Send + Sync + 'static {
41 fn card_for<'a>(
47 &'a self,
48 ctx: &'a CallContext,
49 configured: &'a AgentCard,
50 ) -> Pin<Box<dyn Future<Output = ServerResult<AgentCard>> + Send + 'a>>;
51}
52
53pub async fn for_caller(
56 handler: &RequestHandler,
57 ctx: &CallContext,
58 configured: AgentCard,
59) -> ServerResult<AgentCard> {
60 let Some(provider) = &handler.extended_card_provider else {
61 return Ok(configured);
62 };
63 let card = provider.card_for(ctx, &configured).await?;
64 checked(
65 card,
66 handler.push_sender.is_some(),
67 &handler.advertised_extensions,
68 )
69}
70
71fn checked(
72 mut card: AgentCard,
73 has_push_sender: bool,
74 advertised: &[AgentExtension],
75) -> ServerResult<AgentCard> {
76 let refused = |why: String| {
77 ServerError::Internal(format!(
78 "the extended agent card chosen for this caller is not one this server may serve: {why}"
79 ))
80 };
81 super::lint::publishable(&card).map_err(refused)?;
82 if card.capabilities.push_notifications == Some(true) && !has_push_sender {
83 return Err(refused(
84 "it declares capabilities.pushNotifications, and no push sender is wired".to_owned(),
85 ));
86 }
87 crate::builder::advertise(
88 "extended agent card chosen for this caller",
89 &mut card,
90 advertised,
91 )
92 .map_err(|e| refused(e.to_string()))?;
93 Ok(card)
94}
95
96#[cfg(test)]
97mod tests {
98 use a2a_protocol_types::agent_card::{AgentCapabilities, AgentInterface, AgentSkill};
99 use a2a_protocol_types::extensions::AgentCardSignature;
100
101 use super::*;
102
103 fn card() -> AgentCard {
104 AgentCard::new("a", "1.0.0", AgentInterface::jsonrpc("http://localhost:1"))
105 .with_description("d")
106 .with_skill(AgentSkill::new("s", "S", "d").with_tags(["t"]))
107 .with_capabilities(AgentCapabilities::none())
108 }
109
110 fn advertised() -> Vec<AgentExtension> {
111 vec![AgentExtension::new("urn:example:ext")]
112 }
113
114 fn reason(result: ServerResult<AgentCard>) -> String {
115 match result {
116 Err(ServerError::Internal(why)) => why,
117 other => panic!("expected Internal, got {other:?}"),
118 }
119 }
120
121 #[test]
122 fn a_valid_card_is_served_with_the_advertised_extensions() {
123 let served = checked(card(), false, &advertised()).unwrap();
124 let uris: Vec<_> = served
125 .capabilities
126 .extensions
127 .iter()
128 .flatten()
129 .map(|e| e.uri.as_str())
130 .collect();
131 assert_eq!(uris, ["urn:example:ext"]);
132 }
133
134 #[test]
135 fn an_invalid_card_is_refused() {
136 let mut invalid = card();
137 invalid.skills.clear();
138 let why = reason(checked(invalid, false, &advertised()));
139 assert!(why.contains("not one this server may serve"), "{why}");
140 }
141
142 #[test]
143 fn push_notifications_need_a_sender() {
144 let mut push = card();
145 push.capabilities.push_notifications = Some(true);
146 let why = reason(checked(push.clone(), false, &advertised()));
147 assert!(why.contains("pushNotifications"), "{why}");
148 checked(push, true, &advertised()).expect("served with a sender");
149 }
150
151 #[test]
152 fn a_signed_card_missing_an_advertised_extension_is_refused() {
153 let mut signed = card();
154 signed.signatures = Some(vec![AgentCardSignature::new("e30", "c2ln")]);
155 let why = reason(checked(signed, false, &advertised()));
156 assert!(why.contains("carries a signature"), "{why}");
157 }
158}