Class HttpMcpServerBuilderExtensions
- Namespace
- Microsoft.Extensions.DependencyInjection
- Assembly
- ModelContextProtocol.AspNetCore.dll
Provides methods for configuring HTTP MCP servers via dependency injection.
public static class HttpMcpServerBuilderExtensions
- Inheritance
-
HttpMcpServerBuilderExtensions
- Inherited Members
Methods
AddAuthorizationFilters(IMcpServerBuilder)
Adds authorization filters to support AuthorizeAttribute on MCP server tools, prompts, and resources. This method should always be called when using ASP.NET Core integration to ensure proper authorization support.
public static IMcpServerBuilder AddAuthorizationFilters(this IMcpServerBuilder builder)
Parameters
builderIMcpServerBuilderThe builder instance.
Returns
- IMcpServerBuilder
The builder provided in
builder.
Remarks
This method automatically configures authorization filters for all MCP server handlers. These filters respect authorization attributes such as AuthorizeAttribute and AllowAnonymousAttribute. Tool authorization runs in the alternate-result pipeline before the Tasks extension dispatches background execution, so an unauthorized tool call does not create a task. Each call to this method also adds an ordinary call-tool authorization checkpoint at that point in the filter pipeline. Call this method again after any call-tool filter that changes the matched tool or user to authorize the replacement using the updated context.
Exceptions
- ArgumentNullException
builderis null.
WithDistributedCacheEventStreamStore(IMcpServerBuilder, Action<DistributedCacheEventStreamStoreOptions>?)
Registers a DistributedCacheEventStreamStore as the ISseEventStreamStore for SSE resumability.
[Obsolete("Stateful Streamable HTTP mode is a back-compat-only escape hatch for legacy clients. Set HttpServerTransportOptions.Stateless = true (the default as of the 2026-07-28 protocol revision) for new code. See SEP-2567.", DiagnosticId = "MCP9006", UrlFormat = "https://github.com/modelcontextprotocol/csharp-sdk/blob/main/docs/list-of-diagnostics.md#obsolete-apis")]
public static IMcpServerBuilder WithDistributedCacheEventStreamStore(this IMcpServerBuilder builder, Action<DistributedCacheEventStreamStoreOptions>? configureOptions = null)
Parameters
builderIMcpServerBuilderThe builder instance.
configureOptionsAction<DistributedCacheEventStreamStoreOptions>An optional action to configure DistributedCacheEventStreamStoreOptions.
Returns
- IMcpServerBuilder
The builder provided in
builder.
Remarks
An IDistributedCache implementation must be registered in the service collection before calling this method. The registered cache is automatically assigned to Cache.
To use a specific IDistributedCache instance instead of the one registered in DI,
set the Cache property in the configureOptions callback.
Exceptions
- ArgumentNullException
builderis null.
WithHttpTransport(IMcpServerBuilder, Action<HttpServerTransportOptions>?)
Adds the services necessary for McpEndpointRouteBuilderExtensions.MapMcp to handle MCP requests and sessions using the MCP Streamable HTTP transport.
public static IMcpServerBuilder WithHttpTransport(this IMcpServerBuilder builder, Action<HttpServerTransportOptions>? configureOptions = null)
Parameters
builderIMcpServerBuilderThe builder instance.
configureOptionsAction<HttpServerTransportOptions>Configures options for the Streamable HTTP transport. This allows configuring per-session McpServerOptions and running logic before and after a session.
Returns
- IMcpServerBuilder
The builder provided in
builder.
Remarks
For more information on configuring the underlying HTTP server to control things like port binding and custom TLS certificates, see the Minimal APIs quick reference.
Exceptions
- ArgumentNullException
builderis null.