1pub mod agent_card;
54pub mod executor;
55pub mod handler;
56pub mod jsonrpc;
57pub mod middleware;
58pub mod pagination;
59pub mod push;
60pub mod rest;
61pub mod sse;
62pub mod task_store;
63
64#[cfg(feature = "fuzzing")]
67#[doc(hidden)]
68pub mod fuzz_support {
69 use a2a::A2AError;
70
71 pub fn validate_push_url(url: &str) -> Result<(), A2AError> {
72 crate::push::sender::validate_push_url(url)
73 }
74
75 pub fn is_blocked_ip(ip: std::net::IpAddr) -> bool {
76 crate::push::sender::is_blocked_ip(ip)
77 }
78}
79
80#[cfg(any(feature = "rustls-tls", feature = "rustls-no-provider"))]
81pub mod tls;
82
83pub use agent_card::{AgentCardProducer, StaticAgentCard, WELL_KNOWN_AGENT_CARD_PATH};
84pub use executor::{AgentExecutor, ExecutorContext};
85pub use handler::{
86 DefaultRequestHandler, ExtendedAgentCardResolver, RequestAuthorizer, RequestHandler,
87};
88pub use middleware::{CallContext, CallInterceptor, InterceptedHandler, ServiceParams, User};
89pub use push::{HttpPushSender, HttpPushSenderConfig, InMemoryPushConfigStore, PushConfigStore};
90pub use task_store::{InMemoryTaskStore, TaskStore};
91
92pub(crate) fn sanitized_internal_error(detail: impl std::fmt::Display) -> a2a::A2AError {
104 tracing::error!(error = %detail, "internal error; returning a generic message to the client");
105 a2a::A2AError::internal("Internal error")
106}
107
108#[cfg(test)]
109pub(crate) mod test_util {
110 use std::collections::HashMap;
111 use std::sync::{Arc, Mutex};
112
113 use a2a::*;
114 use async_trait::async_trait;
115 use futures::stream::BoxStream;
116
117 use crate::handler::RequestHandler;
118 use crate::middleware::ServiceParams;
119
120 pub fn install_crypto_provider() {
121 #[cfg(feature = "rustls-tls")]
122 {
123 let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
124 }
125 }
126
127 pub(crate) type CapturedParams = Arc<Mutex<HashMap<&'static str, ServiceParams>>>;
128
129 pub(crate) struct CapturingHandler {
136 captured: CapturedParams,
137 tenants: Mutex<HashMap<&'static str, Option<String>>>,
138 ids: Mutex<HashMap<&'static str, String>>,
139 }
140
141 impl CapturingHandler {
142 pub fn new() -> (Arc<Self>, CapturedParams) {
143 let captured = Arc::new(Mutex::new(HashMap::new()));
144 let handler = Arc::new(CapturingHandler {
145 captured: captured.clone(),
146 tenants: Mutex::new(HashMap::new()),
147 ids: Mutex::new(HashMap::new()),
148 });
149 (handler, captured)
150 }
151
152 fn record(&self, method: &'static str, params: &ServiceParams, tenant: Option<&str>) {
153 self.captured.lock().unwrap().insert(method, params.clone());
154 self.tenants
155 .lock()
156 .unwrap()
157 .insert(method, tenant.map(str::to_owned));
158 }
159
160 pub fn take_tenant(&self, method: &str) -> Option<Option<String>> {
162 self.tenants.lock().unwrap().remove(method)
163 }
164
165 pub fn take_id(&self, method: &str) -> Option<String> {
166 self.ids.lock().unwrap().remove(method)
167 }
168 }
169
170 #[async_trait]
171 impl RequestHandler for CapturingHandler {
172 async fn send_message(
173 &self,
174 params: &ServiceParams,
175 req: SendMessageRequest,
176 ) -> Result<SendMessageResponse, A2AError> {
177 self.record("send_message", params, req.tenant.as_deref());
178 Err(A2AError::internal("stub"))
179 }
180
181 async fn send_streaming_message(
182 &self,
183 params: &ServiceParams,
184 req: SendMessageRequest,
185 ) -> Result<BoxStream<'static, Result<StreamResponse, A2AError>>, A2AError> {
186 self.record("send_streaming_message", params, req.tenant.as_deref());
187 Ok(Box::pin(futures::stream::empty()))
188 }
189
190 async fn get_task(
191 &self,
192 params: &ServiceParams,
193 req: GetTaskRequest,
194 ) -> Result<Task, A2AError> {
195 self.ids.lock().unwrap().insert("get_task", req.id.clone());
196 self.record("get_task", params, req.tenant.as_deref());
197 Err(A2AError::task_not_found("stub"))
198 }
199
200 async fn list_tasks(
201 &self,
202 params: &ServiceParams,
203 req: ListTasksRequest,
204 ) -> Result<ListTasksResponse, A2AError> {
205 self.record("list_tasks", params, req.tenant.as_deref());
206 Ok(ListTasksResponse {
207 tasks: Vec::new(),
208 next_page_token: String::new(),
209 page_size: 0,
210 total_size: 0,
211 })
212 }
213
214 async fn cancel_task(
215 &self,
216 params: &ServiceParams,
217 req: CancelTaskRequest,
218 ) -> Result<Task, A2AError> {
219 self.ids
220 .lock()
221 .unwrap()
222 .insert("cancel_task", req.id.clone());
223 self.record("cancel_task", params, req.tenant.as_deref());
224 Err(A2AError::task_not_found("stub"))
225 }
226
227 async fn subscribe_to_task(
228 &self,
229 params: &ServiceParams,
230 req: SubscribeToTaskRequest,
231 ) -> Result<BoxStream<'static, Result<StreamResponse, A2AError>>, A2AError> {
232 self.ids
233 .lock()
234 .unwrap()
235 .insert("subscribe_to_task", req.id.clone());
236 self.record("subscribe_to_task", params, req.tenant.as_deref());
237 Ok(Box::pin(futures::stream::empty()))
238 }
239
240 async fn create_push_config(
241 &self,
242 params: &ServiceParams,
243 req: TaskPushNotificationConfig,
244 ) -> Result<TaskPushNotificationConfig, A2AError> {
245 self.record("create_push_config", params, req.tenant.as_deref());
246 Ok(req)
247 }
248
249 async fn get_push_config(
250 &self,
251 params: &ServiceParams,
252 req: GetTaskPushNotificationConfigRequest,
253 ) -> Result<TaskPushNotificationConfig, A2AError> {
254 self.record("get_push_config", params, req.tenant.as_deref());
255 Err(A2AError::internal("stub"))
256 }
257
258 async fn list_push_configs(
259 &self,
260 params: &ServiceParams,
261 req: ListTaskPushNotificationConfigsRequest,
262 ) -> Result<ListTaskPushNotificationConfigsResponse, A2AError> {
263 self.record("list_push_configs", params, req.tenant.as_deref());
264 Ok(ListTaskPushNotificationConfigsResponse {
265 configs: Vec::new(),
266 next_page_token: None,
267 })
268 }
269
270 async fn delete_push_config(
271 &self,
272 params: &ServiceParams,
273 req: DeleteTaskPushNotificationConfigRequest,
274 ) -> Result<(), A2AError> {
275 self.record("delete_push_config", params, req.tenant.as_deref());
276 Ok(())
277 }
278
279 async fn get_extended_agent_card(
280 &self,
281 params: &ServiceParams,
282 req: GetExtendedAgentCardRequest,
283 ) -> Result<AgentCard, A2AError> {
284 self.record("get_extended_agent_card", params, req.tenant.as_deref());
285 Err(A2AError::internal("stub"))
286 }
287 }
288
289 #[track_caller]
291 pub(crate) fn assert_header_captured(
292 captured: &CapturedParams,
293 method: &'static str,
294 header: &str,
295 expected: &str,
296 ) {
297 let captured = captured.lock().unwrap();
298 let params = captured
299 .get(method)
300 .unwrap_or_else(|| panic!("{method} should have been called; captured={captured:?}"));
301 assert_eq!(
302 params.get(header),
303 Some(&vec![expected.to_string()]),
304 "{header} should be propagated to {method}; got {params:?}",
305 );
306 }
307}