Built-in server-side support for incremental scope consent (SEP-835)
メンテナーはふだん 2 日以内に返信
まだ誰も着手していません。
- #1483 @copilot-swe-agent による — マージされずにクローズ
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 38/100
- issue の種類
- 機能追加
- 明瞭さ
- 明確に書かれている
- 活発さ
- 停滞
- 技術スタック
- csharp
- 領域
- api, authorization, backend
調査の方向性
まず src/ModelContextProtocol.AspNetCore/AuthorizationFilterSetup.cs と StreamableHttpHandler.cs から始め、次に tests/ModelContextProtocol.AspNetCore.Tests/IncrementalConsentTests.cs の提案されたテストを確認します。承認済みのプリミティブが AddAuthorizationFilters なしでも表示されたままであり、未承認の呼び出しが指定された WWW-Authenticate ヘッダーを伴う HTTP 403 を返し、承認済みの呼び出しが成功し、既存のフィルター動作が変更されないことが完了の条件です。
索引モデルが issue の本文から書いたものです。
説明
Summary
Add built-in server-side support for incremental scope consent (SEP-835) so that MCP servers can use [Authorize] on tools, prompts, and resources without calling AddAuthorizationFilters(), and the SDK automatically returns HTTP 403 with proper WWW-Authenticate headers to trigger client re-authentication.
The SDK already has full client-side support via ClientOAuthProvider, which handles 403 insufficient_scope challenges. This issue covers the missing server-side half.
Motivation
Currently, servers that want per-primitive authorization must call AddAuthorizationFilters(), which:
- Hides
[Authorize]-decorated primitives from list results - Returns JSON-RPC errors (within HTTP 200) for unauthorized invocations
Neither behavior works for incremental scope consent, which requires:
- Primitives to be visible in listings so clients can discover them
- Unauthorized invocations to return HTTP 403 with
WWW-Authenticate: Bearer error="insufficient_scope", scope="...", resource_metadata="..."so the client can re-authenticate with broader scopes
Proposed Design
Usage Pattern
// Server setup — note: NO call to AddAuthorizationFilters()
builder.Services.AddMcpServer()
.WithHttpTransport()
.WithTools<MyTools>();
// Tool with scope requirement
[McpServerToolType]
public class MyTools
{
[McpServerTool, Description("Reads a file")]
[Authorize(Roles = "read_files")]
public static async Task<string> ReadFile(string path, CancellationToken ct)
{
// ...
}
}
How It Works
- Server decorates primitives with
[Authorize(Roles = "scope_name")]but does NOT callAddAuthorizationFilters() AddAuthorizationFilters()is unchanged — servers that use it get the existing hide/block behavior- Primitives with
[Authorize]are visible in listings (since the listing filter is not registered) - On invocation (
tools/call,prompts/get,resources/read), a new pre-flight check inStreamableHttpHandlerevaluates authorization before SSE streaming begins - If unauthorized → HTTP 403 with
WWW-Authenticate: Bearer error="insufficient_scope", scope="scope_name", resource_metadata="..." - The client's
ClientOAuthProviderhandles the 403 → re-auth with new scope → retry
Why Pre-flight?
The auth check must happen before InitializeSseResponse because SSE response headers are committed (HTTP 200) before the MCP filter pipeline runs. Once headers are committed, the status code cannot be changed to 403.
HandlePostRequestAsync (current):
InitializeSseResponse(context) // Sets Content-Type: text/event-stream
└─ session.Transport.HandlePostRequestAsync
└─ responseStream.FlushAsync() // ← COMMITS HEADERS AS 200
MessageWriter.WriteAsync(message) // Queues for session processing
// Auth filter runs here — too late for HTTP 403
The pre-flight check inserts before InitializeSseResponse:
HandlePostRequestAsync (proposed):
parse message, get session
TryHandleInsufficientScopeAsync(...) // NEW — can still write HTTP 403
InitializeSseResponse(context) // Only reached if auth passed
session.Transport.HandlePostRequestAsync(...)
Implementation Plan
1. Relax Check filters in AuthorizationFilterSetup
The PostConfigure method registers "Check" filters that throw InvalidOperationException if auth metadata exists but AddAuthorizationFilters() wasn't called. These must be updated to allow the incremental consent path (HTTP transport handles auth at the HTTP level instead).
File: src/ModelContextProtocol.AspNetCore/AuthorizationFilterSetup.cs
2. Pre-flight authorization in StreamableHttpHandler
Add TryHandleInsufficientScopeAsync in HandlePostRequestAsync:
- Skip if
AddAuthorizationFilterswas called (let it handle auth its way) - For
tools/call,prompts/get,resources/readrequests: parse target name, look up primitive - Check
IAuthorizeDatametadata, evaluateIAuthorizationService - If unauthorized: extract scope from
IAuthorizeData.Roles, write HTTP 403 withWWW-Authenticateheader and JSON-RPC error body
File: src/ModelContextProtocol.AspNetCore/StreamableHttpHandler.cs
3. Tests
[Authorize]primitives visible in listings withoutAddAuthorizationFilters- Invocation returns HTTP 403 with proper
WWW-Authenticateheaders - Authenticated user with sufficient claims can invoke successfully
AddAuthorizationFiltersbehavior is unchanged
File: tests/ModelContextProtocol.AspNetCore.Tests/IncrementalConsentTests.cs (new)
4. Documentation
- XML doc comments on new/modified APIs
- Usage guide documenting the
[Authorize(Roles = "scope")]pattern
Key Advantages
- No new types or attributes — reuses standard
[Authorize(Roles = "...")] - No breaking changes —
AddAuthorizationFiltersis untouched - Clean separation —
AddAuthorizationFilters= hide/block model; omitting it = incremental consent model WithHttpTransport()auto-registers — no extra API calls needed- Works with existing client —
ClientOAuthProvideralready handles 403insufficient_scope
Related
- SEP-835: https://github.com/modelcontextprotocol/modelcontextprotocol/pull/835
- Original issue: #907 (closed — covered client-side; this covers server-side)
- Client-side implementation:
ClientOAuthProvider.ShouldRetryWithNewAccessTokenhandles 403insufficient_scope
- 主要言語
- C#
- スター
- 4.5k
- フォーク
- 814
- 平均マージ
- 9日 19時間
- マージ済み PR(30日)
- 4
環境構築
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
modelcontextprotocol/csharp-sdk のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
modelcontextprotocol/csharp-sdk#1867 ·
メンテナーはふだん 2 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
modelcontextprotocol/csharp-sdk#1840 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
modelcontextprotocol/csharp-sdk#1836 ·
メンテナーはふだん 2 日以内に返信
-
enhancement needs confirmation
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
modelcontextprotocol/csharp-sdk#678 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
enhancement needs confirmation P3 ready for work
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
modelcontextprotocol/csharp-sdk#515 · コメント 6 件 · リアクション 3 件 ·
メンテナーはふだん 2 日以内に返信
modelcontextprotocol/csharp-sdk の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
microsoft/onnxruntime-genai#2633 ·
メンテナーはふだん 1 日以内に返信
-
python triage
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
microsoft/semantic-kernel#14491 ·
メンテナーはふだん 2 日以内に返信
-
area:jobads-cv FE mvp P3
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
klasolsson81/jobbliggaren#1878 ·
メンテナーはふだん 1 日以内に返信
-
bug help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
SubtitleEdit/subtitleedit#15331 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信