@@ -10,20 +10,17 @@ namespace ModelContextProtocol.AspNetCore.Authentication;
1010public class McpAuthenticationOptions : AuthenticationSchemeOptions
1111{
1212 private static readonly Uri DefaultResourceMetadataUri = new ( "/.well-known/oauth-protected-resource" , UriKind . Relative ) ;
13+ private Func < HttpContext , ProtectedResourceMetadata > ? _resourceMetadataProvider ;
14+ private ProtectedResourceMetadata _resourceMetadata ;
1315
1416 /// <summary>
1517 /// Initializes a new instance of the <see cref="McpAuthenticationOptions"/> class.
1618 /// </summary>
1719 public McpAuthenticationOptions ( )
1820 {
19- // Initialize the base property instead of hiding it with 'new'
2021 base . ForwardAuthenticate = "Bearer" ;
21-
22- // Initialize properties in constructor instead of using property initializers
2322 ResourceMetadataUri = DefaultResourceMetadataUri ;
24- ResourceMetadata = new ProtectedResourceMetadata ( ) ;
25-
26- // Initialize events
23+ _resourceMetadata = new ProtectedResourceMetadata ( ) ;
2724 Events = new McpAuthenticationEvents ( ) ;
2825 }
2926
@@ -50,9 +47,19 @@ public McpAuthenticationOptions()
5047 /// <remarks>
5148 /// This contains the OAuth metadata for the protected resource, including authorization servers,
5249 /// supported scopes, and other information needed for clients to authenticate.
53- /// This property is used when <see cref="ResourceMetadataProvider"/> is not set.
50+ /// Setting this property will automatically update the <see cref="ResourceMetadataProvider"/>
51+ /// to return this static instance.
5452 /// </remarks>
55- public ProtectedResourceMetadata ResourceMetadata { get ; set ; }
53+ public ProtectedResourceMetadata ResourceMetadata
54+ {
55+ get => _resourceMetadata ;
56+ set
57+ {
58+ _resourceMetadata = value ?? new ProtectedResourceMetadata ( ) ;
59+ // When static metadata is set, update the provider to use it
60+ _resourceMetadataProvider = _ => _resourceMetadata ;
61+ }
62+ }
5663
5764 /// <summary>
5865 /// Gets or sets a delegate that dynamically provides resource metadata based on the HTTP context.
@@ -62,7 +69,39 @@ public McpAuthenticationOptions()
6269 /// allowing dynamic customization based on the caller or other contextual information.
6370 /// This takes precedence over the static <see cref="ResourceMetadata"/> property.
6471 /// </remarks>
65- public Func < HttpContext , ProtectedResourceMetadata > ? ResourceMetadataProvider { get ; set ; }
72+ public Func < HttpContext , ProtectedResourceMetadata > ? ResourceMetadataProvider
73+ {
74+ get => _resourceMetadataProvider ;
75+ set => _resourceMetadataProvider = value ?? ( _ => _resourceMetadata ) ;
76+ }
77+
78+ /// <summary>
79+ /// Sets a static resource metadata instance that will be returned for all requests.
80+ /// </summary>
81+ /// <param name="metadata">The static resource metadata to use.</param>
82+ /// <returns>The current options instance for method chaining.</returns>
83+ /// <remarks>
84+ /// This is a convenience method equivalent to setting the <see cref="ResourceMetadata"/> property.
85+ /// </remarks>
86+ public McpAuthenticationOptions UseStaticResourceMetadata ( ProtectedResourceMetadata metadata )
87+ {
88+ ResourceMetadata = metadata ?? new ProtectedResourceMetadata ( ) ;
89+ return this ;
90+ }
91+
92+ /// <summary>
93+ /// Sets a delegate to dynamically provide resource metadata for each request.
94+ /// </summary>
95+ /// <param name="provider">A delegate that returns resource metadata for a given HTTP context.</param>
96+ /// <returns>The current options instance for method chaining.</returns>
97+ /// <remarks>
98+ /// This is a convenience method equivalent to setting the <see cref="ResourceMetadataProvider"/> property.
99+ /// </remarks>
100+ public McpAuthenticationOptions UseDynamicResourceMetadata ( Func < HttpContext , ProtectedResourceMetadata > provider )
101+ {
102+ ResourceMetadataProvider = provider ?? throw new ArgumentNullException ( nameof ( provider ) ) ;
103+ return this ;
104+ }
66105
67106 /// <summary>
68107 /// Gets the resource metadata for the current request.
@@ -71,11 +110,10 @@ public McpAuthenticationOptions()
71110 /// <returns>The resource metadata to use for the current request.</returns>
72111 internal ProtectedResourceMetadata GetResourceMetadata ( HttpContext context )
73112 {
74- if ( ResourceMetadataProvider != null )
75- {
76- return ResourceMetadataProvider ( context ) ;
77- }
78-
79- return ResourceMetadata ;
113+ var provider = _resourceMetadataProvider ;
114+
115+ return provider != null
116+ ? provider ( context )
117+ : _resourceMetadata ;
80118 }
81119}
0 commit comments