Skip to content

Commit 7dcdbaf

Browse files
authored
Add 3-arg RequestContext constructor and obsolete 2-arg to eliminate null-forgiving operator usage (#1462)
1 parent afc40d1 commit 7dcdbaf

33 files changed

+190
-219
lines changed

docs/concepts/filters.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ Execution flow: `filter1 -> filter2 -> filter3 -> baseHandler -> filter3 -> filt
317317
{
318318
var logger = context.Services?.GetService<ILogger<Program>>();
319319

320-
logger?.LogInformation($"Processing request from {context.Params?.ProgressToken}");
320+
logger?.LogInformation($"Processing request from {context.Params.ProgressToken}");
321321
var result = await next(context, cancellationToken);
322322
logger?.LogInformation($"Returning {result.Tools?.Count ?? 0} tools");
323323
return result;
@@ -339,7 +339,7 @@ Execution flow: `filter1 -> filter2 -> filter3 -> baseHandler -> filter3 -> filt
339339
catch (Exception ex)
340340
{
341341
var logger = context.Services?.GetService<ILogger<Program>>();
342-
logger?.LogError(ex, "Error while processing CallTool request for {ProgressToken}", context.Params?.ProgressToken);
342+
logger?.LogError(ex, "Error while processing CallTool request for {ProgressToken}", context.Params.ProgressToken);
343343

344344
return new CallToolResult
345345
{

docs/concepts/logging/samples/server/Tools/LoggingTools.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static async Task<string> LoggingTool(
1313
int duration = 10,
1414
int steps = 10)
1515
{
16-
var progressToken = context.Params?.ProgressToken;
16+
var progressToken = context.Params.ProgressToken;
1717
var stepDuration = duration / steps;
1818

1919
// <snippet_LoggingConfiguration >

docs/concepts/pagination/pagination.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ builder.Services.AddMcpServer()
7777
int startIndex = 0;
7878

7979
// Parse cursor to determine starting position
80-
if (ctx.Params?.Cursor is { } cursor)
80+
if (ctx.Params.Cursor is { } cursor)
8181
{
8282
startIndex = int.Parse(cursor);
8383
}

docs/concepts/progress/samples/server/Tools/LongRunningTools.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static async Task<string> LongRunningTool(
1515
int duration = 10,
1616
int steps = 5)
1717
{
18-
var progressToken = context.Params?.ProgressToken;
18+
var progressToken = context.Params.ProgressToken;
1919
var stepDuration = duration / steps;
2020

2121
for (int i = 1; i <= steps; i++)

docs/concepts/resources/resources.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ builder.Services.AddMcpServer()
212212
.WithResources<MyResources>()
213213
.WithSubscribeToResourcesHandler(async (ctx, ct) =>
214214
{
215-
if (ctx.Params?.Uri is { } uri)
215+
if (ctx.Params.Uri is { } uri)
216216
{
217217
// Track the subscription (e.g., in a concurrent dictionary)
218218
subscriptions[ctx.Server.SessionId].TryAdd(uri, 0);
@@ -221,7 +221,7 @@ builder.Services.AddMcpServer()
221221
})
222222
.WithUnsubscribeFromResourcesHandler(async (ctx, ct) =>
223223
{
224-
if (ctx.Params?.Uri is { } uri)
224+
if (ctx.Params.Uri is { } uri)
225225
{
226226
subscriptions[ctx.Server.SessionId].TryRemove(uri, out _);
227227
}

docs/list-of-diagnostics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ When APIs are marked as obsolete, a diagnostic is emitted to warn users that the
3636
| :------------ | :----- | :---------- |
3737
| `MCP9001` | In place | The `EnumSchema` and `LegacyTitledEnumSchema` APIs are deprecated as of specification version 2025-11-25. Use the current schema APIs instead. |
3838
| `MCP9002` | Removed | The `AddXxxFilter` extension methods on `IMcpServerBuilder` (e.g., `AddListToolsFilter`, `AddCallToolFilter`, `AddIncomingMessageFilter`) were superseded by `WithRequestFilters()` and `WithMessageFilters()`. |
39+
| `MCP9003` | In place | The `RequestContext<TParams>(McpServer, JsonRpcRequest)` constructor is obsolete. Use the overload that accepts a `parameters` argument: `RequestContext<TParams>(McpServer, JsonRpcRequest, TParams)`. |

samples/EverythingServer/Program.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
{
131131
throw new McpException("Cannot add subscription for server with null SessionId");
132132
}
133-
if (ctx.Params?.Uri is { } uri)
133+
if (ctx.Params.Uri is { } uri)
134134
{
135135
subscriptions[ctx.Server.SessionId].TryAdd(uri, 0);
136136

@@ -154,7 +154,7 @@ await ctx.Server.SampleAsync([
154154
{
155155
throw new McpException("Cannot remove subscription for server with null SessionId");
156156
}
157-
if (ctx.Params?.Uri is { } uri)
157+
if (ctx.Params.Uri is { } uri)
158158
{
159159
subscriptions[ctx.Server.SessionId].TryRemove(uri, out _);
160160
}
@@ -212,11 +212,6 @@ await ctx.Server.SampleAsync([
212212
})
213213
.WithSetLoggingLevelHandler(async (ctx, ct) =>
214214
{
215-
if (ctx.Params?.Level is null)
216-
{
217-
throw new McpProtocolException("Missing required argument 'level'", McpErrorCode.InvalidParams);
218-
}
219-
220215
// The SDK updates the LoggingLevel field of the IMcpServer
221216

222217
await ctx.Server.SendNotificationAsync("notifications/message", new

samples/EverythingServer/Resources/SimpleResourceType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static ResourceContents TemplateResource(RequestContext<ReadResourceReque
1919
int index = id - 1;
2020
if ((uint)index >= ResourceGenerator.Resources.Count)
2121
{
22-
throw new NotSupportedException($"Unknown resource: {requestContext.Params?.Uri}");
22+
throw new NotSupportedException($"Unknown resource: {requestContext.Params.Uri}");
2323
}
2424

2525
var resource = ResourceGenerator.Resources[index];

samples/EverythingServer/Tools/LongRunningTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static async Task<string> LongRunningOperation(
1515
int duration = 10,
1616
int steps = 5)
1717
{
18-
var progressToken = context.Params?.ProgressToken;
18+
var progressToken = context.Params.ProgressToken;
1919
var stepDuration = duration / steps;
2020

2121
for (int i = 1; i <= steps + 1; i++)

src/Common/Obsoletions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ internal static class Obsoletions
2525

2626
// MCP9002 was used for the AddXxxFilter extension methods on IMcpServerBuilder that were superseded by
2727
// WithMessageFilters() and WithRequestFilters(). The APIs were removed; do not reuse this diagnostic ID.
28+
29+
public const string RequestContextParamsConstructor_DiagnosticId = "MCP9003";
30+
public const string RequestContextParamsConstructor_Message = "Use the constructor overload that accepts a parameters argument.";
31+
public const string RequestContextParamsConstructor_Url = "https://github.com/modelcontextprotocol/csharp-sdk/blob/main/docs/list-of-diagnostics.md#mcp9003";
2832
}

0 commit comments

Comments
 (0)