modelcontextprotocol / modelcontextprotocol/csharp-sdk
Authorize with specific JWT scheme and [Authorize(...)] attribute
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 4.5k
- Forks
- 814
- Avg merge
- 9d 19h
- Merged PRs (30d)
- 4
Description
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 = ...)]
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Startup.cs reproduction using AddMcpServer(), AddAuthorizationFilters(), and the APItoken and MCPtoken JWT schemes. Trace how the MCP HTTP transport selects authentication schemes for [Authorize(AuthenticationSchemes = "MCPtoken")], then verify that tools/list succeeds with the MCP token without making MCPtoken the application-wide default.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- authentication
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100