|
1 | 1 | using Microsoft.AspNetCore.Authentication.JwtBearer; |
2 | 2 | using Microsoft.IdentityModel.Tokens; |
3 | 3 | using ModelContextProtocol.AspNetCore.Auth; |
| 4 | +using ModelContextProtocol.Auth.Types; |
4 | 5 | using ProtectedMCPServer.Tools; |
5 | 6 | using System.Net.Http.Headers; |
6 | 7 | using System.Security.Claims; |
|
67 | 68 | }) |
68 | 69 | .AddMcp(options => |
69 | 70 | { |
70 | | - // Configure the MCP authentication with the same Entra ID server |
71 | | - options.ResourceMetadata.AuthorizationServers.Add(new Uri($"{instance}{tenantId}/v2.0")); |
72 | | - options.ResourceMetadata.BearerMethodsSupported.Add("header"); |
73 | | - options.ResourceMetadata.ScopesSupported.AddRange(["api://167b4284-3f92-4436-92ed-38b38f83ae08/weather.read"]); |
74 | | - options.ResourceMetadata.ResourceDocumentation = new Uri("https://docs.example.com/api/weather"); |
| 71 | + options.ResourceMetadataProvider = context => |
| 72 | + { |
| 73 | + var metadata = new ProtectedResourceMetadata |
| 74 | + { |
| 75 | + BearerMethodsSupported = { "header" }, |
| 76 | + ResourceDocumentation = new Uri("https://docs.example.com/api/weather"), |
| 77 | + AuthorizationServers = { new Uri($"{instance}{tenantId}/v2.0") } |
| 78 | + }; |
| 79 | + |
| 80 | + metadata.ScopesSupported.AddRange(new[] { |
| 81 | + "api://167b4284-3f92-4436-92ed-38b38f83ae08/weather.read" |
| 82 | + }); |
| 83 | + |
| 84 | + return metadata; |
| 85 | + }; |
75 | 86 | }); |
76 | 87 |
|
77 | 88 | // Add authorization services |
|
104 | 115 |
|
105 | 116 | Console.WriteLine("Starting MCP server with authorization at http://localhost:7071"); |
106 | 117 | Console.WriteLine("PRM Document URL: http://localhost:7071/.well-known/oauth-protected-resource"); |
| 118 | +Console.WriteLine(" - This endpoint returns different metadata based on the client type!"); |
| 119 | +Console.WriteLine(" - Try with different User-Agent headers or add ?mobile query parameter"); |
107 | 120 |
|
108 | 121 | Console.WriteLine(); |
109 | 122 | Console.WriteLine("Entra ID (Azure AD) JWT token validation is configured"); |
110 | 123 | Console.WriteLine(); |
111 | | -Console.WriteLine("To test the server:"); |
112 | | -Console.WriteLine("1. Use an MCP client that supports OAuth flow with Microsoft Entra ID"); |
113 | | -Console.WriteLine("2. The client should obtain a token for audience: api://weather-api"); |
114 | | -Console.WriteLine("3. The token should be issued by Microsoft Entra ID tenant: " + tenantId); |
115 | | -Console.WriteLine("4. Include this token in the Authorization header of requests"); |
| 124 | +Console.WriteLine("To test the server with different client types:"); |
| 125 | +Console.WriteLine("1. Standard client: No special headers needed"); |
| 126 | +Console.WriteLine("2. Mobile client: Add 'mobile' in User-Agent or use ?mobile query parameter"); |
| 127 | +Console.WriteLine("3. Partner client: Include 'partner' in User-Agent or add X-Partner-API header"); |
| 128 | +Console.WriteLine(); |
| 129 | +Console.WriteLine("Each client type will receive different authorization requirements!"); |
116 | 130 | Console.WriteLine(); |
117 | 131 | Console.WriteLine("Press Ctrl+C to stop the server"); |
118 | 132 |
|
|
0 commit comments