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 = "HashMap::is_empty")]
95 pub headers: HashMap<String, String>,
96}
97
98#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct ExternalAuth {
108 pub token_endpoint: String,
109 #[serde(default = "default_auth_header")]
110 pub header: String,
111 #[serde(default = "default_auth_scheme")]
112 pub scheme: String,
113}
114
115fn default_auth_header() -> String {
116 "Authorization".to_owned()
117}
118
119fn default_auth_scheme() -> String {
120 "Bearer".to_owned()
121}
122
123impl ExternalAuth {
124 pub fn header_value(&self, bearer: &str) -> String {
125 if self.scheme.trim().is_empty() {
126 bearer.to_owned()
127 } else {
128 format!("{} {bearer}", self.scheme)
129 }
130 }
131}
132
133impl Deployment {
134 pub fn validate(&self, name: &str) -> Result<(), ConfigValidationError> {
135 if matches!(self.server_type, McpServerType::Internal) {
136 if let Some(ep) = self.endpoint.as_deref()
137 && (ep.starts_with("http://") || ep.starts_with("https://"))
138 {
139 return Err(ConfigValidationError::invalid_field(format!(
140 "MCP server '{name}': endpoint must be a relative path (e.g. \
141 /api/v1/mcp/{name}/mcp) or omitted; the host is derived from \
142 server.api_external_url. Remove the scheme+host prefix."
143 )));
144 }
145 if self.external_auth.is_some() || !self.headers.is_empty() {
146 return Err(ConfigValidationError::invalid_field(format!(
147 "MCP server '{name}': external_auth and headers are only valid on \
148 external servers; internal servers are reached through the gateway \
149 with the systemprompt credential."
150 )));
151 }
152 }
153
154 if let Some(ext) = self.external_auth.as_ref() {
155 if ext.token_endpoint.starts_with("http://")
156 || ext.token_endpoint.starts_with("https://")
157 {
158 return Err(ConfigValidationError::invalid_field(format!(
159 "MCP server '{name}': external_auth.token_endpoint must be a relative \
160 path (e.g. /api/public/<provider>/token); the host is derived from \
161 server.api_external_url. Remove the scheme+host prefix."
162 )));
163 }
164 if !ext.token_endpoint.starts_with('/') {
165 return Err(ConfigValidationError::invalid_field(format!(
166 "MCP server '{name}': external_auth.token_endpoint must be an absolute \
167 path beginning with '/'."
168 )));
169 }
170 if ext.header.trim().is_empty() {
171 return Err(ConfigValidationError::invalid_field(format!(
172 "MCP server '{name}': external_auth.header must not be empty."
173 )));
174 }
175 }
176
177 Ok(())
178 }
179}
180
181#[derive(Debug, Clone, Serialize, Deserialize)]
182pub struct SchemaDefinition {
183 pub file: String,
184 pub table: String,
185 pub required_columns: Vec<String>,
186}
187
188#[derive(Debug, Clone, Serialize, Deserialize)]
189pub struct OAuthRequirement {
190 pub required: bool,
191 pub scopes: Vec<Permission>,
192 pub audience: JwtAudience,
193 pub client_id: Option<ClientId>,
194 #[serde(default)]
195 pub ema: bool,
196}
197
198#[derive(Debug, Clone, Serialize, Deserialize)]
199pub struct Settings {
200 pub auto_build: bool,
201 pub build_timeout: u64,
202 pub health_check_timeout: u64,
203 #[serde(default = "default_base_port")]
204 pub base_port: u16,
205 #[serde(default = "default_working_dir")]
206 pub working_dir: String,
207}
208
209const fn default_base_port() -> u16 {
210 5000
211}
212
213fn default_working_dir() -> String {
214 "/app".to_owned()
215}