Built-in server-side support for incremental scope consent (SEP-835)
维护者通常 2 天内回复
还没有人认领这个 Issue。
- #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 中提议的测试。完成的标准是:已授权的 primitives 在没有 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.6k
- 派生
- 817
- 平均合并
- 8 天 7 小时
- 30 天内合并 PR
- 3
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 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 个 reaction ·
维护者通常 2 天内回复
查看 modelcontextprotocol/csharp-sdk 的全部 Issue
相似的 Issue
-
area-ai untriaged
难度 2/5 1-3 小时 新手友好度 85/100
dotnet/extensions#7790 ·
维护者通常 1 天内回复
-
P2 testing
难度 1/5 1 小时以内 新手友好度 90/100
维护者通常 1 天内回复
-
area-Infrastructure-coreclr os-ios os-maccatalyst os-tvos untriaged
难度 2/5 1-3 小时 新手友好度 78/100
dotnet/runtime#134766 · 3 条评论 ·
维护者通常 1 天内回复
-
0 - Backlog Bug
难度 2/5 1-3 小时 新手友好度 88/100
BrighterCommand/Brighter#4444 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 88/100
维护者通常 1 天内回复