Skip to content

Commit e2267ec

Browse files
committed
Replace McpTransportException with InvalidOperationException
1 parent 4998129 commit e2267ec

8 files changed

+24
-91
lines changed

src/ModelContextProtocol/Protocol/Transport/IClientTransport.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ public interface IClientTransport
4242
/// This method is used by <see cref="McpClientFactory"/> to initialize the connection.
4343
/// </para>
4444
/// </remarks>
45-
/// <exception cref="McpTransportException">The transport connection could not be established.</exception>
45+
/// <exception cref="InvalidOperationException">The transport connection could not be established.</exception>
4646
Task<ITransport> ConnectAsync(CancellationToken cancellationToken = default);
4747
}

src/ModelContextProtocol/Protocol/Transport/McpTransportException.cs

-34
This file was deleted.

src/ModelContextProtocol/Protocol/Transport/SseClientSessionTransport.cs

+13-29
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
5858

5959
await _connectionEstablished.Task.WaitAsync(_options.ConnectionTimeout, cancellationToken).ConfigureAwait(false);
6060
}
61-
catch (Exception ex) when (ex is not McpTransportException) // propagate transport exceptions
61+
catch (Exception ex)
6262
{
6363
LogTransportConnectFailed(Name, ex);
6464
await CloseAsync().ConfigureAwait(false);
65-
throw new McpTransportException("Failed to connect transport", ex);
65+
throw new InvalidOperationException("Failed to connect transport", ex);
6666
}
6767
}
6868

@@ -110,7 +110,7 @@ public override async Task SendMessageAsync(
110110
else
111111
{
112112
JsonRpcResponse initializeResponse = JsonSerializer.Deserialize(responseContent, McpJsonUtilities.JsonContext.Default.JsonRpcResponse) ??
113-
throw new McpTransportException("Failed to initialize client");
113+
throw new InvalidOperationException("Failed to initialize client");
114114

115115
LogTransportReceivedMessage(Name, messageId);
116116
await WriteMessageAsync(initializeResponse, cancellationToken).ConfigureAwait(false);
@@ -136,7 +136,7 @@ public override async Task SendMessageAsync(
136136
LogRejectedPost(Name, messageId);
137137
}
138138

139-
throw new McpTransportException("Failed to send message");
139+
throw new InvalidOperationException("Failed to send message");
140140
}
141141
}
142142

@@ -273,34 +273,18 @@ private async Task ProcessSseMessage(string data, CancellationToken cancellation
273273

274274
private void HandleEndpointEvent(string data)
275275
{
276-
try
276+
if (string.IsNullOrEmpty(data))
277277
{
278-
if (string.IsNullOrEmpty(data))
279-
{
280-
LogTransportEndpointEventInvalid(Name);
281-
return;
282-
}
283-
284-
// If data is an absolute URL, the Uri will be constructed entirely from it and not the _sseEndpoint.
285-
_messageEndpoint = new Uri(_sseEndpoint, data);
286-
287-
// Set connected state
288-
SetConnected(true);
289-
_connectionEstablished.TrySetResult(true);
278+
LogTransportEndpointEventInvalid(Name);
279+
return;
290280
}
291-
catch (JsonException ex)
292-
{
293-
if (_logger.IsEnabled(LogLevel.Trace))
294-
{
295-
LogTransportEndpointEventParseFailedSensitive(Name, data, ex);
296-
}
297-
else
298-
{
299-
LogTransportEndpointEventParseFailed(Name, ex);
300-
}
301281

302-
throw new McpTransportException("Failed to parse endpoint event", ex);
303-
}
282+
// If data is an absolute URL, the Uri will be constructed entirely from it and not the _sseEndpoint.
283+
_messageEndpoint = new Uri(_sseEndpoint, data);
284+
285+
// Set connected state
286+
SetConnected(true);
287+
_connectionEstablished.TrySetResult(true);
304288
}
305289

306290
private void CopyAdditionalHeaders(HttpRequestHeaders headers)

src/ModelContextProtocol/Protocol/Transport/StdioClientSessionTransport.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ public StdioClientSessionTransport(StdioClientTransportOptions options, Process
2323
/// <para>
2424
/// For stdio-based transports, this implementation first verifies that the underlying process
2525
/// is still running before attempting to send the message. If the process has exited or cannot
26-
/// be accessed, a <see cref="McpTransportException"/> is thrown with details about the failure.
26+
/// be accessed, a <see cref="InvalidOperationException"/> is thrown with details about the failure.
2727
/// </para>
2828
/// <para>
2929
/// After verifying the process state, this method delegates to the base class implementation
3030
/// to handle the actual message serialization and transmission to the process's standard input stream.
3131
/// </para>
3232
/// </remarks>
33-
/// <exception cref="McpTransportException">
33+
/// <exception cref="InvalidOperationException">
3434
/// Thrown when the underlying process has exited or cannot be accessed.
3535
/// </exception>
3636
public override async Task SendMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken = default)
@@ -49,7 +49,7 @@ public override async Task SendMessageAsync(IJsonRpcMessage message, Cancellatio
4949

5050
if (hasExited)
5151
{
52-
throw new McpTransportException("Transport is not connected", processException);
52+
throw new InvalidOperationException("Transport is not connected", processException);
5353
}
5454

5555
await base.SendMessageAsync(message, cancellationToken).ConfigureAwait(false);

src/ModelContextProtocol/Protocol/Transport/StdioClientTransport.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public async Task<ITransport> ConnectAsync(CancellationToken cancellationToken =
154154
if (!processStarted)
155155
{
156156
LogTransportProcessStartFailed(logger, endpointName);
157-
throw new McpTransportException("Failed to start MCP server process");
157+
throw new InvalidOperationException("Failed to start MCP server process");
158158
}
159159

160160
LogTransportProcessStarted(logger, endpointName, process.Id);
@@ -176,7 +176,7 @@ public async Task<ITransport> ConnectAsync(CancellationToken cancellationToken =
176176
LogTransportShutdownFailed(logger, endpointName, ex2);
177177
}
178178

179-
throw new McpTransportException("Failed to connect transport", ex);
179+
throw new InvalidOperationException("Failed to connect transport", ex);
180180
}
181181
}
182182

src/ModelContextProtocol/Protocol/Transport/StreamClientSessionTransport.cs

+2-19
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,11 @@ public StreamClientSessionTransport(
5757
}
5858

5959
/// <inheritdoc/>
60-
/// <remarks>
61-
/// <para>
62-
/// For stream-based transports, this implementation serializes the JSON-RPC message to the
63-
/// underlying output stream. The specific serialization format includes:
64-
/// <list type="bullet">
65-
/// <item>A Content-Length header that specifies the byte length of the JSON message</item>
66-
/// <item>A blank line separator</item>
67-
/// <item>The UTF-8 encoded JSON representation of the message</item>
68-
/// </list>
69-
/// </para>
70-
/// <para>
71-
/// This implementation first checks if the transport is connected and throws a <see cref="McpTransportException"/>
72-
/// if it's not. It then extracts the message ID (if present) for logging purposes, serializes the message,
73-
/// and writes it to the output stream.
74-
/// </para>
75-
/// </remarks>
76-
/// <exception cref="McpTransportException">Thrown when the transport is not connected.</exception>
7760
public override async Task SendMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken = default)
7861
{
7962
if (!IsConnected)
8063
{
81-
throw new McpTransportException("Transport is not connected");
64+
throw new InvalidOperationException("Transport is not connected");
8265
}
8366

8467
string id = "(no id)";
@@ -99,7 +82,7 @@ public override async Task SendMessageAsync(IJsonRpcMessage message, Cancellatio
9982
catch (Exception ex)
10083
{
10184
LogTransportSendFailed(Name, id, ex);
102-
throw new McpTransportException("Failed to send message", ex);
85+
throw new InvalidOperationException("Failed to send message", ex);
10386
}
10487
}
10588

src/ModelContextProtocol/Protocol/Transport/StreamServerTransport.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public override async Task SendMessageAsync(IJsonRpcMessage message, Cancellatio
6060
{
6161
if (!IsConnected)
6262
{
63-
throw new McpTransportException("Transport is not connected");
63+
throw new InvalidOperationException("Transport is not connected");
6464
}
6565

6666
using var _ = await _sendLock.LockAsync(cancellationToken).ConfigureAwait(false);
@@ -80,7 +80,7 @@ public override async Task SendMessageAsync(IJsonRpcMessage message, Cancellatio
8080
catch (Exception ex)
8181
{
8282
LogTransportSendFailed(Name, id, ex);
83-
throw new McpTransportException("Failed to send message", ex);
83+
throw new InvalidOperationException("Failed to send message", ex);
8484
}
8585
}
8686

src/ModelContextProtocol/Protocol/Transport/TransportBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected async Task WriteMessageAsync(IJsonRpcMessage message, CancellationToke
7373
{
7474
if (!IsConnected)
7575
{
76-
throw new McpTransportException("Transport is not connected");
76+
throw new InvalidOperationException("Transport is not connected");
7777
}
7878

7979
await _messageChannel.Writer.WriteAsync(message, cancellationToken).ConfigureAwait(false);

0 commit comments

Comments
 (0)