Skip to content

Commit 410cfe4

Browse files
author
Tarek Mahmoud Sayed
committed
Add [NotNullWhen(false)] to ValidateMcpHeaders and remove null-forgiving operator
1 parent 66deb5e commit 410cfe4

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

src/ModelContextProtocol.AspNetCore/StreamableHttpHandler.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using ModelContextProtocol.Protocol;
99
using ModelContextProtocol.Server;
1010
using System.Collections.Concurrent;
11+
using System.Diagnostics.CodeAnalysis;
1112
using System.Security.Claims;
1213
using System.Security.Cryptography;
1314
using System.Text.Json.Serialization.Metadata;
@@ -82,7 +83,7 @@ await WriteJsonRpcErrorAsync(context,
8283

8384
if (!ValidateMcpHeaders(context, message, mcpServerOptionsSnapshot.Value.ToolCollection, out errorMessage))
8485
{
85-
await WriteJsonRpcErrorAsync(context, errorMessage!, StatusCodes.Status400BadRequest, (int)McpErrorCode.HeaderMismatch);
86+
await WriteJsonRpcErrorAsync(context, errorMessage, StatusCodes.Status400BadRequest, (int)McpErrorCode.HeaderMismatch);
8687
return;
8788
}
8889

@@ -557,7 +558,7 @@ private static bool ValidateProtocolVersionHeader(HttpContext context, out strin
557558
/// <param name="toolCollection">The tool collection to look up tool schemas for parameter header validation.</param>
558559
/// <param name="errorMessage">Set to the error message if validation fails; null otherwise.</param>
559560
/// <returns>True if validation passes; false otherwise.</returns>
560-
internal static bool ValidateMcpHeaders(HttpContext context, JsonRpcMessage message, McpServerPrimitiveCollection<McpServerTool>? toolCollection, out string? errorMessage)
561+
internal static bool ValidateMcpHeaders(HttpContext context, JsonRpcMessage message, McpServerPrimitiveCollection<McpServerTool>? toolCollection, [NotNullWhen(false)] out string? errorMessage)
561562
{
562563
// Only validate for protocol versions that support standard headers.
563564
var protocolVersion = context.Request.Headers[McpProtocolVersionHeaderName].ToString();
@@ -653,7 +654,7 @@ private static bool ValidateCustomParamHeaders(
653654
HttpContext context,
654655
JsonRpcMessage message,
655656
McpServerPrimitiveCollection<McpServerTool>? toolCollection,
656-
out string? errorMessage)
657+
[NotNullWhen(false)] out string? errorMessage)
657658
{
658659
// Custom param headers are only relevant for tools/call requests
659660
if (message is not JsonRpcRequest { Method: RequestMethods.ToolsCall, Params: { } bodyParams })

0 commit comments

Comments
 (0)