modelcontextprotocol / modelcontextprotocol/csharp-sdk
Authorize with specific JWT scheme and [Authorize(...)] attribute
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- C#
- Sterne
- 4.5k
- Forks
- 814
- Ø Merge
- 9 T. 19 Std.
- Gemergte PRs (30 T.)
- 4
Beschreibung
Describe the bug
I've added an MCP server to my API server. I need it to work with specific JWT tokens, configured differently from JWT tokens used by other API controllers. I've created 2 different JWT Schemes for "APItoken" and "MCPToken". But authoriztion does not work unless I force "MCPToken" scheme to be the default in Services.AddAuthentication(...).
To Reproduce
In Startup.cs:
// Server declaration
builder.Services.AddMcpServer()
.WithHttpTransport(options =>
{
options.PerSessionExecutionContext = false;
options.Stateless = true;
})
.AddAuthorizationFilters()
.WithToolsFromAssembly();
// Authentication schemes
var authBuilder = builder.Services.AddAuthentication();
authBuilder.AddJwtBearer("APItoken", options => { ... });
authBuilder.AddJwtBearer("MCPtoken", options => { ... });
In MCP server definition:
[McpServerToolType]
[Authorize(AuthenticationSchemes = "MCPtoken")]
public class McpTools
{
...
}
Expected behavior
Tools list call should return the defined tool when proper authorization bearer header is set.
Additional context
The only way to make it work is to add "MCPtoken" scheme as default:
var authBuilder = builder.Services.AddAuthentication("MCPtoken");
Other things I've tried:
- Adding"AddMcp()" to authBuilder and setting ForwardAuthenticate to "MCPtoken"
- Creating a policy with AddAuthenticationSchemes("MCPtoken") and requiring the policy with [Authorize(Policy = ...)]
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der Reproduktion in Startup.cs unter Verwendung von AddMcpServer(), AddAuthorizationFilters() und den JWT-Schemata APItoken und MCPtoken. Verfolge, wie der MCP-HTTP-Transport die Authentifizierungsschemata für [Authorize(AuthenticationSchemes = "MCPtoken")] auswählt, und überprüfe anschließend, dass tools/list mit dem MCP-Token erfolgreich ist, ohne MCPtoken zum anwendungsweiten Standard zu machen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- csharp
- Bereich
- authentication
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100