Skip to content

IDataProtectionProvider default implementation prevents use of stateless with a load balancerΒ #484

@johnthcall

Description

@johnthcall

Describe the bug
Because the StatelessSessionId is protected via IDataProtectionProvider when a second instance of the server receives the subsequent request it tries to unprotect the sessionId header which throws System.Security.Cryptography.CryptographicException: The key {59baa26c-5757-494e-bc99-c7a2d3331425} was not found in the key ring. For more information go to https://aka.ms/aspnet/dataprotectionwarning
When running from a client via McpClientFactory.CreateAsync this does not reproduce as the connection is reused and the same instance is hit. When integrating with VS Codes Copilot the tools/list call appears to be establishing another request and as a result failing.

To Reproduce
Steps to reproduce the behavior:

  1. After initializing to the server and getting a sessionId back.
  2. either reboot the server or have the subsequent request use a new connection.
  3. Make a subsequent request passing the sessionId header.

Expected behavior
Stateless either does not depend on this stateful data protection or there is documentation for how to implement the IDataProtectionProvider so that cross instance requests can be processed.

Logs
StackTrace:
Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte[] protectedData)
at Microsoft.AspNetCore.DataProtection.DataProtectionCommonExtensions.Unprotect(IDataProtector protector, String protectedData)
at ModelContextProtocol.AspNetCore.StreamableHttpHandler.GetSessionAsync(HttpContext context, String sessionId)
at ModelContextProtocol.AspNetCore.StreamableHttpHandler.GetOrCreateSessionAsync(HttpContext context)
at ModelContextProtocol.AspNetCore.StreamableHttpHandler.HandlePostRequestAsync(HttpContext context)

Additional context
Running 0.2.0-preview.3 on windows so under the hood CngGcmAuthenticatedEncryptorConfiguration is used.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions