diff --git a/src/Desktop/RodelAgent.UI/ViewModels/Items/McpServerItemViewModel.cs b/src/Desktop/RodelAgent.UI/ViewModels/Items/McpServerItemViewModel.cs index cb754601..966f36ac 100644 --- a/src/Desktop/RodelAgent.UI/ViewModels/Items/McpServerItemViewModel.cs +++ b/src/Desktop/RodelAgent.UI/ViewModels/Items/McpServerItemViewModel.cs @@ -82,6 +82,7 @@ private async Task TryConnectAsync() { await client.DisposeAsync(); McpServers.Remove(Id); + client = null; } _runCts = new CancellationTokenSource(); @@ -109,6 +110,18 @@ private async Task TryConnectAsync() } catch (Exception ex) { + if (client != null) + { + try + { + await client.DisposeAsync(); + } + catch (Exception disposeException) + { + this.Get>().LogWarning(disposeException, "Failed to dispose MCP client after connection failure."); + } + } + State = McpServerState.Error; FunctionCount = 0; Functions.Clear(); @@ -176,4 +189,4 @@ public sealed class McpAgentConfig : McpServerDefinition public bool? IsEnabled { get; set; } } -public sealed partial class McpAgentConfigCollection : Dictionary; \ No newline at end of file +public sealed partial class McpAgentConfigCollection : Dictionary;