Add 3-arg RequestContext constructor and obsolete 2-arg to eliminate null-forgiving operator usage (#1462)

This commit is contained in:
Copilot 2026-03-27 12:33:23 -07:00 • committed by GitHub
commit 7dcdbafe9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 190 additions and 219 deletions

View file

@ -130,7 +130,7 @@ builder.Services
{
throw new McpException("Cannot add subscription for server with null SessionId");
}
if (ctx.Params?.Uri is { } uri)
if (ctx.Params.Uri is { } uri)
{
subscriptions[ctx.Server.SessionId].TryAdd(uri, 0);
@ -154,7 +154,7 @@ builder.Services
{
throw new McpException("Cannot remove subscription for server with null SessionId");
}
if (ctx.Params?.Uri is { } uri)
if (ctx.Params.Uri is { } uri)
{
subscriptions[ctx.Server.SessionId].TryRemove(uri, out _);
}
@ -212,11 +212,6 @@ builder.Services
})
.WithSetLoggingLevelHandler(async (ctx, ct) =>
{
if (ctx.Params?.Level is null)
{
throw new McpProtocolException("Missing required argument 'level'", McpErrorCode.InvalidParams);
}
// The SDK updates the LoggingLevel field of the IMcpServer
await ctx.Server.SendNotificationAsync("notifications/message", new

View file

@ -19,7 +19,7 @@ public class SimpleResourceType
int index = id - 1;
if ((uint)index >= ResourceGenerator.Resources.Count)
{
throw new NotSupportedException($"Unknown resource: {requestContext.Params?.Uri}");
throw new NotSupportedException($"Unknown resource: {requestContext.Params.Uri}");
}
var resource = ResourceGenerator.Resources[index];

View file

@ -15,7 +15,7 @@ public class LongRunningTool
int duration = 10,
int steps = 5)
{
var progressToken = context.Params?.ProgressToken;
var progressToken = context.Params.ProgressToken;
var stepDuration = duration / steps;
for (int i = 1; i <= steps + 1; i++)