modelcontextprotocol / modelcontextprotocol/csharp-sdk
Authorize with specific JWT scheme and [Authorize(...)] attribute
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- C#
- Estrellas
- 4.5k
- Forks
- 814
- Merge medio
- 9 d 19 h
- PR fusionados (30 d)
- 4
Descripción
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 = ...)]
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con la reproducción en Startup.cs usando AddMcpServer(), AddAuthorizationFilters() y los esquemas JWT APItoken y MCPtoken. Rastrea cómo el transporte HTTP de MCP selecciona los esquemas de autenticación para [Authorize(AuthenticationSchemes = "MCPtoken")], y luego verifica que tools/list funcione correctamente con el token de MCP sin convertir MCPtoken en el valor predeterminado de toda la aplicación.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- csharp
- Área
- authentication
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Tranquilo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 45/100