Skip to content

Commit 1fd164c

Browse files
committed
Proper setup
1 parent 6aef68b commit 1fd164c

2 files changed

Lines changed: 25 additions & 19 deletions

File tree

src/ModelContextProtocol.AspNetCore/Auth/McpAuthenticationHandler.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,6 @@ protected override Task HandleChallengeAsync(AuthenticationProperties properties
5151
// Initialize properties if null
5252
properties ??= new AuthenticationProperties();
5353

54-
// Set up the resource URI if not already configured, using the current request as a fallback
55-
if (Options.ResourceMetadata.Resource == null)
56-
{
57-
Options.ResourceMetadata.Resource = new Uri($"{Request.Scheme}://{Request.Host}");
58-
}
59-
60-
// Configure the resource metadata service with our metadata
61-
_resourceMetadataService.ConfigureMetadata(metadata => {
62-
metadata.Resource = Options.ResourceMetadata.Resource;
63-
metadata.AuthorizationServers = Options.ResourceMetadata.AuthorizationServers;
64-
metadata.BearerMethodsSupported = Options.ResourceMetadata.BearerMethodsSupported;
65-
metadata.ScopesSupported = Options.ResourceMetadata.ScopesSupported;
66-
metadata.ResourceDocumentation = Options.ResourceMetadata.ResourceDocumentation;
67-
});
68-
6954
// Set the WWW-Authenticate header with the resource_metadata
7055
string headerValue = $"Bearer realm=\"{Scheme.Name}\"";
7156
headerValue += $", resource_metadata=\"{metadataUrl}\"";

src/ModelContextProtocol.AspNetCore/Auth/McpAuthorizationExtensions.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,36 @@ public static AuthenticationBuilder AddMcp(
4040
string displayName,
4141
Action<McpAuthenticationOptions>? configureOptions = null)
4242
{
43-
builder.Services.TryAddSingleton<ResourceMetadataService>();
43+
// Create options instance to pass to ResourceMetadataService
44+
var options = new McpAuthenticationOptions();
45+
configureOptions?.Invoke(options);
46+
47+
// Register ResourceMetadataService with options
48+
builder.Services.AddSingleton(sp => {
49+
var service = new ResourceMetadataService();
50+
51+
// Configure the service with the resource metadata from options
52+
service.ConfigureMetadata(metadata => {
53+
metadata.Resource = options.ResourceMetadata.Resource;
54+
metadata.AuthorizationServers = options.ResourceMetadata.AuthorizationServers;
55+
metadata.BearerMethodsSupported = options.ResourceMetadata.BearerMethodsSupported;
56+
metadata.ScopesSupported = options.ResourceMetadata.ScopesSupported;
57+
metadata.ResourceDocumentation = options.ResourceMetadata.ResourceDocumentation;
58+
});
59+
60+
return service;
61+
});
4462

45-
// Register the marker to indicate that MCP authorization is configured
46-
// This will be used by MapMcp to apply authorization to endpoints
4763
builder.Services.TryAddSingleton<McpAuthorizationMarker>();
4864

4965
return builder.AddScheme<McpAuthenticationOptions, McpAuthenticationHandler>(
5066
authenticationScheme,
5167
displayName,
52-
configureOptions ?? (options => { }));
68+
opt => {
69+
if (configureOptions != null)
70+
{
71+
configureOptions(opt);
72+
}
73+
});
5374
}
5475
}

0 commit comments

Comments
 (0)