@@ -26,6 +26,7 @@ internal sealed class StreamableHttpHandler(
2626 ILoggerFactory loggerFactory ,
2727 IServiceProvider applicationServices )
2828{
29+ private const string McpSessionIdHeaderName = "Mcp-Session-Id" ;
2930 private static readonly JsonTypeInfo < JsonRpcError > s_errorTypeInfo = GetRequiredJsonTypeInfo < JsonRpcError > ( ) ;
3031
3132 public ConcurrentDictionary < string , HttpMcpSession < StreamableHttpServerTransport > > Sessions { get ; } = new ( StringComparer . Ordinal ) ;
@@ -70,8 +71,8 @@ await WriteJsonRpcErrorAsync(context,
7071 }
7172 finally
7273 {
73- // Stateless sessions are 1:1 with HTTP requests and are outlived by the MCP session tracked by the mcp-session-id .
74- // Non-stateless sessions are 1:1 with the mcp-session-id and outlive the POST request.
74+ // Stateless sessions are 1:1 with HTTP requests and are outlived by the MCP session tracked by the Mcp-Session-Id .
75+ // Non-stateless sessions are 1:1 with the Mcp-Session-Id and outlive the POST request.
7576 // Non-stateless sessions get disposed by a DELETE request or the IdleTrackingBackgroundService.
7677 if ( HttpServerTransportOptions . Stateless )
7778 {
@@ -90,7 +91,7 @@ await WriteJsonRpcErrorAsync(context,
9091 return ;
9192 }
9293
93- var sessionId = context . Request . Headers [ "mcp-session-id" ] . ToString ( ) ;
94+ var sessionId = context . Request . Headers [ McpSessionIdHeaderName ] . ToString ( ) ;
9495 var session = await GetSessionAsync ( context , sessionId ) ;
9596 if ( session is null )
9697 {
@@ -117,7 +118,7 @@ await WriteJsonRpcErrorAsync(context,
117118
118119 public async Task HandleDeleteRequestAsync ( HttpContext context )
119120 {
120- var sessionId = context . Request . Headers [ "mcp-session-id" ] . ToString ( ) ;
121+ var sessionId = context . Request . Headers [ McpSessionIdHeaderName ] . ToString ( ) ;
121122 if ( Sessions . TryRemove ( sessionId , out var session ) )
122123 {
123124 await session . DisposeAsync ( ) ;
@@ -157,14 +158,14 @@ await WriteJsonRpcErrorAsync(context,
157158 return null ;
158159 }
159160
160- context . Response . Headers [ "mcp-session-id" ] = session . Id ;
161+ context . Response . Headers [ McpSessionIdHeaderName ] = session . Id ;
161162 context . Features . Set ( session . Server ) ;
162163 return session ;
163164 }
164165
165166 private async ValueTask < HttpMcpSession < StreamableHttpServerTransport > ? > GetOrCreateSessionAsync ( HttpContext context )
166167 {
167- var sessionId = context . Request . Headers [ "mcp-session-id" ] . ToString ( ) ;
168+ var sessionId = context . Request . Headers [ McpSessionIdHeaderName ] . ToString ( ) ;
168169
169170 if ( string . IsNullOrEmpty ( sessionId ) )
170171 {
@@ -188,11 +189,11 @@ private async ValueTask<HttpMcpSession<StreamableHttpServerTransport>> StartNewS
188189 {
189190 SessionId = sessionId ,
190191 } ;
191- context . Response . Headers [ "mcp-session-id" ] = sessionId ;
192+ context . Response . Headers [ McpSessionIdHeaderName ] = sessionId ;
192193 }
193194 else
194195 {
195- // "(uninitialized stateless id)" is not written anywhere. We delay writing the mcp-session-id
196+ // "(uninitialized stateless id)" is not written anywhere. We delay writing the MCP-Session-Id
196197 // until after we receive the initialize request with the client info we need to serialize.
197198 sessionId = "(uninitialized stateless id)" ;
198199 transport = new ( )
@@ -204,7 +205,7 @@ private async ValueTask<HttpMcpSession<StreamableHttpServerTransport>> StartNewS
204205
205206 var session = await CreateSessionAsync ( context , transport , sessionId ) ;
206207
207- // The HttpMcpSession is not stored between requests in stateless mode. Instead, the session is recreated from the mcp-session-id .
208+ // The HttpMcpSession is not stored between requests in stateless mode. Instead, the session is recreated from the MCP-Session-Id .
208209 if ( ! HttpServerTransportOptions . Stateless )
209210 {
210211 if ( ! Sessions . TryAdd ( sessionId , session ) )
@@ -299,7 +300,7 @@ private void ScheduleStatelessSessionIdWrite(HttpContext context, StreamableHttp
299300
300301 var sessionJson = JsonSerializer . Serialize ( statelessId , StatelessSessionIdJsonContext . Default . StatelessSessionId ) ;
301302 transport . SessionId = Protector . Protect ( sessionJson ) ;
302- context . Response . Headers [ "mcp-session-id" ] = transport . SessionId ;
303+ context . Response . Headers [ McpSessionIdHeaderName ] = transport . SessionId ;
303304 return ValueTask . CompletedTask ;
304305 } ;
305306 }
0 commit comments