Skip to content

Commit 4cff084

Browse files
committed
Minor changes to PRM doc logic
1 parent 8790a35 commit 4cff084

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

src/ModelContextProtocol.AspNetCore/Auth/McpAuthenticationHandler.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,28 @@ protected override Task HandleChallengeAsync(AuthenticationProperties properties
3838

3939
// Generate the full resource metadata URL based on the current request
4040
var baseUrl = $"{Request.Scheme}://{Request.Host}";
41-
var metadataPath = Options.ResourceMetadataUri.ToString();
42-
var metadataUrl = metadataPath.StartsWith("http", StringComparison.OrdinalIgnoreCase)
43-
? metadataPath
44-
: $"{baseUrl}{metadataPath}";
41+
42+
// Properly parse and validate the ResourceMetadataUri
43+
if (!Uri.TryCreate(Options.ResourceMetadataUri.ToString(), UriKind.Absolute, out var prmDocumentUri))
44+
throw new InvalidOperationException("Invalid ResourceMetadataUri in options.");
45+
46+
// Verify that the URI scheme starts with "http"
47+
if (!prmDocumentUri.Scheme.StartsWith("http", StringComparison.OrdinalIgnoreCase))
48+
throw new InvalidOperationException("ResourceMetadataUri must use HTTP or HTTPS scheme.");
49+
50+
var rawPrmDocumentUri = prmDocumentUri.ToString();
4551

4652
// Initialize properties if null
4753
properties ??= new AuthenticationProperties();
4854

4955
// Set the WWW-Authenticate header with the resource_metadata
5056
string headerValue = $"Bearer realm=\"{Scheme.Name}\"";
51-
headerValue += $", resource_metadata=\"{metadataUrl}\"";
57+
headerValue += $", resource_metadata=\"{rawPrmDocumentUri}\"";
5258

5359
Response.Headers["WWW-Authenticate"] = headerValue;
5460

5561
// Store the resource_metadata in properties in case other handlers need it
56-
properties.Items["resource_metadata"] = metadataUrl;
62+
properties.Items["resource_metadata"] = rawPrmDocumentUri;
5763

5864
return base.HandleChallengeAsync(properties);
5965
}

0 commit comments

Comments
 (0)