|
4 | 4 | using Microsoft.AspNetCore.Http; |
5 | 5 | using Microsoft.AspNetCore.WebUtilities; |
6 | 6 | using Microsoft.Extensions.DependencyInjection; |
| 7 | +using ModelContextProtocol; |
7 | 8 | using ModelContextProtocol.AspNetCore.Authentication; |
8 | 9 | using ModelContextProtocol.Authentication; |
9 | 10 | using ModelContextProtocol.Client; |
@@ -528,6 +529,47 @@ await Assert.ThrowsAsync<McpException>(() => McpClient.CreateAsync( |
528 | 529 | transport, loggerFactory: LoggerFactory, cancellationToken: TestContext.Current.CancellationToken)); |
529 | 530 | } |
530 | 531 |
|
| 532 | + [Fact] |
| 533 | + public async Task CannotAuthenticate_WhenProtectedResourceMetadataMissingResource() |
| 534 | + { |
| 535 | + TestOAuthServer.RequireResource = false; |
| 536 | + |
| 537 | + Builder.Services.Configure<McpAuthenticationOptions>(McpAuthenticationDefaults.AuthenticationScheme, options => |
| 538 | + { |
| 539 | + options.Events.OnResourceMetadataRequest = async context => |
| 540 | + { |
| 541 | + context.HandleResponse(); |
| 542 | + |
| 543 | + var metadata = new ProtectedResourceMetadata |
| 544 | + { |
| 545 | + AuthorizationServers = { new Uri(OAuthServerUrl) }, |
| 546 | + ScopesSupported = ["mcp:tools"], |
| 547 | + }; |
| 548 | + |
| 549 | + await Results.Json(metadata, McpJsonUtilities.DefaultOptions).ExecuteAsync(context.HttpContext); |
| 550 | + }; |
| 551 | + }); |
| 552 | + |
| 553 | + await using var app = await StartMcpServerAsync(); |
| 554 | + |
| 555 | + await using var transport = new HttpClientTransport(new() |
| 556 | + { |
| 557 | + Endpoint = new(McpServerUrl), |
| 558 | + OAuth = new() |
| 559 | + { |
| 560 | + ClientId = "demo-client", |
| 561 | + ClientSecret = "demo-secret", |
| 562 | + RedirectUri = new Uri("http://localhost:1179/callback"), |
| 563 | + AuthorizationRedirectDelegate = HandleAuthorizationUrlAsync, |
| 564 | + }, |
| 565 | + }, HttpClient, LoggerFactory); |
| 566 | + |
| 567 | + var ex = await Assert.ThrowsAsync<McpException>(() => McpClient.CreateAsync( |
| 568 | + transport, loggerFactory: LoggerFactory, cancellationToken: TestContext.Current.CancellationToken)); |
| 569 | + |
| 570 | + Assert.Contains("Resource URI in metadata", ex.Message); |
| 571 | + } |
| 572 | + |
531 | 573 | [Fact] |
532 | 574 | public async Task CanAuthenticate_WithAuthorizationServerPathInsertionMetadata() |
533 | 575 | { |
|
0 commit comments