adk-server 0.9.1

HTTP server and A2A protocol for Rust Agent Development Kit (ADK-Rust) agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! A2A interceptor framework for request/response middleware.
//!
//! This module provides an interceptor chain that can inspect, modify, or reject
//! A2A requests before and after delegation to the executor. Interceptors enable
//! cross-cutting concerns like authentication, rate limiting, and audit logging.
//!
//! # Architecture
//!
//! Interceptors execute in registration order for `before_delegation` (first registered
//! runs first) and in reverse order for `after_delegation` (last registered runs first).
//! This follows the standard middleware onion model.
//!
//! # Example
//!
//! ```rust
//! use adk_server::a2a::interceptor::{
//!     A2aDelegationContext, A2aError, A2aInterceptor, InterceptorChain, InterceptorDecision,
//! };
//! use async_trait::async_trait;
//!
//! struct LoggingInterceptor;
//!
//! #[async_trait]
//! impl A2aInterceptor for LoggingInterceptor {
//!     async fn before_delegation(
//!         &self,
//!         ctx: &mut A2aDelegationContext,
//!     ) -> Result<InterceptorDecision, A2aError> {
//!         println!("Before: method={}", ctx.method);
//!         Ok(InterceptorDecision::Continue)
//!     }
//!
//!     async fn after_delegation(
//!         &self,
//!         ctx: &A2aDelegationContext,
//!         response: &mut serde_json::Value,
//!     ) -> Result<(), A2aError> {
//!         println!("After: method={}", ctx.method);
//!         Ok(())
//!     }
//! }
//!
//! # tokio_test::block_on(async {
//! let chain = InterceptorChain::new().add(LoggingInterceptor);
//!
//! let mut ctx = A2aDelegationContext {
//!     method: "tasks/send".to_string(),
//!     params: serde_json::json!({}),
//!     caller_id: None,
//!     metadata: std::collections::HashMap::new(),
//! };
//!
//! let decision = chain.run_before(&mut ctx).await.unwrap();
//! assert!(matches!(decision, InterceptorDecision::Continue));
//! # });
//! ```

use std::collections::HashMap;
use std::fmt;

use async_trait::async_trait;

/// Error type for A2A interceptor operations.
///
/// Represents failures that can occur during interceptor execution,
/// including validation errors, authentication failures, and internal errors.
///
/// # Example
///
/// ```rust
/// use adk_server::a2a::interceptor::A2aError;
///
/// let err = A2aError::new("authentication failed: invalid token");
/// assert_eq!(err.to_string(), "A2A interceptor error: authentication failed: invalid token");
///
/// let rejected = A2aError::rejected(-32001, "rate limit exceeded");
/// assert_eq!(rejected.code(), Some(-32001));
/// ```
#[derive(Debug, Clone)]
pub struct A2aError {
    message: String,
    code: Option<i32>,
}

impl A2aError {
    /// Creates a new `A2aError` with the given message.
    pub fn new(message: impl Into<String>) -> Self {
        Self { message: message.into(), code: None }
    }

    /// Creates a new `A2aError` representing a JSON-RPC rejection with a code and message.
    pub fn rejected(code: i32, message: impl Into<String>) -> Self {
        Self { message: message.into(), code: Some(code) }
    }

    /// Returns the optional JSON-RPC error code associated with this error.
    pub fn code(&self) -> Option<i32> {
        self.code
    }
}

impl fmt::Display for A2aError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(code) = self.code {
            write!(f, "A2A interceptor error (code {code}): {}", self.message)
        } else {
            write!(f, "A2A interceptor error: {}", self.message)
        }
    }
}

impl std::error::Error for A2aError {}

impl From<A2aError> for adk_core::AdkError {
    fn from(err: A2aError) -> Self {
        adk_core::AdkError::agent(err.to_string())
    }
}

/// Context available to interceptors during A2A request processing.
///
/// Contains the JSON-RPC method, parameters, optional caller identity,
/// and arbitrary metadata that interceptors can read or modify.
///
/// # Example
///
/// ```rust
/// use adk_server::a2a::interceptor::A2aDelegationContext;
/// use std::collections::HashMap;
///
/// let ctx = A2aDelegationContext {
///     method: "tasks/send".to_string(),
///     params: serde_json::json!({"message": "hello"}),
///     caller_id: Some("agent-123".to_string()),
///     metadata: HashMap::from([("tenant".to_string(), "acme".to_string())]),
/// };
///
/// assert_eq!(ctx.method, "tasks/send");
/// assert_eq!(ctx.caller_id.as_deref(), Some("agent-123"));
/// ```
#[derive(Debug, Clone)]
pub struct A2aDelegationContext {
    /// The JSON-RPC method being invoked (e.g., `"tasks/send"`).
    pub method: String,
    /// The JSON-RPC parameters for the request.
    pub params: serde_json::Value,
    /// Optional identifier of the calling agent or client.
    pub caller_id: Option<String>,
    /// Arbitrary key-value metadata that interceptors can read or modify.
    pub metadata: HashMap<String, String>,
}

/// Decision returned by an interceptor's `before_delegation` method.
///
/// Controls whether the request continues through the chain, is short-circuited
/// with a pre-built response, or is rejected with a JSON-RPC error.
///
/// # Example
///
/// ```rust
/// use adk_server::a2a::interceptor::InterceptorDecision;
///
/// // Allow the request to proceed
/// let decision = InterceptorDecision::Continue;
///
/// // Short-circuit with a cached response
/// let decision = InterceptorDecision::ShortCircuit(serde_json::json!({"cached": true}));
///
/// // Reject with a JSON-RPC error
/// let decision = InterceptorDecision::Reject { code: -32001, message: "unauthorized".to_string() };
/// ```
#[derive(Debug, Clone)]
pub enum InterceptorDecision {
    /// Pass the request to the next interceptor or to the executor.
    Continue,
    /// Return the given response immediately without calling subsequent
    /// interceptors or the executor.
    ShortCircuit(serde_json::Value),
    /// Return a JSON-RPC error response with the given code and message.
    Reject {
        /// JSON-RPC error code.
        code: i32,
        /// Human-readable error message.
        message: String,
    },
}

/// Trait for A2A request/response interceptors.
///
/// Interceptors can inspect, modify, or reject requests before they reach the
/// executor, and can inspect or modify responses after the executor completes.
///
/// # Execution Order
///
/// - `before_delegation` runs in registration order (first added → first called).
/// - `after_delegation` runs in reverse registration order (last added → first called).
///
/// # Example
///
/// ```rust
/// use adk_server::a2a::interceptor::{
///     A2aDelegationContext, A2aError, A2aInterceptor, InterceptorDecision,
/// };
/// use async_trait::async_trait;
///
/// struct AuthInterceptor {
///     valid_token: String,
/// }
///
/// #[async_trait]
/// impl A2aInterceptor for AuthInterceptor {
///     async fn before_delegation(
///         &self,
///         ctx: &mut A2aDelegationContext,
///     ) -> Result<InterceptorDecision, A2aError> {
///         match ctx.metadata.get("authorization") {
///             Some(token) if token == &self.valid_token => Ok(InterceptorDecision::Continue),
///             _ => Ok(InterceptorDecision::Reject {
///                 code: -32001,
///                 message: "unauthorized".to_string(),
///             }),
///         }
///     }
///
///     async fn after_delegation(
///         &self,
///         _ctx: &A2aDelegationContext,
///         _response: &mut serde_json::Value,
///     ) -> Result<(), A2aError> {
///         Ok(())
///     }
/// }
/// ```
#[async_trait]
pub trait A2aInterceptor: Send + Sync {
    /// Inspect or modify the request before delegation.
    ///
    /// Return [`InterceptorDecision::Continue`] to pass to the next interceptor,
    /// [`InterceptorDecision::ShortCircuit`] to return a response immediately, or
    /// [`InterceptorDecision::Reject`] to return a JSON-RPC error.
    async fn before_delegation(
        &self,
        ctx: &mut A2aDelegationContext,
    ) -> Result<InterceptorDecision, A2aError>;

    /// Inspect or modify the response after delegation.
    ///
    /// Called in reverse registration order. Errors propagate immediately
    /// without calling remaining interceptors.
    async fn after_delegation(
        &self,
        ctx: &A2aDelegationContext,
        response: &mut serde_json::Value,
    ) -> Result<(), A2aError>;
}

/// An ordered chain of [`A2aInterceptor`] instances.
///
/// Executes `before_delegation` in registration order (first added runs first)
/// and `after_delegation` in reverse order (last added runs first), following
/// the standard middleware onion model.
///
/// # Example
///
/// ```rust
/// use adk_server::a2a::interceptor::{
///     A2aDelegationContext, A2aError, A2aInterceptor, InterceptorChain, InterceptorDecision,
/// };
/// use async_trait::async_trait;
///
/// struct PassthroughInterceptor;
///
/// #[async_trait]
/// impl A2aInterceptor for PassthroughInterceptor {
///     async fn before_delegation(
///         &self,
///         _ctx: &mut A2aDelegationContext,
///     ) -> Result<InterceptorDecision, A2aError> {
///         Ok(InterceptorDecision::Continue)
///     }
///
///     async fn after_delegation(
///         &self,
///         _ctx: &A2aDelegationContext,
///         _response: &mut serde_json::Value,
///     ) -> Result<(), A2aError> {
///         Ok(())
///     }
/// }
///
/// # tokio_test::block_on(async {
/// let chain = InterceptorChain::new()
///     .add(PassthroughInterceptor)
///     .add(PassthroughInterceptor);
///
/// let mut ctx = A2aDelegationContext {
///     method: "tasks/send".to_string(),
///     params: serde_json::json!({}),
///     caller_id: None,
///     metadata: std::collections::HashMap::new(),
/// };
///
/// let decision = chain.run_before(&mut ctx).await.unwrap();
/// assert!(matches!(decision, InterceptorDecision::Continue));
///
/// let mut response = serde_json::json!({"result": "ok"});
/// chain.run_after(&ctx, &mut response).await.unwrap();
/// # });
/// ```
pub struct InterceptorChain {
    interceptors: Vec<Box<dyn A2aInterceptor>>,
}

impl InterceptorChain {
    /// Creates a new empty interceptor chain.
    pub fn new() -> Self {
        Self { interceptors: Vec::new() }
    }

    /// Adds an interceptor to the end of the chain.
    ///
    /// Interceptors are executed in the order they are added for `before_delegation`,
    /// and in reverse order for `after_delegation`.
    #[allow(clippy::should_implement_trait)]
    pub fn add(mut self, interceptor: impl A2aInterceptor + 'static) -> Self {
        self.interceptors.push(Box::new(interceptor));
        self
    }

    /// Returns the number of interceptors in the chain.
    pub fn len(&self) -> usize {
        self.interceptors.len()
    }

    /// Returns `true` if the chain contains no interceptors.
    pub fn is_empty(&self) -> bool {
        self.interceptors.is_empty()
    }

    /// Runs all interceptors' `before_delegation` in registration order.
    ///
    /// Stops early if any interceptor returns [`InterceptorDecision::ShortCircuit`]
    /// or [`InterceptorDecision::Reject`].
    ///
    /// # Errors
    ///
    /// Returns [`A2aError`] if any interceptor fails.
    pub async fn run_before(
        &self,
        ctx: &mut A2aDelegationContext,
    ) -> Result<InterceptorDecision, A2aError> {
        for interceptor in &self.interceptors {
            match interceptor.before_delegation(ctx).await? {
                InterceptorDecision::Continue => continue,
                decision => return Ok(decision),
            }
        }
        Ok(InterceptorDecision::Continue)
    }

    /// Runs all interceptors' `after_delegation` in reverse registration order.
    ///
    /// Stops early if any interceptor returns an error.
    ///
    /// # Errors
    ///
    /// Returns [`A2aError`] if any interceptor fails.
    pub async fn run_after(
        &self,
        ctx: &A2aDelegationContext,
        response: &mut serde_json::Value,
    ) -> Result<(), A2aError> {
        for interceptor in self.interceptors.iter().rev() {
            interceptor.after_delegation(ctx, response).await?;
        }
        Ok(())
    }
}

impl Default for InterceptorChain {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Debug for InterceptorChain {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("InterceptorChain")
            .field("interceptor_count", &self.interceptors.len())
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    struct CountingInterceptor {
        id: usize,
        order: std::sync::Arc<std::sync::Mutex<Vec<(usize, &'static str)>>>,
    }

    #[async_trait]
    impl A2aInterceptor for CountingInterceptor {
        async fn before_delegation(
            &self,
            _ctx: &mut A2aDelegationContext,
        ) -> Result<InterceptorDecision, A2aError> {
            self.order.lock().unwrap().push((self.id, "before"));
            Ok(InterceptorDecision::Continue)
        }

        async fn after_delegation(
            &self,
            _ctx: &A2aDelegationContext,
            _response: &mut serde_json::Value,
        ) -> Result<(), A2aError> {
            self.order.lock().unwrap().push((self.id, "after"));
            Ok(())
        }
    }

    struct RejectingInterceptor;

    #[async_trait]
    impl A2aInterceptor for RejectingInterceptor {
        async fn before_delegation(
            &self,
            _ctx: &mut A2aDelegationContext,
        ) -> Result<InterceptorDecision, A2aError> {
            Ok(InterceptorDecision::Reject { code: -32001, message: "denied".to_string() })
        }

        async fn after_delegation(
            &self,
            _ctx: &A2aDelegationContext,
            _response: &mut serde_json::Value,
        ) -> Result<(), A2aError> {
            Ok(())
        }
    }

    struct ShortCircuitInterceptor;

    #[async_trait]
    impl A2aInterceptor for ShortCircuitInterceptor {
        async fn before_delegation(
            &self,
            _ctx: &mut A2aDelegationContext,
        ) -> Result<InterceptorDecision, A2aError> {
            Ok(InterceptorDecision::ShortCircuit(serde_json::json!({"cached": true})))
        }

        async fn after_delegation(
            &self,
            _ctx: &A2aDelegationContext,
            _response: &mut serde_json::Value,
        ) -> Result<(), A2aError> {
            Ok(())
        }
    }

    fn make_ctx() -> A2aDelegationContext {
        A2aDelegationContext {
            method: "tasks/send".to_string(),
            params: serde_json::json!({}),
            caller_id: None,
            metadata: HashMap::new(),
        }
    }

    #[tokio::test]
    async fn test_empty_chain_returns_continue() {
        let chain = InterceptorChain::new();
        let mut ctx = make_ctx();
        let decision = chain.run_before(&mut ctx).await.unwrap();
        assert!(matches!(decision, InterceptorDecision::Continue));
    }

    #[tokio::test]
    async fn test_before_executes_in_registration_order() {
        let order = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let chain = InterceptorChain::new()
            .add(CountingInterceptor { id: 1, order: order.clone() })
            .add(CountingInterceptor { id: 2, order: order.clone() })
            .add(CountingInterceptor { id: 3, order: order.clone() });

        let mut ctx = make_ctx();
        chain.run_before(&mut ctx).await.unwrap();

        let recorded = order.lock().unwrap();
        assert_eq!(recorded.as_slice(), &[(1, "before"), (2, "before"), (3, "before")]);
    }

    #[tokio::test]
    async fn test_after_executes_in_reverse_order() {
        let order = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let chain = InterceptorChain::new()
            .add(CountingInterceptor { id: 1, order: order.clone() })
            .add(CountingInterceptor { id: 2, order: order.clone() })
            .add(CountingInterceptor { id: 3, order: order.clone() });

        let ctx = make_ctx();
        let mut response = serde_json::json!({"result": "ok"});
        chain.run_after(&ctx, &mut response).await.unwrap();

        let recorded = order.lock().unwrap();
        assert_eq!(recorded.as_slice(), &[(3, "after"), (2, "after"), (1, "after")]);
    }

    #[tokio::test]
    async fn test_reject_stops_chain() {
        let order = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let chain = InterceptorChain::new()
            .add(CountingInterceptor { id: 1, order: order.clone() })
            .add(RejectingInterceptor)
            .add(CountingInterceptor { id: 3, order: order.clone() });

        let mut ctx = make_ctx();
        let decision = chain.run_before(&mut ctx).await.unwrap();

        assert!(matches!(decision, InterceptorDecision::Reject { code: -32001, .. }));
        let recorded = order.lock().unwrap();
        // Only interceptor 1 ran before the rejection
        assert_eq!(recorded.as_slice(), &[(1, "before")]);
    }

    #[tokio::test]
    async fn test_short_circuit_stops_chain() {
        let order = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let chain = InterceptorChain::new()
            .add(CountingInterceptor { id: 1, order: order.clone() })
            .add(ShortCircuitInterceptor)
            .add(CountingInterceptor { id: 3, order: order.clone() });

        let mut ctx = make_ctx();
        let decision = chain.run_before(&mut ctx).await.unwrap();

        match decision {
            InterceptorDecision::ShortCircuit(val) => {
                assert_eq!(val, serde_json::json!({"cached": true}));
            }
            _ => panic!("expected ShortCircuit"),
        }
        let recorded = order.lock().unwrap();
        assert_eq!(recorded.as_slice(), &[(1, "before")]);
    }

    #[tokio::test]
    async fn test_chain_len_and_is_empty() {
        let chain = InterceptorChain::new();
        assert!(chain.is_empty());
        assert_eq!(chain.len(), 0);

        let chain = chain.add(ShortCircuitInterceptor);
        assert!(!chain.is_empty());
        assert_eq!(chain.len(), 1);
    }

    #[tokio::test]
    async fn test_context_mutation_propagates() {
        struct MutatingInterceptor;

        #[async_trait]
        impl A2aInterceptor for MutatingInterceptor {
            async fn before_delegation(
                &self,
                ctx: &mut A2aDelegationContext,
            ) -> Result<InterceptorDecision, A2aError> {
                ctx.metadata.insert("enriched".to_string(), "true".to_string());
                Ok(InterceptorDecision::Continue)
            }

            async fn after_delegation(
                &self,
                _ctx: &A2aDelegationContext,
                _response: &mut serde_json::Value,
            ) -> Result<(), A2aError> {
                Ok(())
            }
        }

        let chain = InterceptorChain::new().add(MutatingInterceptor);
        let mut ctx = make_ctx();
        chain.run_before(&mut ctx).await.unwrap();

        assert_eq!(ctx.metadata.get("enriched"), Some(&"true".to_string()));
    }

    #[tokio::test]
    async fn test_a2a_error_display() {
        let err = A2aError::new("something failed");
        assert_eq!(err.to_string(), "A2A interceptor error: something failed");

        let err = A2aError::rejected(-32001, "rate limited");
        assert_eq!(err.to_string(), "A2A interceptor error (code -32001): rate limited");
        assert_eq!(err.code(), Some(-32001));
    }
}