Switchknife.McpGuard
0.1.0
dotnet add package Switchknife.McpGuard --version 0.1.0
NuGet\Install-Package Switchknife.McpGuard -Version 0.1.0
<PackageReference Include="Switchknife.McpGuard" Version="0.1.0" />
<PackageVersion Include="Switchknife.McpGuard" Version="0.1.0" />
<PackageReference Include="Switchknife.McpGuard" />
paket add Switchknife.McpGuard --version 0.1.0
#r "nuget: Switchknife.McpGuard, 0.1.0"
#:package Switchknife.McpGuard@0.1.0
#addin nuget:?package=Switchknife.McpGuard&version=0.1.0
#tool nuget:?package=Switchknife.McpGuard&version=0.1.0
Switchknife.McpGuard
Guards an MCP server on the streamable HTTP transport, or any endpoint that only non-browser clients call, with two checks in this order:
- Any
Originheader is refused with 403. A browser sends one on every cross-origin request and on every POST, so a page that reaches the server through DNS rebinding is turned away before anything else runs. MCP clients (agents, gateways, CLIs) send none. - The
Authorizationheader must be exactlyBearer {token}, compared in fixed time; anything else is 401. A null or empty token refuses every request, so a missing setting closes the endpoint instead of opening it.
builder.Services.AddMcpServer().WithHttpTransport(o => o.Stateless = true).WithTools<MyTools>();
var app = builder.Build();
app.MapMcp("/mcp").RequireMcpGuard(app.Configuration["Mcp:Token"]);
An endpoint authenticated by other means (an authentication scheme, a signature) keeps the Origin check alone:
app.MapMcp("/mcp").RequireAuthorization("read").RejectOrigin();
app.MapGroup("/approvals").RejectOrigin();
Both methods are endpoint conventions: they apply to an endpoint, a route group (every endpoint in it, including ones mapped later) or whatever MapMcp returns, and return the same builder for chaining. Refused requests get the status code and an empty body.
Limits:
- One static token. Rotate it by restarting with a new value; scopes or per-client tokens belong to an authentication scheme, with
RejectOriginbeside it. - The token's length is not hidden: the comparison is fixed-time for equal lengths only.
- No dependency on the MCP SDK; it guards whatever endpoint it is applied to.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0 | 67 | 10/13/2026 |