agent_ui: Fix `context_server` duplication when name is updated (#35403)

Umesh Yadav and Ben Brandt created

Closes #35400

| Before | After |
|--------|--------|
| <video
src="https://github.com/user-attachments/assets/6aae34ca-e022-457a-9b66-47b85c976b23"/>
| <video
src="https://github.com/user-attachments/assets/ae058988-8f70-4605-b537-e045175d2e75"
/> |

Release Notes:

- agent: Fix `context_server` duplication when name is updated

---------

Signed-off-by: Umesh Yadav <git@umesh.dev>
Co-authored-by: Ben Brandt <benjamin.j.brandt@gmail.com>

Change summary

crates/agent_ui/src/agent_configuration/configure_context_server_modal.rs | 22 
1 file changed, 19 insertions(+), 3 deletions(-)

Detailed changes

crates/agent_ui/src/agent_configuration/configure_context_server_modal.rs 🔗

@@ -251,6 +251,7 @@ pub struct ConfigureContextServerModal {
     workspace: WeakEntity<Workspace>,
     source: ConfigurationSource,
     state: State,
+    original_server_id: Option<ContextServerId>,
 }
 
 impl ConfigureContextServerModal {
@@ -348,6 +349,11 @@ impl ConfigureContextServerModal {
                     context_server_store,
                     workspace: workspace_handle,
                     state: State::Idle,
+                    original_server_id: match &target {
+                        ConfigurationTarget::Existing { id, .. } => Some(id.clone()),
+                        ConfigurationTarget::Extension { id, .. } => Some(id.clone()),
+                        ConfigurationTarget::New => None,
+                    },
                     source: ConfigurationSource::from_target(
                         target,
                         language_registry,
@@ -415,9 +421,19 @@ impl ConfigureContextServerModal {
             // When we write the settings to the file, the context server will be restarted.
             workspace.update(cx, |workspace, cx| {
                 let fs = workspace.app_state().fs.clone();
-                update_settings_file::<ProjectSettings>(fs.clone(), cx, |project_settings, _| {
-                    project_settings.context_servers.insert(id.0, settings);
-                });
+                let original_server_id = self.original_server_id.clone();
+                update_settings_file::<ProjectSettings>(
+                    fs.clone(),
+                    cx,
+                    move |project_settings, _| {
+                        if let Some(original_id) = original_server_id {
+                            if original_id != id {
+                                project_settings.context_servers.remove(&original_id.0);
+                            }
+                        }
+                        project_settings.context_servers.insert(id.0, settings);
+                    },
+                );
             });
         } else if let Some(existing_server) = existing_server {
             self.context_server_store