Skip to content

Commit 8fcdf95

Browse files
authored
Consolidate McpClientException/McpServerException into McpException (#209)
1 parent d1a3c20 commit 8fcdf95

25 files changed

+163
-197
lines changed

src/ModelContextProtocol/Client/IMcpClient.cs

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ namespace ModelContextProtocol.Client;
88
public interface IMcpClient : IMcpEndpoint
99
{
1010
/// <summary>
11-
/// Gets the capabilities supported by the server.
11+
/// Gets the capabilities supported by the connected server.
1212
/// </summary>
13-
ServerCapabilities? ServerCapabilities { get; }
13+
ServerCapabilities ServerCapabilities { get; }
1414

1515
/// <summary>
16-
/// Gets the version and implementation information of the server.
16+
/// Gets the implementation information of the connected server.
1717
/// </summary>
18-
Implementation? ServerInfo { get; }
18+
Implementation ServerInfo { get; }
1919

2020
/// <summary>
21-
/// Instructions describing how to use the server and its features.
22-
/// This can be used by clients to improve the LLM's understanding of available tools, resources, etc.
23-
/// It can be thought of like a "hint" to the model. For example, this information MAY be added to the system prompt.
21+
/// Gets any instructions describing how to use the connected server and its features.
2422
/// </summary>
23+
/// <remarks>
24+
/// This can be used by clients to improve an LLM's understanding of available tools, prompts, and resources.
25+
/// It can be thought of like a "hint" to the model and may be added to a system prompt.
26+
/// </remarks>
2527
string? ServerInstructions { get; }
2628
}

src/ModelContextProtocol/Client/McpClient.cs

+13-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ internal sealed class McpClient : McpEndpoint, IMcpClient
1818
private ITransport? _sessionTransport;
1919
private CancellationTokenSource? _connectCts;
2020

21+
private ServerCapabilities? _serverCapabilities;
22+
private Implementation? _serverInfo;
23+
private string? _serverInstructions;
24+
2125
/// <summary>
2226
/// Initializes a new instance of the <see cref="McpClient"/> class.
2327
/// </summary>
@@ -66,13 +70,13 @@ public McpClient(IClientTransport clientTransport, McpClientOptions options, Mcp
6670
}
6771

6872
/// <inheritdoc/>
69-
public ServerCapabilities? ServerCapabilities { get; private set; }
73+
public ServerCapabilities ServerCapabilities => _serverCapabilities ?? throw new InvalidOperationException("The client is not connected.");
7074

7175
/// <inheritdoc/>
72-
public Implementation? ServerInfo { get; private set; }
76+
public Implementation ServerInfo => _serverInfo ?? throw new InvalidOperationException("The client is not connected.");
7377

7478
/// <inheritdoc/>
75-
public string? ServerInstructions { get; private set; }
79+
public string? ServerInstructions => _serverInstructions;
7680

7781
/// <inheritdoc/>
7882
public override string EndpointName { get; }
@@ -112,26 +116,26 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
112116
capabilities: JsonSerializer.Serialize(initializeResponse.Capabilities, McpJsonUtilities.JsonContext.Default.ServerCapabilities),
113117
serverInfo: JsonSerializer.Serialize(initializeResponse.ServerInfo, McpJsonUtilities.JsonContext.Default.Implementation));
114118

115-
ServerCapabilities = initializeResponse.Capabilities;
116-
ServerInfo = initializeResponse.ServerInfo;
117-
ServerInstructions = initializeResponse.Instructions;
119+
_serverCapabilities = initializeResponse.Capabilities;
120+
_serverInfo = initializeResponse.ServerInfo;
121+
_serverInstructions = initializeResponse.Instructions;
118122

119123
// Validate protocol version
120124
if (initializeResponse.ProtocolVersion != _options.ProtocolVersion)
121125
{
122126
_logger.ServerProtocolVersionMismatch(EndpointName, _options.ProtocolVersion, initializeResponse.ProtocolVersion);
123-
throw new McpClientException($"Server protocol version mismatch. Expected {_options.ProtocolVersion}, got {initializeResponse.ProtocolVersion}");
127+
throw new McpException($"Server protocol version mismatch. Expected {_options.ProtocolVersion}, got {initializeResponse.ProtocolVersion}");
124128
}
125129

126130
// Send initialized notification
127131
await SendMessageAsync(
128132
new JsonRpcNotification { Method = NotificationMethods.InitializedNotification },
129133
initializationCts.Token).ConfigureAwait(false);
130134
}
131-
catch (OperationCanceledException) when (initializationCts.IsCancellationRequested)
135+
catch (OperationCanceledException oce) when (initializationCts.IsCancellationRequested)
132136
{
133137
_logger.ClientInitializationTimeout(EndpointName);
134-
throw new McpClientException("Initialization timed out");
138+
throw new McpException("Initialization timed out", oce);
135139
}
136140
}
137141
catch (Exception e)

src/ModelContextProtocol/Client/McpClientException.cs

-46
This file was deleted.

src/ModelContextProtocol/Client/McpClientExtensions.cs

+16-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static class McpClientExtensions
1515
/// Sends a ping request to verify server connectivity.
1616
/// </summary>
1717
/// <param name="client">The client.</param>
18-
/// <param name="cancellationToken">A token to cancel the operation.</param>
18+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
1919
/// <returns>A task that completes when the ping is successful.</returns>
2020
public static Task PingAsync(this IMcpClient client, CancellationToken cancellationToken = default)
2121
{
@@ -34,7 +34,7 @@ public static Task PingAsync(this IMcpClient client, CancellationToken cancellat
3434
/// </summary>
3535
/// <param name="client">The client.</param>
3636
/// <param name="serializerOptions">The serializer options governing tool parameter serialization.</param>
37-
/// <param name="cancellationToken">A token to cancel the operation.</param>
37+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
3838
/// <returns>A list of all available tools.</returns>
3939
public static async Task<IList<McpClientTool>> ListToolsAsync(
4040
this IMcpClient client,
@@ -75,7 +75,7 @@ public static async Task<IList<McpClientTool>> ListToolsAsync(
7575
/// </summary>
7676
/// <param name="client">The client.</param>
7777
/// <param name="serializerOptions">The serializer options governing tool parameter serialization.</param>
78-
/// <param name="cancellationToken">A token to cancel the operation.</param>
78+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
7979
/// <returns>An asynchronous sequence of all available tools.</returns>
8080
/// <remarks>
8181
/// Every iteration through the returned <see cref="IAsyncEnumerable{McpClientTool}"/>
@@ -115,7 +115,7 @@ public static async IAsyncEnumerable<McpClientTool> EnumerateToolsAsync(
115115
/// Retrieves a list of available prompts from the server.
116116
/// </summary>
117117
/// <param name="client">The client.</param>
118-
/// <param name="cancellationToken">A token to cancel the operation.</param>
118+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
119119
/// <returns>A list of all available prompts.</returns>
120120
public static async Task<IList<McpClientPrompt>> ListPromptsAsync(
121121
this IMcpClient client, CancellationToken cancellationToken = default)
@@ -150,7 +150,7 @@ public static async Task<IList<McpClientPrompt>> ListPromptsAsync(
150150
/// Creates an enumerable for asynchronously enumerating all available prompts from the server.
151151
/// </summary>
152152
/// <param name="client">The client.</param>
153-
/// <param name="cancellationToken">A token to cancel the operation.</param>
153+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
154154
/// <returns>An asynchronous sequence of all available prompts.</returns>
155155
/// <remarks>
156156
/// Every iteration through the returned <see cref="IAsyncEnumerable{Prompt}"/>
@@ -188,7 +188,7 @@ public static async IAsyncEnumerable<Prompt> EnumeratePromptsAsync(
188188
/// <param name="name">The name of the prompt to retrieve</param>
189189
/// <param name="arguments">Optional arguments for the prompt</param>
190190
/// <param name="serializerOptions">The serialization options governing argument serialization.</param>
191-
/// <param name="cancellationToken">A token to cancel the operation.</param>
191+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
192192
/// <returns>A task containing the prompt's content and messages.</returns>
193193
public static Task<GetPromptResult> GetPromptAsync(
194194
this IMcpClient client,
@@ -216,7 +216,7 @@ public static Task<GetPromptResult> GetPromptAsync(
216216
/// Retrieves a list of available resource templates from the server.
217217
/// </summary>
218218
/// <param name="client">The client.</param>
219-
/// <param name="cancellationToken">A token to cancel the operation.</param>
219+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
220220
/// <returns>A list of all available resource templates.</returns>
221221
public static async Task<IList<ResourceTemplate>> ListResourceTemplatesAsync(
222222
this IMcpClient client, CancellationToken cancellationToken = default)
@@ -255,7 +255,7 @@ public static async Task<IList<ResourceTemplate>> ListResourceTemplatesAsync(
255255
/// Creates an enumerable for asynchronously enumerating all available resource templates from the server.
256256
/// </summary>
257257
/// <param name="client">The client.</param>
258-
/// <param name="cancellationToken">A token to cancel the operation.</param>
258+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
259259
/// <returns>An asynchronous sequence of all available resource templates.</returns>
260260
/// <remarks>
261261
/// Every iteration through the returned <see cref="IAsyncEnumerable{ResourceTemplate}"/>
@@ -290,7 +290,7 @@ public static async IAsyncEnumerable<ResourceTemplate> EnumerateResourceTemplate
290290
/// Retrieves a list of available resources from the server.
291291
/// </summary>
292292
/// <param name="client">The client.</param>
293-
/// <param name="cancellationToken">A token to cancel the operation.</param>
293+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
294294
/// <returns>A list of all available resources.</returns>
295295
public static async Task<IList<Resource>> ListResourcesAsync(
296296
this IMcpClient client, CancellationToken cancellationToken = default)
@@ -329,7 +329,7 @@ public static async Task<IList<Resource>> ListResourcesAsync(
329329
/// Creates an enumerable for asynchronously enumerating all available resources from the server.
330330
/// </summary>
331331
/// <param name="client">The client.</param>
332-
/// <param name="cancellationToken">A token to cancel the operation.</param>
332+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
333333
/// <returns>An asynchronous sequence of all available resources.</returns>
334334
/// <remarks>
335335
/// Every iteration through the returned <see cref="IAsyncEnumerable{Resource}"/>
@@ -365,7 +365,7 @@ public static async IAsyncEnumerable<Resource> EnumerateResourcesAsync(
365365
/// </summary>
366366
/// <param name="client">The client.</param>
367367
/// <param name="uri">The uri of the resource.</param>
368-
/// <param name="cancellationToken">A token to cancel the operation.</param>
368+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
369369
public static Task<ReadResourceResult> ReadResourceAsync(
370370
this IMcpClient client, string uri, CancellationToken cancellationToken = default)
371371
{
@@ -387,7 +387,7 @@ public static Task<ReadResourceResult> ReadResourceAsync(
387387
/// <param name="reference">A resource (uri) or prompt (name) reference</param>
388388
/// <param name="argumentName">Name of argument. Must be non-null and non-empty.</param>
389389
/// <param name="argumentValue">Value of argument. Must be non-null.</param>
390-
/// <param name="cancellationToken">A token to cancel the operation.</param>
390+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
391391
public static Task<CompleteResult> GetCompletionAsync(this IMcpClient client, Reference reference, string argumentName, string argumentValue, CancellationToken cancellationToken = default)
392392
{
393393
Throw.IfNull(client);
@@ -416,7 +416,7 @@ public static Task<CompleteResult> GetCompletionAsync(this IMcpClient client, Re
416416
/// </summary>
417417
/// <param name="client">The client.</param>
418418
/// <param name="uri">The uri of the resource.</param>
419-
/// <param name="cancellationToken">A token to cancel the operation.</param>
419+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
420420
public static Task SubscribeToResourceAsync(this IMcpClient client, string uri, CancellationToken cancellationToken = default)
421421
{
422422
Throw.IfNull(client);
@@ -435,7 +435,7 @@ public static Task SubscribeToResourceAsync(this IMcpClient client, string uri,
435435
/// </summary>
436436
/// <param name="client">The client.</param>
437437
/// <param name="uri">The uri of the resource.</param>
438-
/// <param name="cancellationToken">A token to cancel the operation.</param>
438+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
439439
public static Task UnsubscribeFromResourceAsync(this IMcpClient client, string uri, CancellationToken cancellationToken = default)
440440
{
441441
Throw.IfNull(client);
@@ -456,7 +456,7 @@ public static Task UnsubscribeFromResourceAsync(this IMcpClient client, string u
456456
/// <param name="toolName">The name of the tool to call.</param>
457457
/// <param name="arguments">Optional arguments for the tool.</param>
458458
/// <param name="serializerOptions">The serialization options governing argument serialization.</param>
459-
/// <param name="cancellationToken">A token to cancel the operation.</param>
459+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
460460
/// <returns>A task containing the tool's response.</returns>
461461
public static Task<CallToolResponse> CallToolAsync(
462462
this IMcpClient client,
@@ -622,7 +622,7 @@ internal static CreateMessageResult ToCreateMessageResult(this ChatResponse chat
622622
/// </summary>
623623
/// <param name="client">The client.</param>
624624
/// <param name="level">The minimum log level of messages to be generated.</param>
625-
/// <param name="cancellationToken">A token to cancel the operation.</param>
625+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
626626
public static Task SetLoggingLevel(this IMcpClient client, LoggingLevel level, CancellationToken cancellationToken = default)
627627
{
628628
Throw.IfNull(client);

src/ModelContextProtocol/Client/McpClientFactory.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private static McpClientOptions CreateDefaultClientOptions()
3737
/// </param>
3838
/// <param name="createTransportFunc">An optional factory method which returns transport implementations based on a server configuration.</param>
3939
/// <param name="loggerFactory">A logger factory for creating loggers for clients.</param>
40-
/// <param name="cancellationToken">A token to cancel the operation.</param>
40+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
4141
/// <returns>An <see cref="IMcpClient"/> that's connected to the specified server.</returns>
4242
/// <exception cref="ArgumentNullException"><paramref name="serverConfig"/> is <see langword="null"/>.</exception>
4343
/// <exception cref="ArgumentNullException"><paramref name="clientOptions"/> is <see langword="null"/>.</exception>

src/ModelContextProtocol/Client/McpClientPrompt.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal McpClientPrompt(IMcpClient client, Prompt prompt)
2222
/// </summary>
2323
/// <param name="arguments">Optional arguments for the prompt</param>
2424
/// <param name="serializerOptions">The serialization options governing argument serialization.</param>
25-
/// <param name="cancellationToken">A token to cancel the operation.</param>
25+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
2626
/// <returns>A task containing the prompt's content and messages.</returns>
2727
public async ValueTask<GetPromptResult> GetAsync(
2828
IEnumerable<KeyValuePair<string, object?>>? arguments = null,

0 commit comments

Comments
 (0)