Skip to content

Commit 1dd9067

Browse files
Merge branch 'main' of https://github.com/Tyler-R-Kendrick/mcp-csharp-sdk into cancellation-enhancements
2 parents 756ceb7 + 8bd9ee8 commit 1dd9067

30 files changed

+585
-315
lines changed

samples/AspNetCoreSseServer/Properties/launchSettings.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"http": {
55
"commandName": "Project",
66
"dotnetRunMessages": true,
7-
"launchBrowser": true,
87
"applicationUrl": "http://localhost:3001",
98
"environmentVariables": {
109
"ASPNETCORE_ENVIRONMENT": "Development"
@@ -13,7 +12,6 @@
1312
"https": {
1413
"commandName": "Project",
1514
"dotnetRunMessages": true,
16-
"launchBrowser": true,
1715
"applicationUrl": "https://localhost:7133;http://localhost:3001",
1816
"environmentVariables": {
1917
"ASPNETCORE_ENVIRONMENT": "Development"

samples/EverythingServer/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ await ctx.Server.RequestSamplingAsync([
115115
}
116116
return Task.FromResult(new EmptyResult());
117117
})
118-
.WithGetCompletionHandler((ctx, ct) =>
118+
.WithCompleteHandler((ctx, ct) =>
119119
{
120120
var exampleCompletions = new Dictionary<string, IEnumerable<string>>
121121
{

samples/QuickstartWeatherServer/Tools/WeatherTools.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ public static async Task<string> GetForecast(
4242
[Description("Longitude of the location.")] double longitude)
4343
{
4444
using var jsonDocument = await client.ReadJsonDocumentAsync($"/points/{latitude},{longitude}");
45-
var jsonElement = jsonDocument.RootElement;
46-
var periods = jsonElement.GetProperty("properties").GetProperty("periods").EnumerateArray();
45+
var forecastUrl = jsonDocument.RootElement.GetProperty("properties").GetProperty("forecast").GetString()
46+
?? throw new Exception($"No forecast URL provided by {client.BaseAddress}points/{latitude},{longitude}");
47+
48+
using var forecastDocument = await client.ReadJsonDocumentAsync(forecastUrl);
49+
var periods = forecastDocument.RootElement.GetProperty("properties").GetProperty("periods").EnumerateArray();
4750

4851
return string.Join("\n---\n", periods.Select(period => $"""
4952
{period.GetProperty("name").GetString()}
@@ -52,4 +55,4 @@ public static async Task<string> GetForecast(
5255
Forecast: {period.GetProperty("detailedForecast").GetString()}
5356
"""));
5457
}
55-
}
58+
}

src/ModelContextProtocol/Client/McpClientExtensions.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Microsoft.Extensions.AI;
2+
using Microsoft.Extensions.Logging;
23
using ModelContextProtocol.Protocol.Messages;
34
using ModelContextProtocol.Protocol.Types;
5+
using ModelContextProtocol.Server;
46
using ModelContextProtocol.Utils;
57
using ModelContextProtocol.Utils.Json;
68
using System.Runtime.CompilerServices;
@@ -386,7 +388,7 @@ public static Task<ReadResourceResult> ReadResourceAsync(
386388
/// <param name="argumentName">Name of argument. Must be non-null and non-empty.</param>
387389
/// <param name="argumentValue">Value of argument. Must be non-null.</param>
388390
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
389-
public static Task<CompleteResult> GetCompletionAsync(this IMcpClient client, Reference reference, string argumentName, string argumentValue, CancellationToken cancellationToken = default)
391+
public static Task<CompleteResult> CompleteAsync(this IMcpClient client, Reference reference, string argumentName, string argumentValue, CancellationToken cancellationToken = default)
390392
{
391393
Throw.IfNull(client);
392394
Throw.IfNull(reference);
@@ -631,6 +633,15 @@ public static Task SetLoggingLevel(this IMcpClient client, LoggingLevel level, C
631633
cancellationToken: cancellationToken);
632634
}
633635

636+
/// <summary>
637+
/// Configures the minimum logging level for the server.
638+
/// </summary>
639+
/// <param name="client">The client.</param>
640+
/// <param name="level">The minimum log level of messages to be generated.</param>
641+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
642+
public static Task SetLoggingLevel(this IMcpClient client, LogLevel level, CancellationToken cancellationToken = default) =>
643+
SetLoggingLevel(client, McpServer.ToLoggingLevel(level), cancellationToken);
644+
634645
/// <summary>Convers a dictionary with <see cref="object"/> values to a dictionary with <see cref="JsonElement"/> values.</summary>
635646
private static IReadOnlyDictionary<string, JsonElement>? ToArgumentsDictionary(
636647
IReadOnlyDictionary<string, object?>? arguments, JsonSerializerOptions options)

src/ModelContextProtocol/Configuration/McpServerBuilderExtensions.cs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.Extensions.Logging;
1+
using Microsoft.Extensions.DependencyInjection.Extensions;
2+
using Microsoft.Extensions.Logging;
23
using Microsoft.Extensions.Options;
34
using ModelContextProtocol.Hosting;
45
using ModelContextProtocol.Protocol.Transport;
@@ -288,15 +289,15 @@ public static IMcpServerBuilder WithReadResourceHandler(this IMcpServerBuilder b
288289
}
289290

290291
/// <summary>
291-
/// Sets the handler for get completion requests.
292+
/// Sets the handler for completion complete requests.
292293
/// </summary>
293294
/// <param name="builder">The builder instance.</param>
294295
/// <param name="handler">The handler.</param>
295-
public static IMcpServerBuilder WithGetCompletionHandler(this IMcpServerBuilder builder, Func<RequestContext<CompleteRequestParams>, CancellationToken, Task<CompleteResult>> handler)
296+
public static IMcpServerBuilder WithCompleteHandler(this IMcpServerBuilder builder, Func<RequestContext<CompleteRequestParams>, CancellationToken, Task<CompleteResult>> handler)
296297
{
297298
Throw.IfNull(builder);
298299

299-
builder.Services.Configure<McpServerHandlers>(s => s.GetCompletionHandler = handler);
300+
builder.Services.Configure<McpServerHandlers>(s => s.CompleteHandler = handler);
300301
return builder;
301302
}
302303

@@ -348,17 +349,8 @@ public static IMcpServerBuilder WithStdioServerTransport(this IMcpServerBuilder
348349
{
349350
Throw.IfNull(builder);
350351

352+
AddSingleSessionServerDependencies(builder.Services);
351353
builder.Services.AddSingleton<ITransport, StdioServerTransport>();
352-
builder.Services.AddHostedService<SingleSessionMcpServerHostedService>();
353-
354-
builder.Services.AddSingleton(services =>
355-
{
356-
ITransport serverTransport = services.GetRequiredService<ITransport>();
357-
IOptions<McpServerOptions> options = services.GetRequiredService<IOptions<McpServerOptions>>();
358-
ILoggerFactory? loggerFactory = services.GetService<ILoggerFactory>();
359-
360-
return McpServerFactory.Create(serverTransport, options.Value, loggerFactory, services);
361-
});
362354

363355
return builder;
364356
}
@@ -378,19 +370,22 @@ public static IMcpServerBuilder WithStreamServerTransport(
378370
Throw.IfNull(inputStream);
379371
Throw.IfNull(outputStream);
380372

373+
AddSingleSessionServerDependencies(builder.Services);
381374
builder.Services.AddSingleton<ITransport>(new StreamServerTransport(inputStream, outputStream));
382-
builder.Services.AddHostedService<SingleSessionMcpServerHostedService>();
383375

384-
builder.Services.AddSingleton(services =>
376+
return builder;
377+
}
378+
379+
private static void AddSingleSessionServerDependencies(IServiceCollection services)
380+
{
381+
services.AddHostedService<SingleSessionMcpServerHostedService>();
382+
services.TryAddSingleton(services =>
385383
{
386384
ITransport serverTransport = services.GetRequiredService<ITransport>();
387385
IOptions<McpServerOptions> options = services.GetRequiredService<IOptions<McpServerOptions>>();
388386
ILoggerFactory? loggerFactory = services.GetService<ILoggerFactory>();
389-
390387
return McpServerFactory.Create(serverTransport, options.Value, loggerFactory, services);
391388
});
392-
393-
return builder;
394389
}
395390
#endregion
396391
}

src/ModelContextProtocol/McpEndpointExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ internal static async Task<TResult> SendRequestAsync<TParameters, TResult>(
8383
}
8484

8585
/// <summary>
86-
/// Sends a notification to the server with parameters.
86+
/// Sends a notification to the server with no parameters.
8787
/// </summary>
8888
/// <param name="client">The client.</param>
8989
/// <param name="method">The notification method name.</param>

src/ModelContextProtocol/Protocol/Types/Capabilities.cs

Lines changed: 0 additions & 207 deletions
This file was deleted.

0 commit comments

Comments
 (0)