Skip to content

Commit c04f198

Browse files
halter73Copilot
andcommitted
Add sessions doc and prefer stateless mode in docs, samples, and error messages
Recommend stateless mode as the default for HTTP-based MCP servers across documentation, samples, and error messages. Docs: - Add comprehensive sessions conceptual doc covering stateless (recommended), stateful, and stdio session behaviors - Update getting-started, transports, filters, and other conceptual docs to use stateless mode in examples - Add Sampling to docs table of contents - Clarify ConfigureSessionOptions runs per-request in stateless mode Samples: - Convert ProtectedMcpServer to stateless mode - Add comments to AspNetCoreMcpServer and EverythingServer explaining why they require sessions Error messages: - Improve missing Mcp-Session-Id errors to suggest stateless mode and link to session documentation Tests: - Add tests for progress notifications and ConfigureSessionOptions in stateless mode - Verify error messages reference stateless mode guidance Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 51a4fde commit c04f198

17 files changed

Lines changed: 515 additions & 11 deletions

File tree

docs/concepts/elicitation/elicitation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Here's an example implementation of how a console application might handle elici
172172

173173
### URL Elicitation Required Error
174174

175-
When a tool cannot proceed without first completing a URL-mode elicitation (for example, when third-party OAuth authorization is needed), and calling `ElicitAsync` is not practical (for example in <xref: ModelContextProtocol.AspNetCore.HttpServerTransportOptions.Stateless> is enabled disabling server-to-client requests), the server may throw a <xref:ModelContextProtocol.UrlElicitationRequiredException>. This is a specialized error (JSON-RPC error code `-32042`) that signals to the client that one or more URL-mode elicitations must be completed before the original request can be retried.
175+
When a tool cannot proceed without first completing a URL-mode elicitation (for example, when third-party OAuth authorization is needed), and calling `ElicitAsync` is not practical (for example in [stateless](sessions/sessions.md) mode where server-to-client requests are disabled), the server may throw a <xref:ModelContextProtocol.UrlElicitationRequiredException>. This is a specialized error (JSON-RPC error code `-32042`) that signals to the client that one or more URL-mode elicitations must be completed before the original request can be retried.
176176

177177
#### Throwing UrlElicitationRequiredException on the Server
178178

docs/concepts/filters.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,10 @@ builder.Services.AddAuthentication("Bearer")
544544
builder.Services.AddAuthorization();
545545

546546
builder.Services.AddMcpServer()
547-
.WithHttpTransport()
547+
.WithHttpTransport(options =>
548+
{
549+
options.Stateless = true;
550+
})
548551
.AddAuthorizationFilters() // Required for authorization support
549552
.WithTools<WeatherTools>()
550553
.WithRequestFilters(requestFilters =>

docs/concepts/getting-started.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ using System.ComponentModel;
7979

8080
var builder = WebApplication.CreateBuilder(args);
8181
builder.Services.AddMcpServer()
82-
.WithHttpTransport()
82+
.WithHttpTransport(options =>
83+
{
84+
// Stateless mode is recommended for servers that don't need
85+
// server-to-client requests like sampling or elicitation.
86+
// See the Sessions documentation for details.
87+
options.Stateless = true;
88+
})
8389
.WithToolsFromAssembly();
8490
var app = builder.Build();
8591

docs/concepts/httpcontext/samples/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
// Add services to the container.
66

77
builder.Services.AddMcpServer()
8-
.WithHttpTransport()
8+
.WithHttpTransport(options =>
9+
{
10+
options.Stateless = true;
11+
})
912
.WithTools<ContextTools>();
1013

1114
// <snippet_AddHttpContextAccessor>

docs/concepts/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ Install the SDK and build your first MCP client and server.
3636
| [Prompts](prompts/prompts.md) | Learn how to implement and consume reusable prompt templates with rich content types. |
3737
| [Completions](completions/completions.md) | Learn how to implement argument auto-completion for prompts and resource templates. |
3838
| [Logging](logging/logging.md) | Learn how to implement logging in MCP servers and how clients can consume log messages. |
39+
| [Sessions](sessions/sessions.md) | Learn when to use stateless vs. stateful mode for HTTP servers and how to configure sessions. |
3940
| [HTTP Context](httpcontext/httpcontext.md) | Learn how to access the underlying `HttpContext` for a request. |
4041
| [MCP Server Handler Filters](filters.md) | Learn how to add filters to the handler pipeline. Filters let you wrap the original handler with additional functionality. |

docs/concepts/logging/logging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ MCP servers that implement the Logging utility must declare this in the capabili
4646
[Initialization]: https://modelcontextprotocol.io/specification/2025-11-25/basic/lifecycle#initialization
4747

4848
Servers built with the C# SDK always declare the logging capability. Doing so does not obligate the server
49-
to send log messages&mdash;only allows it. Note that stateless MCP servers might not be capable of sending log
49+
to send log messages&mdash;only allows it. Note that [stateless](sessions/sessions.md) MCP servers might not be capable of sending log
5050
messages as there might not be an open connection to the client on which the log messages could be sent.
5151

5252
The C# SDK provides an extension method <xref:Microsoft.Extensions.DependencyInjection.McpServerBuilderExtensions.WithSetLoggingLevelHandler*> on <xref:Microsoft.Extensions.DependencyInjection.IMcpServerBuilder> to allow the

docs/concepts/progress/samples/server/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
// Add services to the container.
66

77
builder.Services.AddMcpServer()
8-
.WithHttpTransport()
8+
.WithHttpTransport(options =>
9+
{
10+
options.Stateless = true;
11+
})
912
.WithTools<LongRunningTools>();
1013

1114
builder.Logging.AddConsole(options =>

0 commit comments

Comments
 (0)