context_server_settings.rs

 1use std::sync::Arc;
 2
 3use collections::HashMap;
 4use gpui::App;
 5use schemars::JsonSchema;
 6use schemars::r#gen::SchemaGenerator;
 7use schemars::schema::{InstanceType, Schema, SchemaObject};
 8use serde::{Deserialize, Serialize};
 9use settings::{Settings, SettingsSources};
10
11pub fn init(cx: &mut App) {
12    ContextServerSettings::register(cx);
13}
14
15#[derive(Deserialize, Serialize, Clone, PartialEq, Eq, JsonSchema, Debug, Default)]
16pub struct ServerConfig {
17    /// The command to run this context server.
18    ///
19    /// This will override the command set by an extension.
20    pub command: Option<ServerCommand>,
21    /// The settings for this context server.
22    ///
23    /// Consult the documentation for the context server to see what settings
24    /// are supported.
25    #[schemars(schema_with = "server_config_settings_json_schema")]
26    pub settings: Option<serde_json::Value>,
27}
28
29fn server_config_settings_json_schema(_generator: &mut SchemaGenerator) -> Schema {
30    Schema::Object(SchemaObject {
31        instance_type: Some(InstanceType::Object.into()),
32        ..Default::default()
33    })
34}
35
36#[derive(Deserialize, Serialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
37pub struct ServerCommand {
38    pub path: String,
39    pub args: Vec<String>,
40    pub env: Option<HashMap<String, String>>,
41}
42
43#[derive(Deserialize, Serialize, Default, Clone, PartialEq, Eq, JsonSchema, Debug)]
44pub struct ContextServerSettings {
45    /// Settings for context servers used in the Assistant.
46    #[serde(default)]
47    pub context_servers: HashMap<Arc<str>, ServerConfig>,
48}
49
50impl Settings for ContextServerSettings {
51    const KEY: Option<&'static str> = None;
52
53    type FileContent = Self;
54
55    fn load(
56        sources: SettingsSources<Self::FileContent>,
57        _: &mut gpui::App,
58    ) -> anyhow::Result<Self> {
59        sources.json_merge()
60    }
61
62    fn import_from_vscode(vscode: &settings::VsCodeSettings, current: &mut Self::FileContent) {
63        // we don't handle "inputs" replacement strings, see perplexity-key in this example:
64        // https://code.visualstudio.com/docs/copilot/chat/mcp-servers#_configuration-example
65        #[derive(Deserialize)]
66        struct VsCodeServerCommand {
67            command: String,
68            args: Option<Vec<String>>,
69            env: Option<HashMap<String, String>>,
70            // note: we don't support envFile and type
71        }
72        impl From<VsCodeServerCommand> for ServerCommand {
73            fn from(cmd: VsCodeServerCommand) -> Self {
74                Self {
75                    path: cmd.command,
76                    args: cmd.args.unwrap_or_default(),
77                    env: cmd.env,
78                }
79            }
80        }
81        if let Some(mcp) = vscode.read_value("mcp").and_then(|v| v.as_object()) {
82            current
83                .context_servers
84                .extend(mcp.iter().filter_map(|(k, v)| {
85                    Some((
86                        k.clone().into(),
87                        ServerConfig {
88                            command: Some(
89                                serde_json::from_value::<VsCodeServerCommand>(v.clone())
90                                    .ok()?
91                                    .into(),
92                            ),
93                            settings: None,
94                        },
95                    ))
96                }));
97        }
98    }
99}