Document and demonstrate allowed hosts and CORS policy guidance (#1554)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Stephen Halter <halter73@gmail.com>
This commit is contained in:
parent
0781beafa0
commit
2c5d1ed2be
7 changed files with 288 additions and 9 deletions
|
|
@ -6,5 +6,5 @@
|
|||
"AspNetCoreMcpPerSessionTools": "Debug"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
"AllowedHosts": "localhost;127.0.0.1;[::1]"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,23 @@ using AspNetCoreMcpServer.Resources;
|
|||
using System.Net.Http.Headers;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
var allowedOrigins = builder.Configuration.GetSection("Mcp:AllowedOrigins").Get<string[]>() ?? ["http://localhost:5173"];
|
||||
|
||||
// Only enable CORS if you intentionally want browser-based cross-origin access to this server.
|
||||
// Keep the allowlist narrowly scoped to known origins. Broad CORS settings weaken security.
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("McpBrowserClient", policy =>
|
||||
{
|
||||
policy.WithOrigins(allowedOrigins)
|
||||
.WithMethods("GET", "POST", "DELETE")
|
||||
// Browsers can send Accept without extra CORS configuration. These are the MCP-specific
|
||||
// and non-safelisted headers the browser-based client needs for stateful Streamable HTTP.
|
||||
.WithHeaders("Content-Type", "MCP-Protocol-Version", "Mcp-Session-Id")
|
||||
.WithExposedHeaders("Mcp-Session-Id");
|
||||
});
|
||||
});
|
||||
|
||||
// Note: This sample uses SampleLlmTool which calls server.AsSamplingChatClient() to send
|
||||
// a server-to-client sampling request. This requires stateful (session-based) mode. Set
|
||||
// Stateless = false explicitly for forward compatibility in case the default changes.
|
||||
|
|
@ -36,6 +53,7 @@ builder.Services.AddHttpClient("WeatherApi", client =>
|
|||
|
||||
var app = builder.Build();
|
||||
|
||||
app.MapMcp();
|
||||
app.UseCors();
|
||||
app.MapMcp().RequireCors("McpBrowserClient");
|
||||
|
||||
app.Run();
|
||||
|
|
|
|||
|
|
@ -5,5 +5,10 @@
|
|||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "localhost;127.0.0.1;[::1]",
|
||||
"Mcp": {
|
||||
"AllowedOrigins": [
|
||||
"http://localhost:5173"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,5 +5,5 @@
|
|||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "localhost;127.0.0.1;[::1]"
|
||||
}
|
||||
|
|
|
|||
9
samples/LongRunningTasks/appsettings.json
Normal file
9
samples/LongRunningTasks/appsettings.json
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "localhost;127.0.0.1;[::1]"
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using ModelContextProtocol.AspNetCore.Authentication;
|
||||
using ProtectedMcpServer.Tools;
|
||||
using System.Net.Http.Headers;
|
||||
|
|
@ -9,6 +10,26 @@ var builder = WebApplication.CreateBuilder(args);
|
|||
|
||||
var serverUrl = "http://localhost:7071/";
|
||||
var inMemoryOAuthServerUrl = "https://localhost:7029";
|
||||
var allowedOrigins = builder.Configuration.GetSection("Mcp:AllowedOrigins").Get<string[]>() ?? ["http://localhost:5173"];
|
||||
|
||||
// This sample runs the MCP server on localhost:7071, and it is intended to be callable from a
|
||||
// companion web app running on a different host (localhost:5173), while preventing requests from
|
||||
// other origins. This scenario requires enabling CORS and enabling a restrictive CORS policy.
|
||||
//
|
||||
// If your server is not meant for cross-origin browser access, leave CORS disabled.
|
||||
//
|
||||
// Only apply a lenient CORS policy if your server is intended to be callable from any browser.
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("McpBrowserClient", policy =>
|
||||
{
|
||||
policy.WithOrigins(allowedOrigins)
|
||||
.WithMethods("POST")
|
||||
.WithHeaders(HeaderNames.ContentType, HeaderNames.Authorization, "MCP-Protocol-Version")
|
||||
.WithExposedHeaders(HeaderNames.WWWAuthenticate);
|
||||
});
|
||||
});
|
||||
|
||||
builder.Services.AddAuthentication(options =>
|
||||
{
|
||||
|
|
@ -84,11 +105,12 @@ builder.Services.AddHttpClient("WeatherApi", client =>
|
|||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseCors();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
// Use the default MCP policy name that we've configured
|
||||
app.MapMcp().RequireAuthorization();
|
||||
app.MapMcp().RequireAuthorization().RequireCors("McpBrowserClient");
|
||||
|
||||
Console.WriteLine($"Starting MCP server with authorization at {serverUrl}");
|
||||
Console.WriteLine($"Using in-memory OAuth server at {inMemoryOAuthServerUrl}");
|
||||
|
|
|
|||
14
samples/ProtectedMcpServer/appsettings.json
Normal file
14
samples/ProtectedMcpServer/appsettings.json
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "localhost;127.0.0.1;[::1]",
|
||||
"Mcp": {
|
||||
"AllowedOrigins": [
|
||||
"http://localhost:5173"
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue