Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
settings_ui: Fix MCP server toggle not updating in UI (#60552)
counterfactual5604221dbc758
diff --git a/crates/project/src/context_server_store.rs b/crates/project/src/context_server_store.rsindex 3fb29cdb1c9..9e3a65134dc 100644--- a/crates/project/src/context_server_store.rs+++ b/crates/project/src/context_server_store.rs@@ -570,6 +570,21 @@ impl ContextServerStore {}}+ /// Returns whether a server is enabled.+ /// Servers with no settings entry only originate from an extension+ /// descriptor in the registry, and those are enabled by default+ /// ([`ContextServerSettings::default_extension`]).+ pub fn is_server_enabled(&self, id: &ContextServerId, cx: &App) -> bool {+ match self.settings_for_server(id) {+ Some(settings) => settings.enabled(),+ None => self+ .registry+ .read(cx)+ .context_server_descriptor(&id.0)+ .is_some(),+ }+ }+/// Returns a sorted slice of available unique context server IDs. Within the/// slice, context servers which have `mcp-server-` as a prefix in their ID will/// appear after servers that do not have this prefix in their ID.@@ -859,6 +874,7 @@ impl ContextServerStore {server_id: id.clone(),status: ContextServerStatus::Stopped,});+ cx.notify();Ok(())}@@ -1667,6 +1683,7 @@ impl ContextServerStore {server_id: id,status,});+ cx.notify();}fn available_context_servers_changed(&mut self, cx: &mut Context<Self>) {diff --git a/crates/project/tests/integration/context_server_store.rs b/crates/project/tests/integration/context_server_store.rsindex 2600708df09..984bb334edb 100644--- a/crates/project/tests/integration/context_server_store.rs+++ b/crates/project/tests/integration/context_server_store.rs@@ -1658,6 +1658,92 @@ async fn test_multi_worktree_duplicate_server_first_wins(cx: &mut TestAppContext});}+#[gpui::test]+async fn test_is_server_enabled(cx: &mut TestAppContext) {+ // We'll be setting up 4 different servers in order to test the following+ // scenarios:+ //+ // 1. Explicit Settings, Enabled+ // 2. Explicit Settings, Disabled+ // 3. No Settings, Registry Descriptor, Enabled+ // 4. No Settings, No Descriptor, Disabled+ const SERVER_1_ID: &str = "mcp-1";+ const SERVER_2_ID: &str = "mcp-2";+ const SERVER_3_ID: &str = "mcp-3";+ const SERVER_4_ID: &str = "mcp-4";++ let (_fs, project) = setup_context_server_test(+ cx,+ json!({"code.rs": ""}),+ vec![+ (+ SERVER_1_ID.into(),+ ContextServerSettings::Extension {+ enabled: true,+ remote: false,+ settings: json!({}),+ },+ ),+ (+ SERVER_2_ID.into(),+ ContextServerSettings::Extension {+ enabled: false,+ remote: false,+ settings: json!({}),+ },+ ),+ ],+ )+ .await;++ let registry = cx.new(|cx| {+ let mut registry = ContextServerDescriptorRegistry::new();+ let descriptor = Arc::new(FakeContextServerDescriptor::new(SERVER_3_ID));+ registry.register_context_server_descriptor(SERVER_3_ID.into(), descriptor, cx);++ registry+ });++ let store = cx.new(|cx| {+ ContextServerStore::test(+ registry.clone(),+ project.read(cx).worktree_store(),+ Some(project.downgrade()),+ cx,+ )+ });++ // Sanity check before proceeding, confirm server 1 and 2 have settings,+ // server 3 is present in the registry while server 4 does not meet any of+ // the conditions.+ cx.update(|cx| {+ let settings = ProjectSettings::get_global(cx);++ assert!(settings.context_servers.contains_key(SERVER_1_ID));+ assert!(settings.context_servers.contains_key(SERVER_2_ID));+ assert!(!settings.context_servers.contains_key(SERVER_3_ID));+ assert!(!settings.context_servers.contains_key(SERVER_4_ID));+ });++ registry.update(cx, |registry, _cx| {+ let descriptors = registry.context_server_descriptors();+ assert_eq!(descriptors.len(), 1);+ assert_eq!(descriptors[0].0.as_ref(), SERVER_3_ID);+ });++ let server_1_id = ContextServerId(SERVER_1_ID.into());+ let server_2_id = ContextServerId(SERVER_2_ID.into());+ let server_3_id = ContextServerId(SERVER_3_ID.into());+ let server_4_id = ContextServerId(SERVER_4_ID.into());++ store.read_with(cx, |store, cx| {+ assert!(store.is_server_enabled(&server_1_id, cx));+ assert!(!store.is_server_enabled(&server_2_id, cx));+ assert!(store.is_server_enabled(&server_3_id, cx));+ assert!(!store.is_server_enabled(&server_4_id, cx));+ })+}+fn assert_server_events(store: &Entity<ContextServerStore>,expected_events: Vec<(ContextServerId, ContextServerStatus)>,diff --git a/crates/settings_ui/src/pages/mcp_servers_page.rs b/crates/settings_ui/src/pages/mcp_servers_page.rsindex f06e6b33bee..2e5f9628965 100644--- a/crates/settings_ui/src/pages/mcp_servers_page.rs+++ b/crates/settings_ui/src/pages/mcp_servers_page.rs@@ -163,6 +163,11 @@ fn render_context_server(let server_configuration = store.read(cx).configuration_for_server(context_server_id);let is_running = matches!(server_status, ContextServerStatus::Running);+ let is_enabled = store.read(cx).is_server_enabled(context_server_id, cx);+ let is_transitioning = matches!(+ server_status,+ ContextServerStatus::Starting | ContextServerStatus::Authenticating+ );let item_id = SharedString::from(context_server_id.0.to_string());// Determine the source from the configured settings rather than the runtime@@ -170,7 +175,6 @@ fn render_context_server(// started has no runtime configuration, and must not be mistaken for an// extension-provided server.let provided_by_extension = store.read(cx).is_extension_provided(context_server_id, cx);-let display_name = if provided_by_extension {resolve_extension_display_name(context_server_id, cx).unwrap_or_else(|| item_id.clone())} else {@@ -218,7 +222,8 @@ fn render_context_server(let uninstall_button = render_uninstall_button(context_server_id, provided_by_extension);// Build toggle switch- let toggle_switch = render_toggle_switch(context_server_id, store, is_running);+ let toggle_switch =+ render_toggle_switch(context_server_id, store, is_enabled, is_transitioning);// Surface invalid settings (which prevent the server from starting at all)// ahead of runtime status feedback, so the misconfiguration is visible.@@ -317,19 +322,21 @@ fn render_uninstall_button(fn render_toggle_switch(context_server_id: &ContextServerId,store: &Entity<ContextServerStore>,- is_running: bool,+ is_enabled: bool,+ is_transitioning: bool,) -> impl IntoElement {let context_server_id = context_server_id.clone();let store = store.clone();Switch::new(SharedString::from(format!("mcp-toggle-{}", context_server_id.0)),- if is_running {+ if is_enabled {ToggleState::Selected} else {ToggleState::Unselected},)+ .disabled(is_transitioning).tab_index(0isize).on_click({move |state, _window, cx| {