Publish multi-version documentation site (#1727)

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: bb4b44f0-f505-49c5-9878-76e2b40c7a15
Copilot-Session: 8eac1204-10ae-4319-9333-aee5dd43dcc8
This commit is contained in:
Jeff Handley 2026-07-24 15:55:35 -07:00 • committed by GitHub
commit ae36026695
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -9,7 +9,7 @@ uid: identity
When building production MCP servers, you often need to know _who_ is calling a tool so you can enforce permissions, filter data, or audit access. The MCP C# SDK provides built-in support for propagating the caller's identity from the transport layer into your tool, prompt, and resource handlers — no custom headers or workarounds required.
## How Identity Flows Through the SDK
## How identity flows through the SDK
When a client sends a request over an authenticated HTTP transport (Streamable HTTP or SSE), the ASP.NET Core authentication middleware populates `HttpContext.User` with a `ClaimsPrincipal`. The SDK's transport layer automatically copies this `ClaimsPrincipal` into `JsonRpcMessage.Context.User`, which then flows through message filters, request filters, and finally into the handler or tool method.
@ -24,7 +24,7 @@ HTTP Request (with auth token)
This means you can access the authenticated user's identity at every stage of request processing.
## Direct `ClaimsPrincipal` Parameter Injection (Recommended)
## Direct `ClaimsPrincipal` parameter injection (recommended)
The simplest and recommended approach is to declare a `ClaimsPrincipal` parameter on your tool method. The SDK automatically injects the authenticated user without including it in the tool's input schema:
@ -56,7 +56,7 @@ public class UserAwarePrompts
}
```
### Why This Works
### Why this works
The SDK registers `ClaimsPrincipal` as one of the built-in services available during request processing. When a tool, prompt, or resource method declares a `ClaimsPrincipal` parameter, the SDK:
@ -66,7 +66,7 @@ The SDK registers `ClaimsPrincipal` as one of the built-in services available du
This behavior is transport-agnostic. For HTTP transports, the `ClaimsPrincipal` comes from ASP.NET Core authentication. For other transports (like stdio), it's `null` unless you set it explicitly via a message filter.
## Accessing Identity in Filters
## Accessing identity in filters
Both message filters and request-specific filters expose the user via `context.User`:
@ -87,7 +87,7 @@ services.AddMcpServer()
.WithTools<UserAwareTools>();
```
## Role-Based Access with `[Authorize]` Attributes
## Role-based access with `[Authorize]` attributes
For declarative authorization, you can use standard ASP.NET Core `[Authorize]` attributes on your tools, prompts, and resources. This requires calling `AddAuthorizationFilters()` during server configuration:
@ -134,7 +134,7 @@ When authorization fails, the SDK automatically:
For more details on authorization filters and their execution order, see [Filters](xref:filters).
## Using `IHttpContextAccessor` (HTTP-Only Alternative)
## Using `IHttpContextAccessor` (HTTP-only alternative)
If you need access to the full `HttpContext` (not just the user), you can inject `IHttpContextAccessor` into your tool class. This gives you access to HTTP headers, query strings, and other request metadata:
@ -158,7 +158,7 @@ public class HttpContextTools(IHttpContextAccessor contextAccessor)
For more details, including important caveats about stale `HttpContext` with the legacy SSE transport, see [HTTP Context](xref:httpcontext).
## Transport Considerations
## Transport considerations
| Transport | Identity Source | Notes |
| --- | --- | --- |
@ -166,7 +166,7 @@ For more details, including important caveats about stale `HttpContext` with the
| SSE | Same as Streamable HTTP, but the `HttpContext` is tied to the long-lived SSE connection. | The `ClaimsPrincipal` parameter injection still works correctly, but `IHttpContextAccessor` may return stale claims if the client's token was refreshed after the SSE connection was established. |
| Stdio | No built-in authentication. `ClaimsPrincipal` is `null` unless set via a message filter. | For process-level identity, you can set the user in a message filter based on environment variables or other process-level context. |
### Setting Identity for Stdio Transport
### Setting identity for stdio transport
For stdio-based servers where the caller's identity comes from the process environment rather than HTTP authentication, you can set the user in a message filter:
@ -188,7 +188,7 @@ services.AddMcpServer()
.WithTools<UserAwareTools>();
```
## Full Example: Protected HTTP Server
## Full example: protected HTTP server
For a complete example of an MCP server with JWT authentication, OAuth resource metadata, and protected tools, see the [ProtectedMcpServer sample](https://github.com/modelcontextprotocol/csharp-sdk/tree/main/samples/ProtectedMcpServer).