fix(aspnetcore): explain why a stateful session lookup failed - #1876
Open
ump45nose wants to merge 1 commit into
Open
fix(aspnetcore): explain why a stateful session lookup failed#1876ump45nose wants to merge 1 commit into
ump45nose wants to merge 1 commit into
Conversation
A request for an Mcp-Session-Id this instance does not know is answered with `404 Session not found` and nothing else. That is what an operator sees when a stateful server runs more than one instance without session affinity, or after a restart drops in-memory sessions, and the bare message made a deployment misconfiguration look like a client bug. Report the likely cause and both remedies (session affinity, or stateless mode) in the error, matching the guidance the sibling session errors already carry, and cover the unknown-session path with a test that replays the load-balanced case. Also state the affinity requirement in the sentence that tells readers to configure SessionMode, not only in the stateless guide. The -32001 code, the 404 status, and the "Session not found" prefix are unchanged. Related to modelcontextprotocol#1861
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Related to #1861. A stateful server answers a request for a session it does not have with
404and the bare messageSession not found. That is exactly what a multi-instance deployment without session affinity produces — or a restart that drops in-memory sessions — and the report shows the result: intermittent connection failures with nothing in the response pointing at load balancing or at stateful mode.Three changes:
1. The 404 explains itself. The message now names the likely cause and both remedies, in the same shape as the sibling session errors, which already point at stateless mode and the docs:
The
-32001code, the404status, and theSession not foundprefix are unchanged, so clients that match on either keep working. Legacy Streamable HTTP clients and raw HTTP tooling (the Postman MCP client and the inspector used in the report) see this text directly.2. The unknown-session path is now covered.
Stateful_UnknownSessionId_Returns404WithSessionAffinityGuidancereplays the load-balanced case against a real ASP.NET Core server: noinitializeis performed, the request carries a session id minted by "another instance", and the test asserts the status, the-32001code and the guidance. Nothing in the repository referenced-32001or this message before, so the path was entirely uncovered.3. The affinity requirement is visible where
SessionModeis configured.transports.mdalready told readers to setStatefulexplicitly, and its comparison table already said "Requires session affinity", but the consequence — and the reason a multi-instance deployment fails intermittently — only appeared in the stateless guide. The sentence that tells readers to configure stateful mode now carries it too.Not changed: stateful sessions still require session affinity. This PR does not add cross-instance session storage, and it does not change session semantics — it makes an existing failure mode diagnosable. If you would rather keep the wire message minimal and put this in a server log instead, I'm happy to move it.
Verification
Assert.Contains("session affinity", message), and passes after. The 404 and-32001assertions passed both before and after, which is how I confirmed the scenario reaches the intended path rather than a protocol-version rejection.dotnet test tests/ModelContextProtocol.AspNetCore.Tests -f net10.0→ 621 passed, 0 failed, 46 skipped.--filter "FullyQualifiedName~Stateful_UnknownSessionId|FullyQualifiedName~Client_CanReconnect_AfterSessionExpiry"→ 2 passed, 2 skipped (the skipped pair are the stateless-mode variants, which have no sessions).Related to #1861.