Skip to content

Commit 0aeec19

Browse files
committed
Cleanup
1 parent cbb5c36 commit 0aeec19

File tree

2 files changed

+11
-38
lines changed

2 files changed

+11
-38
lines changed

samples/ProtectedMCPClient/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ static async Task Main(string[] args)
1313

1414
var serverUrl = "http://localhost:7071/sse";
1515

16-
// Create a single HttpClient instance to be shared
1716
var httpClient = new HttpClient();
1817

19-
// Pass the HttpClient to the authorization provider
2018
var tokenProvider = new BasicOAuthAuthorizationProvider(
2119
new Uri(serverUrl),
2220
clientId: "6ad97b5f-7a7b-413f-8603-7a3517d4adb8",
@@ -60,6 +58,8 @@ static async Task Main(string[] args)
6058
"GetAlerts",
6159
new Dictionary<string, object?> { { "state", "WA" } }
6260
);
61+
62+
//var result = await client.CallToolAsync("GetAuthorizationInfo");
6363
Console.WriteLine("Result: " + result.Content[0].Text);
6464
Console.WriteLine();
6565
}

samples/ProtectedMCPServer/Program.cs

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,39 @@
1+
using System.Net.Http.Headers;
2+
using System.Security.Claims;
13
using Microsoft.AspNetCore.Authentication.JwtBearer;
24
using Microsoft.IdentityModel.Tokens;
35
using ModelContextProtocol.AspNetCore.Authentication;
46
using ModelContextProtocol.Types.Authentication;
57
using ProtectedMCPServer.Tools;
6-
using System.Net.Http.Headers;
7-
using System.Security.Claims;
88

99
var builder = WebApplication.CreateBuilder(args);
1010

11-
// Define Entra ID (Azure AD) configuration
12-
var tenantId = "a2213e1c-e51e-4304-9a0d-effe57f31655"; // This is the tenant ID from your existing configuration
11+
var serverUrl = "http://localhost:7071/";
12+
var tenantId = "a2213e1c-e51e-4304-9a0d-effe57f31655";
1313
var instance = "https://login.microsoftonline.com/";
1414

15-
// Configure authentication to use MCP for challenges and Entra ID JWT Bearer for token validation
1615
builder.Services.AddAuthentication(options =>
1716
{
1817
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
19-
options.DefaultChallengeScheme = McpAuthenticationDefaults.AuthenticationScheme; // Use MCP for challenges
18+
options.DefaultChallengeScheme = McpAuthenticationDefaults.AuthenticationScheme;
2019
})
2120
.AddJwtBearer(options =>
2221
{
23-
// Configure for Entra ID (Azure AD) token validation
2422
options.Authority = $"{instance}{tenantId}/v2.0";
2523
options.TokenValidationParameters = new TokenValidationParameters
2624
{
27-
// Configure validation parameters for Entra ID tokens
2825
ValidateIssuer = true,
2926
ValidateAudience = true,
3027
ValidateLifetime = true,
3128
ValidateIssuerSigningKey = true,
32-
33-
// Default audience - you should replace this with your actual app/API registration ID
3429
ValidAudience = "167b4284-3f92-4436-92ed-38b38f83ae08",
35-
36-
// This validates that tokens come from your Entra ID tenant
3730
ValidIssuer = $"{instance}{tenantId}/v2.0",
38-
39-
// These claims are used by the app for identity representation
4031
NameClaimType = "name",
4132
RoleClaimType = "roles"
4233
};
4334

44-
// Enable metadata-based issuer key retrieval
4535
options.MetadataAddress = $"{instance}{tenantId}/v2.0/.well-known/openid-configuration";
4636

47-
// Add development mode debug logging for token validation
4837
options.Events = new JwtBearerEvents
4938
{
5039
OnTokenValidated = context =>
@@ -85,16 +74,13 @@
8574
};
8675
});
8776

88-
// Add authorization services
8977
builder.Services.AddAuthorization(options =>
9078
{
91-
// Modify the MCP policy to include both MCP and JWT Bearer schemes
92-
// This ensures the bearer token is properly authenticated while maintaining MCP for challenges
9379
options.AddMcpPolicy(configurePolicy: builder =>
9480
builder.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme));
9581
});
9682

97-
// Configure MCP Server
83+
builder.Services.AddHttpContextAccessor();
9884
builder.Services.AddMcpServer()
9985
.WithTools<WeatherTools>()
10086
.WithHttpTransport();
@@ -113,21 +99,8 @@
11399

114100
app.MapMcp().RequireAuthorization(McpAuthenticationDefaults.AuthenticationScheme);
115101

116-
Console.WriteLine("Starting MCP server with authorization at http://localhost:7071");
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");
120-
121-
Console.WriteLine();
122-
Console.WriteLine("Entra ID (Azure AD) JWT token validation is configured");
123-
Console.WriteLine();
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!");
130-
Console.WriteLine();
102+
Console.WriteLine($"Starting MCP server with authorization at {serverUrl}");
103+
Console.WriteLine($"PRM Document URL: {serverUrl}.well-known/oauth-protected-resource");
131104
Console.WriteLine("Press Ctrl+C to stop the server");
132105

133-
app.Run("http://localhost:7071/");
106+
app.Run(serverUrl);

0 commit comments

Comments
 (0)