Switchknife.McpGuard 0.1.0

dotnet add package Switchknife.McpGuard --version 0.1.0
                    
NuGet\Install-Package Switchknife.McpGuard -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Switchknife.McpGuard" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Switchknife.McpGuard" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Switchknife.McpGuard" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Switchknife.McpGuard --version 0.1.0
                    
#r "nuget: Switchknife.McpGuard, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Switchknife.McpGuard@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Switchknife.McpGuard&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Switchknife.McpGuard&version=0.1.0
                    
Install as a Cake Tool

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:

  1. Any Origin header 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.
  2. The Authorization header must be exactly Bearer {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 RejectOrigin beside 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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