Skip to content

Document recommended pattern for role/identity propagation in MCP tool execution#1497

Open
Copilot wants to merge 3 commits intomainfrom
copilot/update-documentation-role-identity-propagation
Open

Document recommended pattern for role/identity propagation in MCP tool execution#1497
Copilot wants to merge 3 commits intomainfrom
copilot/update-documentation-role-identity-propagation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 3, 2026

Fixes #1452

The SDK has built-in ClaimsPrincipal propagation via JsonRpcMessage.Context.User and automatic parameter injection into tool/prompt/resource handlers, but this wasn't documented anywhere.

Adds docs/concepts/identity/identity.md covering:

  • Direct ClaimsPrincipal parameter injection — the recommended, transport-agnostic pattern already demonstrated by ClaimsPrincipalTools in tests
  • Identity flow diagram — HTTP auth middleware → transport → filters → handler
  • Filter-level access via context.User
  • Declarative authorization with [Authorize]/[AllowAnonymous] + AddAuthorizationFilters()
  • Transport comparison — Streamable HTTP vs SSE vs stdio, with a stdio message filter pattern for setting identity from process context
  • IHttpContextAccessor positioned as HTTP-only alternative with cross-ref to existing httpcontext doc
[McpServerToolType]
public class UserAwareTools
{
    [McpServerTool, Description("Returns a personalized greeting.")]
    public string Greet(ClaimsPrincipal? user, string message)
    {
        var userName = user?.Identity?.Name ?? "anonymous";
        return $"{userName}: {message}";
    }
}

Updates toc.yml and index.md to wire up navigation.

Create docs/concepts/identity/identity.md covering:
- How ClaimsPrincipal flows through the SDK pipeline
- Direct ClaimsPrincipal parameter injection (recommended)
- Accessing identity in filters
- Role-based access with [Authorize] attributes
- IHttpContextAccessor as HTTP-only alternative
- Transport considerations (Streamable HTTP, SSE, stdio)
- Setting identity for stdio via message filters

Update toc.yml and index.md to include the new page.

Agent-Logs-Url: https://github.com/modelcontextprotocol/csharp-sdk/sessions/4d418af2-a8c6-4d8d-8985-5bb54f93a8e9

Co-authored-by: jeffhandley <1031940+jeffhandley@users.noreply.github.com>
Copilot AI changed the title [WIP] Document recommended pattern for role/identity propagation in MCP tool execution Document recommended pattern for role/identity propagation in MCP tool execution Apr 3, 2026
Copilot AI requested a review from jeffhandley April 3, 2026 00:27
@jeffhandley jeffhandley marked this pull request as ready for review April 3, 2026 04:22
Copilot AI requested a review from jeffhandley April 3, 2026 04:23
@jeffhandley
Copy link
Copy Markdown
Contributor

Here's a set of tests generated from these docs that validated all of the captured behavior: 082c3b5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document recommended pattern for role/identity propagation in MCP tool execution

2 participants