Skip to main content

systemprompt_models/mcp/
server.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use std::path::{Path, PathBuf};
4
5use crate::ai::ToolModelConfig;
6use crate::auth::{AuthenticatedUser, Permission};
7use crate::mcp::deployment::McpServerType;
8
9pub const RUNNING: &str = "running";
10pub const ERROR: &str = "error";
11pub const STOPPED: &str = "stopped";
12pub const STARTING: &str = "starting";
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct McpServerConfig {
16    pub name: String,
17    pub server_type: McpServerType,
18    pub binary: String,
19    pub enabled: bool,
20    pub display_in_web: bool,
21    pub port: u16,
22    #[serde(
23        serialize_with = "serialize_path",
24        deserialize_with = "deserialize_path"
25    )]
26    pub crate_path: PathBuf,
27    pub display_name: String,
28    pub description: String,
29    pub capabilities: Vec<String>,
30    #[serde(default)]
31    pub schemas: Vec<super::deployment::SchemaDefinition>,
32    pub oauth: super::deployment::OAuthRequirement,
33    #[serde(default)]
34    pub tools: HashMap<String, super::deployment::ToolMetadata>,
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub model_config: Option<ToolModelConfig>,
37    #[serde(default)]
38    pub env_vars: Vec<String>,
39    pub version: String,
40    pub host: String,
41    pub module_name: String,
42    pub protocol: String,
43    #[serde(default)]
44    pub remote_endpoint: String,
45}
46
47fn serialize_path<S>(path: &Path, serializer: S) -> Result<S::Ok, S::Error>
48where
49    S: serde::Serializer,
50{
51    path.to_string_lossy().serialize(serializer)
52}
53
54fn deserialize_path<'de, D>(deserializer: D) -> Result<PathBuf, D::Error>
55where
56    D: serde::Deserializer<'de>,
57{
58    let s = String::deserialize(deserializer)?;
59    Ok(PathBuf::from(s))
60}
61
62impl McpServerConfig {
63    pub fn endpoint(&self, api_server_url: &str) -> String {
64        format!("{}/api/v1/mcp/{}/mcp", api_server_url, self.name)
65    }
66
67    pub const fn is_internal(&self) -> bool {
68        matches!(self.server_type, McpServerType::Internal)
69    }
70
71    pub const fn is_external(&self) -> bool {
72        matches!(self.server_type, McpServerType::External)
73    }
74}
75
76#[derive(Debug, Clone, Serialize, Deserialize)]
77pub enum McpAuthState {
78    Authenticated(AuthenticatedUser),
79    Anonymous,
80}
81
82impl McpAuthState {
83    pub const fn is_authenticated(&self) -> bool {
84        matches!(self, Self::Authenticated(_))
85    }
86
87    pub const fn is_anonymous(&self) -> bool {
88        matches!(self, Self::Anonymous)
89    }
90
91    pub const fn user(&self) -> Option<&AuthenticatedUser> {
92        match self {
93            Self::Authenticated(user) => Some(user),
94            Self::Anonymous => None,
95        }
96    }
97
98    pub fn has_permission(&self, permission: Permission) -> bool {
99        match self {
100            Self::Authenticated(user) => user.has_permission(permission),
101            Self::Anonymous => permission == Permission::Anonymous,
102        }
103    }
104
105    pub fn username(&self) -> String {
106        match self {
107            Self::Authenticated(user) => user.username.clone(),
108            Self::Anonymous => "anonymous".to_string(),
109        }
110    }
111}