modelcontextprotocol / modelcontextprotocol/csharp-sdk

Authorize with specific JWT scheme and [Authorize(...)] attribute

オープン
#968 コメント 3 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

area-auth enhancement P2 ready for work
主要言語
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 = ...)]

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. 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

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。