Skip to content

Commit e9faef7

Browse files
committed
Address feedback and fix test
1 parent 6aa993d commit e9faef7

File tree

13 files changed

+118
-248
lines changed

13 files changed

+118
-248
lines changed
Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using ModelContextProtocol.Protocol.Messages;
2-
using ModelContextProtocol.Protocol.Types;
1+
using ModelContextProtocol.Protocol.Types;
32

43
namespace ModelContextProtocol.Client;
54

65
/// <summary>
76
/// Represents an instance of an MCP client connecting to a specific server.
87
/// </summary>
9-
public interface IMcpClient : IAsyncDisposable
8+
public interface IMcpClient : IMcpEndpoint
109
{
1110
/// <summary>
1211
/// Gets the capabilities supported by the server.
@@ -24,40 +23,4 @@ public interface IMcpClient : IAsyncDisposable
2423
/// It can be thought of like a "hint" to the model. For example, this information MAY be added to the system prompt.
2524
/// </summary>
2625
string? ServerInstructions { get; }
27-
28-
/// <summary>
29-
/// Adds a handler for server notifications of a specific method.
30-
/// </summary>
31-
/// <param name="method">The notification method to handle.</param>
32-
/// <param name="handler">The async handler function to process notifications.</param>
33-
/// <remarks>
34-
/// <para>
35-
/// Each method may have multiple handlers. Adding a handler for a method that already has one
36-
/// will not replace the existing handler.
37-
/// </para>
38-
/// <para>
39-
/// <see cref="NotificationMethods"> provides constants for common notification methods.</see>
40-
/// </para>
41-
/// </remarks>
42-
void AddNotificationHandler(string method, Func<JsonRpcNotification, Task> handler);
43-
44-
/// <summary>
45-
/// Sends a generic JSON-RPC request to the server.
46-
/// </summary>
47-
/// <typeparam name="TResult">The expected response type.</typeparam>
48-
/// <param name="request">The JSON-RPC request to send.</param>
49-
/// <param name="cancellationToken">A token to cancel the operation.</param>
50-
/// <returns>A task containing the server's response.</returns>
51-
/// <remarks>
52-
/// It is recommended to use the capability-specific methods that use this one in their implementation.
53-
/// Use this method for custom requests or those not yet covered explicitly.
54-
/// </remarks>
55-
Task<TResult> SendRequestAsync<TResult>(JsonRpcRequest request, CancellationToken cancellationToken = default) where TResult : class;
56-
57-
/// <summary>
58-
/// Sends a message to the server.
59-
/// </summary>
60-
/// <param name="message">The message.</param>
61-
/// <param name="cancellationToken">A token to cancel the operation.</param>
62-
Task SendMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken = default);
6326
}

src/ModelContextProtocol/Client/McpClient.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ public McpClient(IClientTransport clientTransport, McpClientOptions options, Mcp
4242

4343
SetRequestHandler<CreateMessageRequestParams, CreateMessageResult>(
4444
RequestMethods.SamplingCreateMessage,
45-
(request, ct) => samplingHandler(request, new ClientTokenProgress(this, request?.Meta?.ProgressToken), ct));
45+
(request, cancellationToken) => samplingHandler(
46+
request,
47+
request?.Meta?.ProgressToken is { } token ? new TokenProgress(this, token) : NullProgress.Instance,
48+
cancellationToken));
4649
}
4750

4851
if (options.Capabilities?.Roots is { } rootsCapability)
@@ -54,7 +57,7 @@ public McpClient(IClientTransport clientTransport, McpClientOptions options, Mcp
5457

5558
SetRequestHandler<ListRootsRequestParams, ListRootsResult>(
5659
RequestMethods.RootsList,
57-
(request, ct) => rootsHandler(request, ct));
60+
(request, cancellationToken) => rootsHandler(request, cancellationToken));
5861
}
5962
}
6063

src/ModelContextProtocol/Client/McpClientExtensions.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
namespace ModelContextProtocol.Client;
1010

11-
/// <summary>
12-
/// Provides extensions for operating on MCP clients.
13-
/// </summary>
11+
/// <summary>Provides extension methods for interacting with an <see cref="IMcpClient"/>.</summary>
1412
public static class McpClientExtensions
1513
{
1614
/// <summary>
@@ -542,18 +540,17 @@ internal static CreateMessageResult ToCreateMessageResult(this ChatResponse chat
542540

543541
var (messages, options) = requestParams.ToChatClientArguments();
544542
var progressToken = requestParams.Meta?.ProgressToken;
545-
int progressValue = 0;
546-
var streamingResponses = chatClient.GetStreamingResponseAsync(
547-
messages, options, cancellationToken);
543+
548544
List<ChatResponseUpdate> updates = [];
549-
await foreach (var streamingResponse in streamingResponses)
545+
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, options, cancellationToken))
550546
{
551-
updates.Add(streamingResponse);
547+
updates.Add(update);
548+
552549
if (progressToken is not null)
553550
{
554551
progress.Report(new()
555552
{
556-
Progress = ++progressValue,
553+
Progress = updates.Count,
557554
});
558555
}
559556
}

src/ModelContextProtocol/ClientTokenProgress.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using ModelContextProtocol.Protocol.Messages;
2+
3+
namespace ModelContextProtocol;
4+
5+
/// <summary>Represents a client or server MCP endpoint.</summary>
6+
public interface IMcpEndpoint : IAsyncDisposable
7+
{
8+
/// <summary>Sends a generic JSON-RPC request to the connected endpoint.</summary>
9+
/// <typeparam name="TResult">The expected response type.</typeparam>
10+
/// <param name="request">The JSON-RPC request to send.</param>
11+
/// <param name="cancellationToken">A token to cancel the operation.</param>
12+
/// <returns>A task containing the client's response.</returns>
13+
Task<TResult> SendRequestAsync<TResult>(JsonRpcRequest request, CancellationToken cancellationToken = default) where TResult : class;
14+
15+
/// <summary>Sends a message to the connected endpoint.</summary>
16+
/// <param name="message">The message.</param>
17+
/// <param name="cancellationToken">A token to cancel the operation.</param>
18+
Task SendMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken = default);
19+
20+
/// <summary>
21+
/// Adds a handler for server notifications of a specific method.
22+
/// </summary>
23+
/// <param name="method">The notification method to handle.</param>
24+
/// <param name="handler">The async handler function to process notifications.</param>
25+
/// <remarks>
26+
/// <para>
27+
/// Each method may have multiple handlers. Adding a handler for a method that already has one
28+
/// will not replace the existing handler.
29+
/// </para>
30+
/// <para>
31+
/// <see cref="NotificationMethods"> provides constants for common notification methods.</see>
32+
/// </para>
33+
/// </remarks>
34+
void AddNotificationHandler(string method, Func<JsonRpcNotification, Task> handler);
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using ModelContextProtocol.Protocol.Messages;
2+
using ModelContextProtocol.Utils;
3+
4+
namespace ModelContextProtocol;
5+
6+
/// <summary>Provides extension methods for interacting with an <see cref="IMcpEndpoint"/>.</summary>
7+
public static class McpEndpointExtensions
8+
{
9+
/// <summary>Notifies the connected endpoint of progress.</summary>
10+
/// <param name="endpoint">The endpoint issueing the notification.</param>
11+
/// <param name="progressToken">The <see cref="ProgressToken"/> identifying the operation.</param>
12+
/// <param name="progress">The progress update to send.</param>
13+
/// <param name="cancellationToken">A token to cancel the operation.</param>
14+
/// <returns>A task representing the completion of the operation.</returns>
15+
/// <exception cref="ArgumentNullException"><paramref name="endpoint"/> is <see langword="null"/>.</exception>
16+
public static Task NotifyProgressAsync(
17+
this IMcpEndpoint endpoint,
18+
ProgressToken progressToken,
19+
ProgressNotificationValue progress,
20+
CancellationToken cancellationToken = default)
21+
{
22+
Throw.IfNull(endpoint);
23+
24+
return endpoint.SendMessageAsync(new JsonRpcNotification()
25+
{
26+
Method = NotificationMethods.ProgressNotification,
27+
Params = new ProgressNotification()
28+
{
29+
ProgressToken = progressToken,
30+
Progress = progress,
31+
},
32+
}, cancellationToken);
33+
}
34+
}

src/ModelContextProtocol/Protocol/Types/Tool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public JsonElement InputSchema
3838
{
3939
if (!McpJsonUtilities.IsValidMcpToolSchema(value))
4040
{
41-
throw new ArgumentException("The specified document is not a valid MPC tool JSON schema.", nameof(InputSchema));
41+
throw new ArgumentException("The specified document is not a valid MCP tool JSON schema.", nameof(InputSchema));
4242
}
4343

4444
_inputSchema = value;

src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private static TemporaryAIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
111111
if (requestContent?.Server is { } server &&
112112
requestContent?.Params?.Meta?.ProgressToken is { } progressToken)
113113
{
114-
return new ServerTokenProgress(server, progressToken);
114+
return new TokenProgress(server, progressToken);
115115
}
116116

117117
return NullProgress.Instance;
Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using ModelContextProtocol.Protocol.Messages;
2-
using ModelContextProtocol.Protocol.Types;
1+
using ModelContextProtocol.Protocol.Types;
32

43
namespace ModelContextProtocol.Server;
54

65
/// <summary>
76
/// Represents a server that can communicate with a client using the MCP protocol.
87
/// </summary>
9-
public interface IMcpServer : IAsyncDisposable
8+
public interface IMcpServer : IMcpEndpoint
109
{
1110
/// <summary>
1211
/// Gets the capabilities supported by the client.
@@ -26,42 +25,8 @@ public interface IMcpServer : IAsyncDisposable
2625
/// </summary>
2726
IServiceProvider? Services { get; }
2827

29-
/// <summary>
30-
/// Adds a handler for client notifications of a specific method.
31-
/// </summary>
32-
/// <param name="method">The notification method to handle.</param>
33-
/// <param name="handler">The async handler function to process notifications.</param>
34-
/// <remarks>
35-
/// <para>
36-
/// Each method may have multiple handlers. Adding a handler for a method that already has one
37-
/// will not replace the existing handler.
38-
/// </para>
39-
/// <para>
40-
/// <see cref="NotificationMethods"> provides constants for common notification methods.</see>
41-
/// </para>
42-
/// </remarks>
43-
void AddNotificationHandler(string method, Func<JsonRpcNotification, Task> handler);
44-
4528
/// <summary>
4629
/// Runs the server, listening for and handling client requests.
4730
/// </summary>
4831
Task RunAsync(CancellationToken cancellationToken = default);
49-
50-
/// <summary>
51-
/// Sends a generic JSON-RPC request to the client.
52-
/// NB! This is a temporary method that is available to send not yet implemented feature messages.
53-
/// Once all MCP features are implemented this will be made private, as it is purely a convenience for those who wish to implement features ahead of the library.
54-
/// </summary>
55-
/// <typeparam name="TResult">The expected response type.</typeparam>
56-
/// <param name="request">The JSON-RPC request to send.</param>
57-
/// <param name="cancellationToken">A token to cancel the operation.</param>
58-
/// <returns>A task containing the client's response.</returns>
59-
Task<TResult> SendRequestAsync<TResult>(JsonRpcRequest request, CancellationToken cancellationToken = default) where TResult : class;
60-
61-
/// <summary>
62-
/// Sends a message to the client.
63-
/// </summary>
64-
/// <param name="message">The message.</param>
65-
/// <param name="cancellationToken">A token to cancel the operation.</param>
66-
Task SendMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken = default);
6732
}

src/ModelContextProtocol/Server/McpServerExtensions.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace ModelContextProtocol.Server;
99

10-
/// <inheritdoc />
10+
/// <summary>Provides extension methods for interacting with an <see cref="IMcpServer"/>.</summary>
1111
public static class McpServerExtensions
1212
{
1313
/// <summary>
@@ -169,26 +169,6 @@ public static Task<ListRootsResult> RequestRootsAsync(
169169
cancellationToken);
170170
}
171171

172-
/// <summary>
173-
/// Requests the client to list the roots it exposes.
174-
/// </summary>
175-
/// <param name="server">The server issueing the request.</param>
176-
/// <param name="notification">The notification to send.</param>
177-
/// <param name="cancellationToken">A token to cancel the operation.</param>
178-
/// <returns>A task containing the response from the client.</returns>
179-
public static Task NotifyProgressAsync(
180-
this IMcpServer server,
181-
ProgressNotification notification,
182-
CancellationToken cancellationToken = default)
183-
{
184-
Throw.IfNull(server);
185-
return server.SendMessageAsync(new JsonRpcNotification()
186-
{
187-
Method = NotificationMethods.ProgressNotification,
188-
Params = notification,
189-
}, cancellationToken);
190-
}
191-
192172
/// <summary>Provides an <see cref="IChatClient"/> implementation that's implemented via client sampling.</summary>
193173
/// <param name="server"></param>
194174
private sealed class SamplingChatClient(IMcpServer server) : IChatClient

src/ModelContextProtocol/ServerTokenProgress.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
11
using ModelContextProtocol.Protocol.Messages;
2-
using ModelContextProtocol.Server;
32

43
namespace ModelContextProtocol;
54

65
/// <summary>
76
/// Provides an <see cref="IProgress{ProgressNotificationValue}"/> tied to a specific progress token and that will issue
8-
/// progress notifications to the supplied endpoint.
7+
/// progress notifications on the supplied endpoint.
98
/// </summary>
10-
internal sealed class TokenProgress(IMcpServer server, ProgressToken progressToken) : IProgress<ProgressNotificationValue>
9+
internal sealed class TokenProgress(IMcpEndpoint endpoint, ProgressToken progressToken) : IProgress<ProgressNotificationValue>
1110
{
1211
/// <inheritdoc />
1312
public void Report(ProgressNotificationValue value)
1413
{
15-
_ = server.SendMessageAsync(new JsonRpcNotification()
16-
{
17-
Method = NotificationMethods.ProgressNotification,
18-
Params = new ProgressNotification()
19-
{
20-
ProgressToken = progressToken,
21-
Progress = new()
22-
{
23-
Progress = value.Progress,
24-
Total = value.Total,
25-
Message = value.Message,
26-
},
27-
},
28-
}, CancellationToken.None);
14+
_ = endpoint.NotifyProgressAsync(progressToken, value, CancellationToken.None);
2915
}
3016
}

0 commit comments

Comments
 (0)