1use crate::ai::ToolModelConfig;
13use crate::auth::{JwtAudience, Permission};
14use crate::errors::ConfigValidationError;
15use crate::mcp::capabilities::ToolVisibility;
16use serde::{Deserialize, Serialize};
17use std::collections::HashMap;
18use systemprompt_identifiers::ClientId;
19
20#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
21pub enum McpServerType {
22 #[default]
23 #[serde(rename = "internal")]
24 Internal,
25 #[serde(rename = "external")]
26 External,
27}
28
29impl McpServerType {
30 pub const fn as_str(&self) -> &'static str {
31 match self {
32 Self::Internal => "internal",
33 Self::External => "external",
34 }
35 }
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize, Default)]
39pub struct ToolUiConfig {
40 #[serde(default = "default_resource_uri_template")]
41 pub resource_uri_template: String,
42 #[serde(default = "default_visibility_enum")]
43 pub visibility: Vec<ToolVisibility>,
44}
45
46fn default_resource_uri_template() -> String {
47 "ui://systemprompt/{artifact_id}".to_owned()
48}
49
50fn default_visibility_enum() -> Vec<ToolVisibility> {
51 vec![ToolVisibility::Model, ToolVisibility::App]
52}
53
54#[derive(Debug, Clone, Serialize, Deserialize, Default)]
55pub struct ToolMetadata {
56 #[serde(default)]
57 pub terminal_on_success: bool,
58 #[serde(skip_serializing_if = "Option::is_none")]
59 pub model_config: Option<ToolModelConfig>,
60 #[serde(skip_serializing_if = "Option::is_none")]
61 pub ui: Option<ToolUiConfig>,
62}
63
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct DeploymentConfig {
66 pub deployments: HashMap<String, Deployment>,
67 pub settings: Settings,
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct Deployment {
72 #[serde(default, alias = "type")]
73 pub server_type: McpServerType,
74 pub binary: String,
75 pub package: Option<String>,
76 pub port: u16,
77 #[serde(default, skip_serializing_if = "Option::is_none")]
78 pub endpoint: Option<String>,
79 pub enabled: bool,
80 pub display_in_web: bool,
81 #[serde(default)]
82 pub dev_only: bool,
83 #[serde(default)]
84 pub schemas: Vec<SchemaDefinition>,
85 pub oauth: OAuthRequirement,
86 #[serde(default)]
87 pub tools: HashMap<String, ToolMetadata>,
88 #[serde(skip_serializing_if = "Option::is_none")]
89 pub model_config: Option<ToolModelConfig>,
90 #[serde(default)]
91 pub env_vars: Vec<String>,
92 #[serde(default, skip_serializing_if = "Option::is_none")]
93 pub external_auth: Option<ExternalAuth>,
94 #[serde(default, skip_serializing_if = "Option::is_none")]
95 pub connector: Option<ConnectorConfig>,
96 #[serde(default, skip_serializing_if = "HashMap::is_empty")]
97 pub headers: HashMap<String, String>,
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize)]
109pub struct ExternalAuth {
110 pub token_endpoint: String,
111 #[serde(default = "default_auth_header")]
112 pub header: String,
113 #[serde(default = "default_auth_scheme")]
114 pub scheme: String,
115}
116
117fn default_auth_header() -> String {
118 "Authorization".to_owned()
119}
120
121fn default_auth_scheme() -> String {
122 "Bearer".to_owned()
123}
124
125impl ExternalAuth {
126 pub fn header_value(&self, bearer: &str) -> String {
127 if self.scheme.trim().is_empty() {
128 bearer.to_owned()
129 } else {
130 format!("{} {bearer}", self.scheme)
131 }
132 }
133}
134
135impl Deployment {
136 pub fn validate(&self, name: &str) -> Result<(), ConfigValidationError> {
137 if matches!(self.server_type, McpServerType::Internal) {
138 if let Some(ep) = self.endpoint.as_deref()
139 && (ep.starts_with("http://") || ep.starts_with("https://"))
140 {
141 return Err(ConfigValidationError::invalid_field(format!(
142 "MCP server '{name}': endpoint must be a relative path (e.g. \
143 /api/v1/mcp/{name}/mcp) or omitted; the host is derived from \
144 server.api_external_url. Remove the scheme+host prefix."
145 )));
146 }
147 if self.external_auth.is_some() || self.connector.is_some() || !self.headers.is_empty()
148 {
149 return Err(ConfigValidationError::invalid_field(format!(
150 "MCP server '{name}': external_auth and headers are only valid on \
151 external servers; internal servers are reached through the gateway \
152 with the systemprompt credential."
153 )));
154 }
155 }
156
157 if let Some(connector) = self.connector.as_ref() {
158 if connector.adapter != "generic"
159 || !self
160 .endpoint
161 .as_deref()
162 .is_some_and(|endpoint| endpoint.starts_with("https://"))
163 {
164 return Err(ConfigValidationError::invalid_field(format!(
165 "MCP server '{name}': generic connector requires an HTTPS resource"
166 )));
167 }
168 if connector.client_secret.is_some() && connector.client_id_secret.is_none() {
169 return Err(ConfigValidationError::invalid_field(format!(
170 "MCP server '{name}': connector client secret requires a client ID"
171 )));
172 }
173 }
174 if let Some(ext) = self.external_auth.as_ref() {
175 if ext.token_endpoint.starts_with("http://")
176 || ext.token_endpoint.starts_with("https://")
177 {
178 return Err(ConfigValidationError::invalid_field(format!(
179 "MCP server '{name}': external_auth.token_endpoint must be a relative \
180 path (e.g. /api/public/<provider>/token); the host is derived from \
181 server.api_external_url. Remove the scheme+host prefix."
182 )));
183 }
184 if !ext.token_endpoint.starts_with('/') {
185 return Err(ConfigValidationError::invalid_field(format!(
186 "MCP server '{name}': external_auth.token_endpoint must be an absolute \
187 path beginning with '/'."
188 )));
189 }
190 if ext.header.trim().is_empty() {
191 return Err(ConfigValidationError::invalid_field(format!(
192 "MCP server '{name}': external_auth.header must not be empty."
193 )));
194 }
195 }
196
197 Ok(())
198 }
199}
200
201#[derive(Debug, Clone, Serialize, Deserialize)]
202pub struct SchemaDefinition {
203 pub file: String,
204 pub table: String,
205 pub required_columns: Vec<String>,
206}
207
208#[derive(Debug, Clone, Serialize, Deserialize)]
209pub struct OAuthRequirement {
210 pub required: bool,
211 pub scopes: Vec<Permission>,
212 pub audience: JwtAudience,
213 pub client_id: Option<ClientId>,
214 #[serde(default)]
215 pub ema: bool,
216}
217
218#[derive(Debug, Clone, Serialize, Deserialize)]
219pub struct Settings {
220 pub auto_build: bool,
221 pub build_timeout: u64,
222 pub health_check_timeout: u64,
223 #[serde(default = "default_base_port")]
224 pub base_port: u16,
225 #[serde(default = "default_working_dir")]
226 pub working_dir: String,
227}
228
229const fn default_base_port() -> u16 {
230 5000
231}
232
233fn default_working_dir() -> String {
234 "/app".to_owned()
235}
236
237#[derive(Debug, Clone, Serialize, Deserialize)]
239#[serde(deny_unknown_fields)]
240pub struct ConnectorConfig {
241 #[serde(default = "generic_adapter")]
242 pub adapter: String,
243 #[serde(default)]
244 pub scopes: Vec<String>,
245 #[serde(default)]
246 pub authorization_origins: Vec<String>,
247 #[serde(default)]
248 pub client_id_secret: Option<String>,
249 #[serde(default)]
250 pub client_secret: Option<String>,
251}
252fn generic_adapter() -> String {
253 "generic".to_owned()
254}