Skip to content

Commit caf89ee

Browse files
committed
Refactor TransportManager to use switch expressions and improve error handling
- Replace if-else chains with switch expressions for better readability and exhaustiveness checking - Add GetClient() helper method to centralize client retrieval logic - Wrap StopAsync in try-catch to log failures when stopping a failed transport - Use client.TransportName instead of mode.ToString() for consistent naming in error messages
1 parent df14d84 commit caf89ee

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

MCPForUnity/Editor/Services/Transport/TransportManager.cs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,32 @@ private IMcpTransportClient GetOrCreateClient(TransportMode mode)
4545
};
4646
}
4747

48+
private IMcpTransportClient GetClient(TransportMode mode)
49+
{
50+
return mode switch
51+
{
52+
TransportMode.Http => _httpClient,
53+
TransportMode.Stdio => _stdioClient,
54+
_ => throw new ArgumentOutOfRangeException(nameof(mode), mode, "Unsupported transport mode"),
55+
};
56+
}
57+
4858
public async Task<bool> StartAsync(TransportMode mode)
4959
{
5060
IMcpTransportClient client = GetOrCreateClient(mode);
5161

5262
bool started = await client.StartAsync();
5363
if (!started)
5464
{
55-
await client.StopAsync();
56-
UpdateState(mode, TransportState.Disconnected(mode.ToString().ToLowerInvariant(), "Failed to start"));
65+
try
66+
{
67+
await client.StopAsync();
68+
}
69+
catch (Exception ex)
70+
{
71+
McpLog.Warn($"Error while stopping transport {client.TransportName}: {ex.Message}");
72+
}
73+
UpdateState(mode, TransportState.Disconnected(client.TransportName, "Failed to start"));
5774
return false;
5875
}
5976

@@ -90,7 +107,7 @@ async Task StopClient(IMcpTransportClient client, TransportMode clientMode)
90107

91108
public async Task<bool> VerifyAsync(TransportMode mode)
92109
{
93-
IMcpTransportClient client = mode == TransportMode.Http ? _httpClient : _stdioClient;
110+
IMcpTransportClient client = GetClient(mode);
94111
if (client == null)
95112
{
96113
return false;
@@ -104,20 +121,28 @@ public async Task<bool> VerifyAsync(TransportMode mode)
104121

105122
public TransportState GetState(TransportMode mode)
106123
{
107-
return mode == TransportMode.Http ? _httpState : _stdioState;
124+
return mode switch
125+
{
126+
TransportMode.Http => _httpState,
127+
TransportMode.Stdio => _stdioState,
128+
_ => throw new ArgumentOutOfRangeException(nameof(mode), mode, "Unsupported transport mode"),
129+
};
108130
}
109131

110132
public bool IsRunning(TransportMode mode) => GetState(mode).IsConnected;
111133

112134
private void UpdateState(TransportMode mode, TransportState state)
113135
{
114-
if (mode == TransportMode.Http)
115-
{
116-
_httpState = state;
117-
}
118-
else
136+
switch (mode)
119137
{
120-
_stdioState = state;
138+
case TransportMode.Http:
139+
_httpState = state;
140+
break;
141+
case TransportMode.Stdio:
142+
_stdioState = state;
143+
break;
144+
default:
145+
throw new ArgumentOutOfRangeException(nameof(mode), mode, "Unsupported transport mode");
121146
}
122147
}
123148
}

0 commit comments

Comments
 (0)