test(aspnetcore): cover RequireAuthorization() on the MCP endpoint across session modes - #1870
Open
ump45nose wants to merge 1 commit into
Open
test(aspnetcore): cover RequireAuthorization() on the MCP endpoint across session modes#1870ump45nose wants to merge 1 commit into
ump45nose wants to merge 1 commit into
Conversation
…ross session modes
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
Adds end-to-end coverage for protecting the MCP endpoint itself with
RequireAuthorization().AuthorizeAttributeTestsalready covers tool-level[Authorize]: the request reaches the MCP handler and the tool collection is filtered per user. The endpoint-level challenge has different requirements and had no coverage — it must happen before the handler runs, it must hold for every request in an established session, and it must work in each session mode without making the OAuth protected-resource metadata document unreachable.New file:
tests/ModelContextProtocol.AspNetCore.Tests/MapMcpAuthorizationTests.csInitialize_WithoutCredentials_IsChallenged— the initialinitializePOST is rejected with 401 inStateful,StatefulForInitializeClients, andStateless.Initialize_WithCredentials_Succeeds— 200, and a session id is minted in the session-based modes but not inStateless(which keeps the endpoint free of server state).StatefulSession_FollowUpRequests_RequireCredentials— an authorizedinitializefollowed by an authorizedtools/liston the same session works, while the sameMcp-Session-Idsent without credentials is rejected with 401. The session id identifies a session; it must not authenticate the caller.ResourceMetadata_RemainsReachable_WhileTheMcpEndpointIsChallenged— withMapMcp("/mcp").RequireAuthorization()and the SDK'sMcpAuthenticationHandlerconfigured,GET /.well-known/oauth-protected-resource/mcpis served without credentials, and the 401 challenge advertisesresource_metadata="…/.well-known/oauth-protected-resource/mcp"so a client can still discover the authorization server from the challenge alone. The endpoint is mounted at/mcphere because that is the deployment shape where the metadata document mirrors the resource path.The authentication scheme is a test-only header-based handler, so the challenged and authorized paths are both exercised without standing up a token issuer.
No product code changes.
Verification
dotnet build tests/ModelContextProtocol.AspNetCore.Testssucceeds fornet10.0,net9.0, andnet8.0with 0 warnings.dotnet test -c Release -f net10.0(SDK 10.0.101) on this branch:ModelContextProtocol.AspNetCore.Tests: 602 passed, 55 skipped, 0 failedModelContextProtocol.Tests: 2359 passed, 5 skipped, 0 failedMapMcpAuthorizationTestson its own: 8 passed, 0 failedOnly the .NET 10 runtime is installed on my machine, so the
net8.0/net9.0legs are compile-verified rather than executed here.ModelContextProtocol.Analyzers.Teststargetsnet9.0only and is unaffected by this change.Built and tested locally with
-p:NuGetAudit=false:Microsoft.Build.Tasks.Git8.0.0 raises NU1902 on this repository today, which reproduces on unmodifiedmainand is unrelated to this change.Related to #1785, where endpoint-level
RequireAuthorization()combined with session mode was the configuration under investigation.