File tree Expand file tree Collapse file tree 3 files changed +8
-5
lines changed
samples/ProtectedMCPServer
src/ModelContextProtocol.AspNetCore/Auth Expand file tree Collapse file tree 3 files changed +8
-5
lines changed Original file line number Diff line number Diff line change 1515} )
1616. AddScheme < AuthenticationSchemeOptions , SimpleAuthHandler > ( "Bearer" , options => { } )
1717. AddMcp ( options => {
18- // Configure MCP authentication options with the resource metadata URI
18+ // Ensure ResourceMetadataUri matches the actual mapping in McpEndpointRouteBuilderExtensions.cs
1919 options . ResourceMetadataUri = new Uri ( "/.well-known/oauth-protected-resource" , UriKind . Relative ) ;
2020
2121 // Configure the resource metadata using our enhanced options
3838// Don't forget to register the ResourceMetadataService
3939builder . Services . AddSingleton < ResourceMetadataService > ( ) ;
4040
41- // IMPORTANT: Register the McpAuthorizationMarker to enable authorization on MCP endpoints
42- builder . Services . AddSingleton < McpAuthorizationMarker > ( ) ;
43-
4441// Configure MCP Server
4542builder . Services . AddMcpServer ( options =>
4643{
107104app . UseAuthorization ( ) ;
108105
109106// Map MCP endpoints with authorization
107+ // Note: The SDK will automatically map /.well-known/oauth-protected-resource
108+ // and make it accessible without authorization
110109app . MapMcp ( ) ;
111110
112111Console . WriteLine ( "Starting MCP server with authorization at http://localhost:7071" ) ;
Original file line number Diff line number Diff line change @@ -41,6 +41,10 @@ public static AuthenticationBuilder AddMcp(
4141 Action < McpAuthenticationOptions > ? configureOptions = null )
4242 {
4343 builder . Services . TryAddSingleton < ResourceMetadataService > ( ) ;
44+
45+ // Register the marker to indicate that MCP authorization is configured
46+ // This will be used by MapMcp to apply authorization to endpoints
47+ builder . Services . TryAddSingleton < McpAuthorizationMarker > ( ) ;
4448
4549 return builder . AddScheme < McpAuthenticationOptions , McpAuthenticationHandler > (
4650 authenticationScheme ,
Original file line number Diff line number Diff line change @@ -3,4 +3,4 @@ namespace ModelContextProtocol.AspNetCore.Auth;
33/// <summary>
44/// Marker class to indicate that MCP authorization has been configured.
55/// </summary>
6- internal class McpAuthorizationMarker { }
6+ public class McpAuthorizationMarker { }
You can’t perform that action at this time.
0 commit comments