Skip to content

Commit 0bcfe73

Browse files
Copilotstephentoub
andcommitted
Address PR feedback: move SupportedProtocolVersions to ProtocolVersions class, use nullable out param
- Move SupportedProtocolVersions from McpSession to new ProtocolVersions static class in Protocol namespace (following RequestMethods/NotificationMethods pattern) - Revert McpSession.cs to original state - Change out string to out string? with null on success - Add ProtocolVersionsTests for the new public property Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent f9e3396 commit 0bcfe73

4 files changed

Lines changed: 44 additions & 11 deletions

File tree

src/ModelContextProtocol.AspNetCore/StreamableHttpHandler.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task HandlePostRequestAsync(HttpContext context)
3535
{
3636
if (!ValidateProtocolVersionHeader(context, out var errorMessage))
3737
{
38-
await WriteJsonRpcErrorAsync(context, errorMessage, StatusCodes.Status400BadRequest);
38+
await WriteJsonRpcErrorAsync(context, errorMessage!, StatusCodes.Status400BadRequest);
3939
return;
4040
}
4141

@@ -83,7 +83,7 @@ public async Task HandleGetRequestAsync(HttpContext context)
8383
{
8484
if (!ValidateProtocolVersionHeader(context, out var errorMessage))
8585
{
86-
await WriteJsonRpcErrorAsync(context, errorMessage, StatusCodes.Status400BadRequest);
86+
await WriteJsonRpcErrorAsync(context, errorMessage!, StatusCodes.Status400BadRequest);
8787
return;
8888
}
8989

@@ -186,7 +186,7 @@ public async Task HandleDeleteRequestAsync(HttpContext context)
186186
{
187187
if (!ValidateProtocolVersionHeader(context, out var errorMessage))
188188
{
189-
await WriteJsonRpcErrorAsync(context, errorMessage, StatusCodes.Status400BadRequest);
189+
await WriteJsonRpcErrorAsync(context, errorMessage!, StatusCodes.Status400BadRequest);
190190
return;
191191
}
192192

@@ -413,17 +413,17 @@ internal static Task RunSessionAsync(HttpContext httpContext, McpServer session,
413413
/// Validates the MCP-Protocol-Version header if present. A missing header is allowed for backwards compatibility,
414414
/// but an invalid or unsupported value must be rejected with 400 Bad Request per the MCP spec.
415415
/// </summary>
416-
private static bool ValidateProtocolVersionHeader(HttpContext context, out string errorMessage)
416+
private static bool ValidateProtocolVersionHeader(HttpContext context, out string? errorMessage)
417417
{
418418
var protocolVersionHeader = context.Request.Headers[McpProtocolVersionHeaderName].ToString();
419419
if (!string.IsNullOrEmpty(protocolVersionHeader) &&
420-
!McpSession.SupportedProtocolVersions.Contains(protocolVersionHeader))
420+
!ProtocolVersions.SupportedVersions.Contains(protocolVersionHeader))
421421
{
422422
errorMessage = $"Bad Request: The MCP-Protocol-Version header value '{protocolVersionHeader}' is not supported.";
423423
return false;
424424
}
425425

426-
errorMessage = "";
426+
errorMessage = null;
427427
return true;
428428
}
429429

src/ModelContextProtocol.Core/McpSession.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ namespace ModelContextProtocol;
2828
/// </remarks>
2929
public abstract partial class McpSession : IAsyncDisposable
3030
{
31-
/// <summary>
32-
/// Gets the protocol versions supported by this implementation of MCP.
33-
/// </summary>
34-
public static IReadOnlyList<string> SupportedProtocolVersions { get; } = McpSessionHandler.SupportedProtocolVersions;
35-
3631
/// <summary>Gets an identifier associated with the current MCP session.</summary>
3732
/// <remarks>
3833
/// Typically populated in transports supporting multiple sessions, such as Streamable HTTP or SSE.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace ModelContextProtocol.Protocol;
2+
3+
/// <summary>
4+
/// Provides the protocol versions supported by this implementation of MCP.
5+
/// </summary>
6+
public static class ProtocolVersions
7+
{
8+
/// <summary>
9+
/// Gets the protocol versions supported by this implementation of MCP.
10+
/// </summary>
11+
public static IReadOnlyList<string> SupportedVersions { get; } = McpSessionHandler.SupportedProtocolVersions;
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using ModelContextProtocol.Protocol;
2+
3+
namespace ModelContextProtocol.Tests.Protocol;
4+
5+
public class ProtocolVersionsTests
6+
{
7+
[Fact]
8+
public void SupportedVersions_IsNotEmpty()
9+
{
10+
Assert.NotEmpty(ProtocolVersions.SupportedVersions);
11+
}
12+
13+
[Fact]
14+
public void SupportedVersions_ContainsExpectedVersions()
15+
{
16+
Assert.Contains("2024-11-05", ProtocolVersions.SupportedVersions);
17+
Assert.Contains("2025-03-26", ProtocolVersions.SupportedVersions);
18+
Assert.Contains("2025-06-18", ProtocolVersions.SupportedVersions);
19+
}
20+
21+
[Fact]
22+
public void SupportedVersions_ReturnsSameInstance()
23+
{
24+
Assert.Same(ProtocolVersions.SupportedVersions, ProtocolVersions.SupportedVersions);
25+
}
26+
}

0 commit comments

Comments
 (0)