Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions src/ModelContextProtocol/Client/McpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public McpClient(IClientTransport clientTransport, McpClientOptions options, Mcp
}

SetRequestHandler<CreateMessageRequestParams, CreateMessageResult>(
"sampling/createMessage",
RequestMethods.SamplingCreateMessage,
(request, ct) => samplingHandler(request, ct));
}

Expand All @@ -54,7 +54,7 @@ public McpClient(IClientTransport clientTransport, McpClientOptions options, Mcp
}

SetRequestHandler<ListRootsRequestParams, ListRootsResult>(
"roots/list",
RequestMethods.RootsList,
(request, ct) => rootsHandler(request, ct));
}
}
Expand Down Expand Up @@ -89,21 +89,21 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
using var initializationCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
initializationCts.CancelAfter(_options.InitializationTimeout);

try
{
// Send initialize request
var initializeResponse = await SendRequestAsync<InitializeResult>(
new JsonRpcRequest
try
{
// Send initialize request
var initializeResponse = await SendRequestAsync<InitializeResult>(
new JsonRpcRequest
{
Method = RequestMethods.Initialize,
Params = new InitializeRequestParams()
{
Method = "initialize",
Params = new InitializeRequestParams()
{
ProtocolVersion = _options.ProtocolVersion,
Capabilities = _options.Capabilities ?? new ClientCapabilities(),
ClientInfo = _options.ClientInfo,
}
},
initializationCts.Token).ConfigureAwait(false);
ProtocolVersion = _options.ProtocolVersion,
Capabilities = _options.Capabilities ?? new ClientCapabilities(),
ClientInfo = _options.ClientInfo
}
},
initializationCts.Token).ConfigureAwait(false);

// Store server information
_logger.ServerCapabilitiesReceived(EndpointName,
Expand All @@ -123,7 +123,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)

// Send initialized notification
await SendMessageAsync(
new JsonRpcNotification { Method = "notifications/initialized" },
new JsonRpcNotification { Method = NotificationMethods.InitializedNotification },
initializationCts.Token).ConfigureAwait(false);
}
catch (OperationCanceledException) when (initializationCts.IsCancellationRequested)
Expand Down
32 changes: 16 additions & 16 deletions src/ModelContextProtocol/Client/McpClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static Task PingAsync(this IMcpClient client, CancellationToken cancellat
Throw.IfNull(client);

return client.SendRequestAsync<dynamic>(
CreateRequest("ping", null),
CreateRequest(RequestMethods.Ping, null),
cancellationToken);
}

Expand All @@ -61,7 +61,7 @@ public static async Task<IList<McpClientTool>> ListToolsAsync(
do
{
var toolResults = await client.SendRequestAsync<ListToolsResult>(
CreateRequest("tools/list", CreateCursorDictionary(cursor)),
CreateRequest(RequestMethods.ToolsList, CreateCursorDictionary(cursor)),
cancellationToken).ConfigureAwait(false);

tools ??= new List<McpClientTool>(toolResults.Tools.Count);
Expand Down Expand Up @@ -96,7 +96,7 @@ public static async IAsyncEnumerable<McpClientTool> EnumerateToolsAsync(
do
{
var toolResults = await client.SendRequestAsync<ListToolsResult>(
CreateRequest("tools/list", CreateCursorDictionary(cursor)),
CreateRequest(RequestMethods.ToolsList, CreateCursorDictionary(cursor)),
cancellationToken).ConfigureAwait(false);

foreach (var tool in toolResults.Tools)
Expand Down Expand Up @@ -126,7 +126,7 @@ public static async Task<IList<Prompt>> ListPromptsAsync(
do
{
var promptResults = await client.SendRequestAsync<ListPromptsResult>(
CreateRequest("prompts/list", CreateCursorDictionary(cursor)),
CreateRequest(RequestMethods.PromptsList, CreateCursorDictionary(cursor)),
cancellationToken).ConfigureAwait(false);

if (prompts is null)
Expand Down Expand Up @@ -164,7 +164,7 @@ public static async IAsyncEnumerable<Prompt> EnumeratePromptsAsync(
do
{
var promptResults = await client.SendRequestAsync<ListPromptsResult>(
CreateRequest("prompts/list", CreateCursorDictionary(cursor)),
CreateRequest(RequestMethods.PromptsList, CreateCursorDictionary(cursor)),
cancellationToken).ConfigureAwait(false);

foreach (var prompt in promptResults.Prompts)
Expand Down Expand Up @@ -192,7 +192,7 @@ public static Task<GetPromptResult> GetPromptAsync(
Throw.IfNullOrWhiteSpace(name);

return client.SendRequestAsync<GetPromptResult>(
CreateRequest("prompts/get", CreateParametersDictionary(name, arguments)),
CreateRequest(RequestMethods.PromptsGet, CreateParametersDictionary(name, arguments)),
cancellationToken);
}

Expand All @@ -213,7 +213,7 @@ public static async Task<IList<ResourceTemplate>> ListResourceTemplatesAsync(
do
{
var templateResults = await client.SendRequestAsync<ListResourceTemplatesResult>(
CreateRequest("resources/templates/list", CreateCursorDictionary(cursor)),
CreateRequest(RequestMethods.ResourcesTemplatesList, CreateCursorDictionary(cursor)),
cancellationToken).ConfigureAwait(false);

if (templates is null)
Expand Down Expand Up @@ -251,7 +251,7 @@ public static async IAsyncEnumerable<ResourceTemplate> EnumerateResourceTemplate
do
{
var templateResults = await client.SendRequestAsync<ListResourceTemplatesResult>(
CreateRequest("resources/templates/list", CreateCursorDictionary(cursor)),
CreateRequest(RequestMethods.ResourcesTemplatesList, CreateCursorDictionary(cursor)),
cancellationToken).ConfigureAwait(false);

foreach (var template in templateResults.ResourceTemplates)
Expand Down Expand Up @@ -281,7 +281,7 @@ public static async Task<IList<Resource>> ListResourcesAsync(
do
{
var resourceResults = await client.SendRequestAsync<ListResourcesResult>(
CreateRequest("resources/list", CreateCursorDictionary(cursor)),
CreateRequest(RequestMethods.ResourcesList, CreateCursorDictionary(cursor)),
cancellationToken).ConfigureAwait(false);

if (resources is null)
Expand Down Expand Up @@ -319,7 +319,7 @@ public static async IAsyncEnumerable<Resource> EnumerateResourcesAsync(
do
{
var resourceResults = await client.SendRequestAsync<ListResourcesResult>(
CreateRequest("resources/list", CreateCursorDictionary(cursor)),
CreateRequest(RequestMethods.ResourcesList, CreateCursorDictionary(cursor)),
cancellationToken).ConfigureAwait(false);

foreach (var resource in resourceResults.Resources)
Expand All @@ -345,7 +345,7 @@ public static Task<ReadResourceResult> ReadResourceAsync(
Throw.IfNullOrWhiteSpace(uri);

return client.SendRequestAsync<ReadResourceResult>(
CreateRequest("resources/read", new() { ["uri"] = uri }),
CreateRequest(RequestMethods.ResourcesRead, new() { ["uri"] = uri }),
cancellationToken);
}

Expand All @@ -369,7 +369,7 @@ public static Task<CompleteResult> GetCompletionAsync(this IMcpClient client, Re
}

return client.SendRequestAsync<CompleteResult>(
CreateRequest("completion/complete", new()
CreateRequest(RequestMethods.CompletionComplete, new()
{
["ref"] = reference,
["argument"] = new Argument { Name = argumentName, Value = argumentValue }
Expand All @@ -389,7 +389,7 @@ public static Task SubscribeToResourceAsync(this IMcpClient client, string uri,
Throw.IfNullOrWhiteSpace(uri);

return client.SendRequestAsync<EmptyResult>(
CreateRequest("resources/subscribe", new() { ["uri"] = uri }),
CreateRequest(RequestMethods.ResourcesSubscribe, new() { ["uri"] = uri }),
cancellationToken);
}

Expand All @@ -405,7 +405,7 @@ public static Task UnsubscribeFromResourceAsync(this IMcpClient client, string u
Throw.IfNullOrWhiteSpace(uri);

return client.SendRequestAsync<EmptyResult>(
CreateRequest("resources/unsubscribe", new() { ["uri"] = uri }),
CreateRequest(RequestMethods.ResourcesUnsubscribe, new() { ["uri"] = uri }),
cancellationToken);
}

Expand All @@ -424,7 +424,7 @@ public static Task<CallToolResponse> CallToolAsync(
Throw.IfNull(toolName);

return client.SendRequestAsync<CallToolResponse>(
CreateRequest("tools/call", CreateParametersDictionary(toolName, arguments)),
CreateRequest(RequestMethods.ToolsCall, CreateParametersDictionary(toolName, arguments)),
cancellationToken);
}

Expand Down Expand Up @@ -560,7 +560,7 @@ public static Task SetLoggingLevel(this IMcpClient client, LoggingLevel level, C
Throw.IfNull(client);

return client.SendRequestAsync<EmptyResult>(
CreateRequest("logging/setLevel", new() { ["level"] = level }),
CreateRequest(RequestMethods.LoggingSetLevel, new() { ["level"] = level }),
cancellationToken);
}

Expand Down
12 changes: 12 additions & 0 deletions src/ModelContextProtocol/NopProgress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ModelContextProtocol;

/// <summary>Provides an <see cref="IProgress{ProgressNotificationValue}"/> that's a nop.</summary>
internal sealed class NullProgress : IProgress<ProgressNotificationValue>
{
public static NullProgress Instance { get; } = new();

/// <inheritdoc />
public void Report(ProgressNotificationValue value)
{
}
}
14 changes: 14 additions & 0 deletions src/ModelContextProtocol/ProgressNotificationValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace ModelContextProtocol;

/// <summary>Provides a progress value that can be sent using <see cref="IProgress{ProgressNotificationValue}"/>.</summary>
public record struct ProgressNotificationValue
{
/// <summary>Gets or sets the progress thus far.</summary>
public required float Progress { get; init; }

/// <summary>Gets or sets the total number of items to process (or total progress required), if known.</summary>
public float? Total { get; init; }

/// <summary>Gets or sets an optional message describing the current progress.</summary>
public string? Message { get; init; }
}
23 changes: 23 additions & 0 deletions src/ModelContextProtocol/Protocol/Messages/NotificationMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,27 @@ public static class NotificationMethods
/// Sent by the server when a log message is generated.
/// </summary>
public const string LoggingMessageNotification = "notifications/message";

/// <summary>
/// Sent from the client to the server after initialization has finished.
/// </summary>
public const string InitializedNotification = "notifications/initialized";

/// <summary>
/// Sent to inform the receiver of a progress update for a long-running request.
/// </summary>
public const string ProgressNotification = "notifications/progress";

/// <summary>
/// Sent by either side to indicate that it is cancelling a previously-issued request.
/// </summary>
/// <remarks>
/// The request SHOULD still be in-flight, but due to communication latency, it is always possible that this notification
/// MAY arrive after the request has already finished.
///
/// This notification indicates that the result will be unused, so any associated processing SHOULD cease.
///
/// A client MUST NOT attempt to cancel its `initialize` request.".
/// </remarks>
public const string CancelledNotification = "notifications/cancelled";
}
43 changes: 0 additions & 43 deletions src/ModelContextProtocol/Protocol/Messages/OperationNames.cs

This file was deleted.

Loading