modelcontextprotocol / modelcontextprotocol/csharp-sdk
Authorize with specific JWT scheme and [Authorize(...)] attribute
まだ誰も着手していません。
- 主要言語
- C#
- スター
- 4.5k
- フォーク
- 814
- 平均マージ
- 9日 19時間
- マージ済み PR(30日)
- 4
説明
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 = ...)]
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
AddMcpServer()、AddAuthorizationFilters()、および JWT スキームの APItoken と MCPtoken を使用して、Startup.cs の再現から始めます。MCP HTTP トランスポートが [Authorize(AuthenticationSchemes = "MCPtoken")] に対して認証スキームを選択する方法を追跡し、MCPtoken をアプリケーション全体のデフォルトにすることなく、MCP トークンで tools/list が成功することを確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- csharp
- 領域
- authentication
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100