@@ -16,6 +16,7 @@ namespace ModelContextProtocol.AspNetCore.Authentication;
1616public class McpAuthenticationHandler : AuthenticationHandler < McpAuthenticationOptions > , IAuthenticationRequestHandler
1717{
1818 private readonly IOptionsMonitor < McpAuthenticationOptions > _optionsMonitor ;
19+ private string _resourceMetadataPath ;
1920
2021 /// <summary>
2122 /// Initializes a new instance of the <see cref="McpAuthenticationHandler"/> class.
@@ -27,18 +28,17 @@ public McpAuthenticationHandler(
2728 : base ( options , logger , encoder )
2829 {
2930 _optionsMonitor = options ;
31+ _resourceMetadataPath = options . CurrentValue . ResourceMetadataUri . ToString ( ) ;
3032 }
3133
3234 /// <inheritdoc />
3335 public async Task < bool > HandleRequestAsync ( )
3436 {
3537 // Check if the request is for the resource metadata endpoint
3638 string requestPath = Request . Path . Value ?? string . Empty ;
37- var options = _optionsMonitor . CurrentValue ;
38- string resourceMetadataPath = options . ResourceMetadataUri . ToString ( ) ;
3939
4040 // If the path doesn't match, let the request continue through the pipeline
41- if ( ! string . Equals ( requestPath , resourceMetadataPath , StringComparison . OrdinalIgnoreCase ) )
41+ if ( ! string . Equals ( requestPath , _resourceMetadataPath , StringComparison . OrdinalIgnoreCase ) )
4242 {
4343 return false ;
4444 }
@@ -61,16 +61,22 @@ private string GetAbsoluteResourceMetadataUri()
6161 var options = _optionsMonitor . CurrentValue ;
6262 var resourceMetadataUri = options . ResourceMetadataUri ;
6363
64+ // If the options have changed, update the cached path
65+ string currentPath = resourceMetadataUri . ToString ( ) ;
66+ if ( _resourceMetadataPath != currentPath )
67+ {
68+ _resourceMetadataPath = currentPath ;
69+ }
70+
6471 if ( resourceMetadataUri . IsAbsoluteUri )
6572 {
6673 return resourceMetadataUri . ToString ( ) ;
6774 }
6875
6976 // For relative URIs, combine with the base URL
7077 string baseUrl = GetBaseUrl ( ) ;
71- string resourceMetadataPath = resourceMetadataUri . ToString ( ) ;
7278
73- if ( ! Uri . TryCreate ( baseUrl + resourceMetadataPath , UriKind . Absolute , out var absoluteUri ) )
79+ if ( ! Uri . TryCreate ( baseUrl + _resourceMetadataPath , UriKind . Absolute , out var absoluteUri ) )
7480 {
7581 throw new InvalidOperationException ( "Could not create absolute URI for resource metadata." ) ;
7682 }
0 commit comments