Skip to main content

adk_server/yaml_agent/
schema.rs

1//! YAML agent definition schema types.
2//!
3//! Defines the internal representation of YAML agent definition files,
4//! including model configuration, tool references, and sub-agent references.
5//! Unknown fields are preserved in a metadata map for forward compatibility.
6//!
7//! # Example
8//!
9//! ```yaml
10//! name: my_agent
11//! description: "A helpful assistant"
12//! model:
13//!   provider: gemini
14//!   model_id: gemini-3.8-flash
15//! instructions: |
16//!   You are a helpful assistant.
17//! tools:
18//!   - name: get_weather
19//!   - mcp:
20//!       endpoint: "npx @modelcontextprotocol/server-filesystem"
21//!       args: ["/data"]
22//! sub_agents:
23//!   - ref: researcher
24//! config:
25//!   temperature: 0.7
26//! ```
27
28use std::collections::HashMap;
29
30use serde::{Deserialize, Serialize};
31
32/// Top-level YAML agent definition.
33///
34/// Represents a complete agent configuration loaded from a YAML file.
35/// Unknown fields are captured in the `metadata` map for forward compatibility.
36#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
37#[serde(rename_all = "snake_case")]
38pub struct YamlAgentDefinition {
39    /// Agent name (required).
40    pub name: String,
41
42    /// Optional human-readable description.
43    #[serde(default)]
44    pub description: Option<String>,
45
46    /// Model configuration specifying provider and model ID.
47    pub model: ModelConfig,
48
49    /// Optional system instructions for the agent.
50    #[serde(default)]
51    pub instructions: Option<String>,
52
53    /// Tool references — either named tools or MCP server endpoints.
54    #[serde(default)]
55    pub tools: Vec<ToolReference>,
56
57    /// Sub-agent references resolved from other loaded YAML files.
58    #[serde(default)]
59    pub sub_agents: Vec<SubAgentReference>,
60
61    /// Arbitrary configuration parameters (e.g., temperature, max_tokens).
62    #[serde(default)]
63    pub config: HashMap<String, serde_json::Value>,
64
65    /// Plugin references for attaching lifecycle hooks to the agent.
66    #[serde(default)]
67    pub plugins: Vec<PluginReference>,
68
69    /// Session backend configuration for state persistence.
70    #[serde(default)]
71    pub session: Option<SessionConfig>,
72
73    /// Memory backend configuration for semantic memory.
74    #[serde(default)]
75    pub memory: Option<MemoryConfig>,
76
77    /// Forward-compatibility: unknown fields preserved here.
78    #[serde(flatten)]
79    pub metadata: HashMap<String, serde_json::Value>,
80}
81
82/// Model configuration for an agent.
83///
84/// Specifies which LLM provider and model to use, along with optional
85/// generation parameters.
86#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
87#[serde(rename_all = "snake_case")]
88pub struct ModelConfig {
89    /// LLM provider name (e.g., "gemini", "openai", "anthropic").
90    pub provider: String,
91
92    /// Model identifier within the provider (e.g., "gemini-3.8-flash").
93    pub model_id: String,
94
95    /// Optional sampling temperature.
96    #[serde(default)]
97    pub temperature: Option<f64>,
98
99    /// Optional maximum number of tokens to generate.
100    #[serde(default)]
101    pub max_tokens: Option<u32>,
102}
103
104/// A reference to a tool, either by name or as an MCP server endpoint.
105///
106/// Uses serde's untagged enum representation so that YAML entries like
107/// `- name: get_weather` and `- mcp: { endpoint: "..." }` are both accepted.
108#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
109#[serde(untagged)]
110pub enum ToolReference {
111    /// A tool resolved by name from the registered toolset.
112    Named {
113        /// Tool name to resolve.
114        name: String,
115    },
116    /// An MCP server tool reference.
117    Mcp {
118        /// MCP server configuration.
119        mcp: McpToolReference,
120    },
121}
122
123/// MCP server tool reference specifying an endpoint and optional arguments.
124#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
125pub struct McpToolReference {
126    /// MCP server endpoint command or URL.
127    pub endpoint: String,
128
129    /// Optional command-line arguments for the MCP server.
130    #[serde(default)]
131    pub args: Vec<String>,
132}
133
134/// A reference to a sub-agent by name.
135///
136/// The referenced agent is resolved from other loaded YAML definitions.
137#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
138pub struct SubAgentReference {
139    /// Name of the referenced sub-agent.
140    #[serde(rename = "ref")]
141    pub reference: String,
142}
143
144/// Plugin reference in a YAML agent definition.
145///
146/// Specifies a plugin by name with optional plugin-specific configuration.
147///
148/// # Example
149///
150/// ```yaml
151/// plugins:
152///   - name: telemetry
153///     config:
154///       endpoint: "http://localhost:4317"
155/// ```
156#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
157pub struct PluginReference {
158    /// Plugin name to resolve from the plugin registry.
159    pub name: String,
160
161    /// Optional plugin-specific configuration.
162    #[serde(default, skip_serializing_if = "Option::is_none")]
163    pub config: Option<serde_json::Value>,
164}
165
166/// Session backend configuration.
167///
168/// Specifies which session persistence backend to use and its options.
169///
170/// # Example
171///
172/// ```yaml
173/// session:
174///   backend: postgres
175///   connection_string: "${DATABASE_URL}"
176///   pool_size: 5
177/// ```
178#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
179pub struct SessionConfig {
180    /// Backend type: "inmemory", "sqlite", "postgres", or "redis".
181    pub backend: String,
182
183    /// Backend-specific configuration options (connection strings, TTL, pool size).
184    #[serde(flatten)]
185    pub options: HashMap<String, serde_json::Value>,
186}
187
188/// Memory backend configuration.
189///
190/// Specifies which memory/RAG backend to use and its options.
191///
192/// # Example
193///
194/// ```yaml
195/// memory:
196///   backend: postgres
197///   connection_string: "${DATABASE_URL}"
198/// ```
199#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
200pub struct MemoryConfig {
201    /// Backend type: "inmemory" or "postgres".
202    pub backend: String,
203
204    /// Backend-specific configuration options.
205    #[serde(flatten)]
206    pub options: HashMap<String, serde_json::Value>,
207}