Skip to main content

aether_core/core/
agent_deps.rs

1use crate::core::AgentRegistry;
2use crate::events::{AgentObserver, DynObserverFactory, TraceContext};
3use aether_auth::OAuthCredentialStorage;
4use rmcp::model::ClientCapabilities;
5use std::sync::Arc;
6
7/// Cross-cutting dependencies threaded to every agent a run spawns — the root
8/// agent and any sub-agents created by in-memory MCP servers. Bundling them
9/// keeps the plumbing through builders and servers a single value.
10#[derive(Clone, Default)]
11pub struct AgentDeps {
12    pub oauth_credential_store: Option<Arc<dyn OAuthCredentialStorage>>,
13    pub observer_factory: Option<DynObserverFactory>,
14    /// Remote trace these agents continue, set by whoever handled the request
15    /// that spawned them.
16    pub parent_trace_context: Option<TraceContext>,
17    pub agent_registry: AgentRegistry,
18    pub mcp_client_capabilities: Option<ClientCapabilities>,
19}
20
21impl AgentDeps {
22    pub fn new(
23        oauth_credential_store: Arc<dyn OAuthCredentialStorage>,
24        observer_factory: Option<DynObserverFactory>,
25    ) -> Self {
26        Self { oauth_credential_store: Some(oauth_credential_store), observer_factory, ..Self::default() }
27    }
28
29    /// Continue `parent`'s trace in every agent built from these deps.
30    pub fn with_parent_trace_context(mut self, parent: Option<TraceContext>) -> Self {
31        self.parent_trace_context = parent;
32        self
33    }
34
35    pub fn with_agent_registry(mut self, registry: AgentRegistry) -> Self {
36        self.agent_registry = registry;
37        self
38    }
39
40    pub fn with_mcp_client_capabilities(mut self, capabilities: ClientCapabilities) -> Self {
41        self.mcp_client_capabilities = Some(capabilities);
42        self
43    }
44
45    pub fn supports_mcp_url_elicitation(&self) -> bool {
46        self.mcp_client_capabilities
47            .as_ref()
48            .and_then(|capabilities| capabilities.elicitation.as_ref())
49            .is_some_and(|elicitation| elicitation.url.is_some())
50    }
51
52    /// A fresh observer isolated to one agent, if a factory is configured.
53    pub fn observer(&self, agent_name: &str) -> Option<Box<dyn AgentObserver>> {
54        self.observer_factory
55            .as_ref()
56            .map(|factory| factory.agent(Some(agent_name), self.parent_trace_context.as_ref()))
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63    use rmcp::model::{ElicitationCapability, FormElicitationCapability, UrlElicitationCapability};
64
65    #[test]
66    fn mcp_url_elicitation_support_requires_advertised_url_capability() {
67        assert!(!AgentDeps::default().supports_mcp_url_elicitation());
68
69        let mut form_only = ClientCapabilities::default();
70        form_only.elicitation = Some(ElicitationCapability::new().with_form(FormElicitationCapability::new()));
71        assert!(!AgentDeps::default().with_mcp_client_capabilities(form_only).supports_mcp_url_elicitation());
72
73        let mut url = ClientCapabilities::default();
74        url.elicitation = Some(ElicitationCapability::new().with_url(UrlElicitationCapability::new()));
75        assert!(AgentDeps::default().with_mcp_client_capabilities(url).supports_mcp_url_elicitation());
76    }
77}