Skip to content

Commit 8da9ccd

Browse files
Copilothalter73
andcommitted
Make RunSessionHandler experimental (MCPEXP003)
Co-authored-by: halter73 <54385+halter73@users.noreply.github.com>
1 parent eb049ed commit 8da9ccd

File tree

12 files changed

+50
-0
lines changed

12 files changed

+50
-0
lines changed

docs/list-of-diagnostics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ If you use experimental APIs, you will get one of the diagnostics shown below. T
2525
| :------------ | :---------- |
2626
| `MCPEXP001` | MCP experimental APIs including Tasks and Extensions. Tasks provide a mechanism for asynchronous long-running operations that can be polled for status and results (see [MCP Tasks specification](https://modelcontextprotocol.io/specification/draft/basic/utilities/tasks)). Extensions provide a framework for extending the Model Context Protocol while maintaining interoperability (see [SEP-2133](https://github.com/modelcontextprotocol/modelcontextprotocol/pull/2133)). |
2727
| `MCPEXP002` | Subclassing `McpClient` and `McpServer` is experimental and subject to change (see [#1363](https://github.com/modelcontextprotocol/csharp-sdk/pull/1363)). |
28+
| `MCPEXP003` | `RunSessionHandler` is experimental and may be removed or change signatures in a future release. Consider using `ConfigureSessionOptions` instead, which provides access to the `HttpContext` of the initializing request with fewer known issues. |
2829

2930
## Obsolete APIs
3031

samples/EverythingServer/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
.WithHttpTransport(options =>
5252
{
5353
// Add a RunSessionHandler to remove all subscriptions for the session when it ends
54+
#pragma warning disable MCPEXP003 // RunSessionHandler is experimental
5455
options.RunSessionHandler = async (httpContext, mcpServer, token) =>
5556
{
5657
if (mcpServer.SessionId == null)
@@ -76,6 +77,7 @@
7677
subscriptions.TryRemove(mcpServer.SessionId, out _);
7778
}
7879
};
80+
#pragma warning restore MCPEXP003
7981
})
8082
.WithTools<AddTool>()
8183
.WithTools<AnnotatedMessageTool>()

src/Common/Experimentals.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,19 @@ internal static class Experimentals
7171
/// URL for experimental subclassing of McpClient and McpServer.
7272
/// </summary>
7373
public const string Subclassing_Url = "https://github.com/modelcontextprotocol/csharp-sdk/pull/1363";
74+
75+
/// <summary>
76+
/// Diagnostic ID for the experimental <c>RunSessionHandler</c> API.
77+
/// </summary>
78+
public const string RunSessionHandler_DiagnosticId = "MCPEXP003";
79+
80+
/// <summary>
81+
/// Message for the experimental <c>RunSessionHandler</c> API.
82+
/// </summary>
83+
public const string RunSessionHandler_Message = "RunSessionHandler is experimental and may be removed or changed in a future release. Consider using ConfigureSessionOptions instead.";
84+
85+
/// <summary>
86+
/// URL for the experimental <c>RunSessionHandler</c> API.
87+
/// </summary>
88+
public const string RunSessionHandler_Url = "https://github.com/modelcontextprotocol/csharp-sdk/blob/main/docs/list-of-diagnostics.md#mcpexp003";
7489
}

src/ModelContextProtocol.AspNetCore/HttpServerTransportOptions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ public class HttpServerTransportOptions
2424
/// </summary>
2525
/// <remarks>
2626
/// This callback is useful for running logic before a session starts and after it completes.
27+
/// <para>
28+
/// Consider using <see cref="ConfigureSessionOptions"/> instead, which provides access to the
29+
/// <see cref="HttpContext"/> of the initializing request with fewer known issues.
30+
/// </para>
31+
/// <para>
32+
/// This API is experimental and may be removed or change signatures in a future release.
33+
/// </para>
2734
/// </remarks>
35+
[System.Diagnostics.CodeAnalysis.Experimental(Experimentals.RunSessionHandler_DiagnosticId, UrlFormat = Experimentals.RunSessionHandler_Url)]
2836
public Func<HttpContext, McpServer, CancellationToken, Task>? RunSessionHandler { get; set; }
2937

3038
/// <summary>

src/ModelContextProtocol.AspNetCore/ModelContextProtocol.AspNetCore.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
<None Include="README.md" pack="true" PackagePath="\" />
2121
</ItemGroup>
2222

23+
<ItemGroup>
24+
<Compile Include="..\Common\Experimentals.cs" Link="Experimentals.cs" />
25+
</ItemGroup>
26+
2327
<ItemGroup>
2428
<ProjectReference Include="..\ModelContextProtocol\ModelContextProtocol.csproj" />
2529
</ItemGroup>

src/ModelContextProtocol.AspNetCore/SseHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public async Task HandleSseRequestAsync(HttpContext context)
5656
await using var mcpServer = McpServer.Create(transport, mcpServerOptions, loggerFactory, context.RequestServices);
5757
context.Features.Set(mcpServer);
5858

59+
#pragma warning disable MCPEXP003 // RunSessionHandler is experimental
5960
var runSessionAsync = httpMcpServerOptions.Value.RunSessionHandler ?? StreamableHttpHandler.RunSessionAsync;
61+
#pragma warning restore MCPEXP003
6062
await runSessionAsync(context, mcpServer, cancellationToken);
6163
}
6264
finally

src/ModelContextProtocol.AspNetCore/StreamableHttpHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,9 @@ private async ValueTask<StreamableHttpSession> CreateSessionAsync(
390390
var userIdClaim = GetUserIdClaim(context.User);
391391
var session = new StreamableHttpSession(sessionId, transport, server, userIdClaim, sessionManager);
392392

393+
#pragma warning disable MCPEXP003 // RunSessionHandler is experimental
393394
var runSessionAsync = HttpServerTransportOptions.RunSessionHandler ?? RunSessionAsync;
395+
#pragma warning restore MCPEXP003
394396
session.ServerRunTask = runSessionAsync(context, server, session.SessionClosed);
395397

396398
return session;

tests/ModelContextProtocol.AspNetCore.Tests/MapMcpStreamableHttpTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,14 @@ public async Task CanResumeSessionWithMapMcpAndRunSessionHandler()
213213
}).WithHttpTransport(opts =>
214214
{
215215
ConfigureStateless(opts);
216+
#pragma warning disable MCPEXP003 // RunSessionHandler is experimental
216217
opts.RunSessionHandler = async (context, server, cancellationToken) =>
217218
{
218219
Interlocked.Increment(ref runSessionCount);
219220
serverTcs.TrySetResult(server);
220221
await server.RunAsync(cancellationToken);
221222
};
223+
#pragma warning restore MCPEXP003
222224
}).WithTools<EchoHttpContextUserTools>();
223225

224226
await using var app = Builder.Build();
@@ -481,11 +483,13 @@ public async Task DisposeAsync_DoesNotHang_WhenOwnsSessionIsFalse_WithUnsolicite
481483
Builder.Services.AddMcpServer().WithHttpTransport(opts =>
482484
{
483485
ConfigureStateless(opts);
486+
#pragma warning disable MCPEXP003 // RunSessionHandler is experimental
484487
opts.RunSessionHandler = async (context, server, cancellationToken) =>
485488
{
486489
serverTcs.TrySetResult(server);
487490
await server.RunAsync(cancellationToken);
488491
};
492+
#pragma warning restore MCPEXP003
489493
}).WithTools<ClaimsPrincipalTools>();
490494

491495
await using var app = Builder.Build();

tests/ModelContextProtocol.AspNetCore.Tests/ResumabilityIntegrationTestsBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,13 @@ public virtual async Task Client_CanResumeUnsolicitedMessageStream_AfterDisconne
253253

254254
await using var app = await CreateServerAsync(configureTransport: options =>
255255
{
256+
#pragma warning disable MCPEXP003 // RunSessionHandler is experimental
256257
options.RunSessionHandler = (httpContext, mcpServer, cancellationToken) =>
257258
{
258259
serverTcs.TrySetResult(mcpServer);
259260
return mcpServer.RunAsync(cancellationToken);
260261
};
262+
#pragma warning restore MCPEXP003
261263
});
262264

263265
await using var client = await ConnectClientAsync();

tests/ModelContextProtocol.AspNetCore.Tests/SseIntegrationTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public async Task ConnectAndReceiveNotification_InMemoryServer()
8383
Builder.Services.AddMcpServer()
8484
.WithHttpTransport(httpTransportOptions =>
8585
{
86+
#pragma warning disable MCPEXP003 // RunSessionHandler is experimental
8687
httpTransportOptions.RunSessionHandler = (httpContext, mcpServer, cancellationToken) =>
8788
{
8889
// We could also use ServerCapabilities.NotificationHandlers, but it's good to have some test coverage of RunSessionHandler.
@@ -93,6 +94,7 @@ public async Task ConnectAndReceiveNotification_InMemoryServer()
9394
});
9495
return mcpServer.RunAsync(cancellationToken);
9596
};
97+
#pragma warning restore MCPEXP003
9698
});
9799

98100
await using var app = Builder.Build();

0 commit comments

Comments
 (0)