@@ -9,18 +9,21 @@ namespace ModelContextProtocol.AspNetCore.Authentication;
99/// </summary>
1010public class McpAuthenticationOptions : AuthenticationSchemeOptions
1111{
12- private static readonly Uri DefaultResourceMetadataUri = new ( "/.well-known/oauth-protected-resource" , UriKind . Relative ) ;
12+ private static readonly Uri DefaultResourceMetadataUri = new ( "/.well-known/oauth-protected-resource" , UriKind . Relative ) ;
13+
1314 private Func < HttpContext , ProtectedResourceMetadata > ? _resourceMetadataProvider ;
14- private ProtectedResourceMetadata _resourceMetadata ;
1515
16- /// <summary>
16+ private ProtectedResourceMetadata ? _resourceMetadata ; /// <summary>
1717 /// Initializes a new instance of the <see cref="McpAuthenticationOptions"/> class.
1818 /// </summary>
19+ /// <remarks>
20+ /// After creating an instance, you must set the ResourceMetadata property or use the
21+ /// UseStaticResourceMetadata method to provide a valid resource URI before using this options instance.
22+ /// </remarks>
1923 public McpAuthenticationOptions ( )
2024 {
2125 base . ForwardAuthenticate = "Bearer" ;
2226 ResourceMetadataUri = DefaultResourceMetadataUri ;
23- _resourceMetadata = new ProtectedResourceMetadata ( ) { Resource = new Uri ( "http://localhost" ) } ;
2427 Events = new McpAuthenticationEvents ( ) ;
2528 }
2629
@@ -39,8 +42,8 @@ public McpAuthenticationOptions()
3942 /// <remarks>
4043 /// This URI will be included in the WWW-Authenticate header when a 401 response is returned.
4144 /// </remarks>
42- public Uri ResourceMetadataUri { get ; set ; }
43-
45+ public Uri ResourceMetadataUri { get ; set ; }
46+
4447 /// <summary>
4548 /// Gets or sets the static protected resource metadata.
4649 /// </summary>
@@ -50,17 +53,26 @@ public McpAuthenticationOptions()
5053 /// Setting this property will automatically update the <see cref="ProtectedResourceMetadataProvider"/>
5154 /// to return this static instance.
5255 /// </remarks>
56+ /// <exception cref="ArgumentNullException">Thrown when trying to set a null value.</exception>
57+ /// <exception cref="ArgumentException">Thrown when the Resource property of the metadata is null.</exception>
5358 public ProtectedResourceMetadata ResourceMetadata
5459 {
55- get => _resourceMetadata ;
60+ get => _resourceMetadata ?? throw new InvalidOperationException (
61+ "ResourceMetadata has not been configured. Use ResourceMetadata property setter or UseStaticResourceMetadata method to provide a valid resource URI." ) ;
5662 set
5763 {
58- _resourceMetadata = value ?? new ProtectedResourceMetadata ( ) { Resource = new Uri ( "http://localhost" ) } ;
64+ ArgumentNullException . ThrowIfNull ( value ) ;
65+ if ( value . Resource == null )
66+ {
67+ throw new ArgumentException ( "The Resource property of the metadata cannot be null. A valid resource URI is required." , nameof ( value ) ) ;
68+ }
69+
70+ _resourceMetadata = value ;
5971 // When static metadata is set, update the provider to use it
6072 _resourceMetadataProvider = _ => _resourceMetadata ;
6173 }
62- }
63-
74+ }
75+
6476 /// <summary>
6577 /// Gets or sets a delegate that dynamically provides resource metadata based on the HTTP context.
6678 /// </summary>
@@ -72,9 +84,9 @@ public ProtectedResourceMetadata ResourceMetadata
7284 public Func < HttpContext , ProtectedResourceMetadata > ? ProtectedResourceMetadataProvider
7385 {
7486 get => _resourceMetadataProvider ;
75- set => _resourceMetadataProvider = value ?? ( _ => _resourceMetadata ) ;
76- }
77-
87+ set => _resourceMetadataProvider = value ;
88+ }
89+
7890 /// <summary>
7991 /// Sets a static resource metadata instance that will be returned for all requests.
8092 /// </summary>
@@ -83,9 +95,17 @@ public Func<HttpContext, ProtectedResourceMetadata>? ProtectedResourceMetadataPr
8395 /// <remarks>
8496 /// This is a convenience method equivalent to setting the <see cref="ResourceMetadata"/> property.
8597 /// </remarks>
98+ /// <exception cref="ArgumentNullException">Thrown when metadata is null.</exception>
99+ /// <exception cref="ArgumentException">Thrown when the Resource property of the metadata is null.</exception>
86100 public McpAuthenticationOptions UseStaticResourceMetadata ( ProtectedResourceMetadata metadata )
87101 {
88- ResourceMetadata = metadata ?? new ProtectedResourceMetadata ( ) { Resource = new Uri ( "http://localhost" ) } ;
102+ ArgumentNullException . ThrowIfNull ( metadata ) ;
103+ if ( metadata . Resource == null )
104+ {
105+ throw new ArgumentException ( "The Resource property of the metadata cannot be null. A valid resource URI is required." , nameof ( metadata ) ) ;
106+ }
107+
108+ ResourceMetadata = metadata ;
89109 return this ;
90110 }
91111
@@ -101,19 +121,21 @@ public McpAuthenticationOptions UseDynamicResourceMetadata(Func<HttpContext, Pro
101121 {
102122 ProtectedResourceMetadataProvider = provider ?? throw new ArgumentNullException ( nameof ( provider ) ) ;
103123 return this ;
104- }
105-
124+ }
125+
106126 /// <summary>
107127 /// Gets the resource metadata for the current request.
108128 /// </summary>
109129 /// <param name="context">The HTTP context for the current request.</param>
110130 /// <returns>The resource metadata to use for the current request.</returns>
131+ /// <exception cref="InvalidOperationException">Thrown when no resource metadata has been configured.</exception>
111132 internal ProtectedResourceMetadata GetResourceMetadata ( HttpContext context )
112133 {
113134 var provider = _resourceMetadataProvider ;
114135
115136 return provider != null
116137 ? provider ( context )
117- : _resourceMetadata ;
138+ : _resourceMetadata ?? throw new InvalidOperationException (
139+ "ResourceMetadata has not been configured. Use ResourceMetadata property setter or UseStaticResourceMetadata method to provide a valid resource URI." ) ;
118140 }
119141}
0 commit comments