modelcontextprotocol / modelcontextprotocol/csharp-sdk
Authorize with specific JWT scheme and [Authorize(...)] attribute
Nessuno ha ancora preso questa issue.
- Lingua principale
- C#
- Stelle
- 4.5k
- Fork
- 814
- Merge medio
- 9g 19h
- PR unite (30g)
- 4
Descrizione
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 = ...)]
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione in Startup.cs usando AddMcpServer(), AddAuthorizationFilters() e gli schemi JWT APItoken e MCPtoken. Traccia il modo in cui il trasporto HTTP MCP seleziona gli schemi di autenticazione per [Authorize(AuthenticationSchemes = "MCPtoken")], quindi verifica che tools/list abbia esito positivo con il token MCP senza rendere MCPtoken il valore predefinito a livello di applicazione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- csharp
- Ambito
- authentication
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100