|
1 | 1 | using Microsoft.AspNetCore.Authorization; |
| 2 | +using Microsoft.Extensions.Caching.Distributed; |
2 | 3 | using Microsoft.Extensions.DependencyInjection.Extensions; |
3 | 4 | using Microsoft.Extensions.Options; |
4 | 5 | using ModelContextProtocol.AspNetCore; |
@@ -33,6 +34,7 @@ public static IMcpServerBuilder WithHttpTransport(this IMcpServerBuilder builder |
33 | 34 | builder.Services.AddHostedService<IdleTrackingBackgroundService>(); |
34 | 35 |
|
35 | 36 | builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IPostConfigureOptions<McpServerOptions>, AuthorizationFilterSetup>()); |
| 37 | + builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<HttpServerTransportOptions>, HttpServerTransportOptionsSetup>()); |
36 | 38 |
|
37 | 39 | if (configureOptions is not null) |
38 | 40 | { |
@@ -64,4 +66,37 @@ public static IMcpServerBuilder AddAuthorizationFilters(this IMcpServerBuilder b |
64 | 66 |
|
65 | 67 | return builder; |
66 | 68 | } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Registers a <see cref="DistributedCacheEventStreamStore"/> as the <see cref="ISseEventStreamStore"/> for SSE resumability. |
| 72 | + /// </summary> |
| 73 | + /// <param name="builder">The builder instance.</param> |
| 74 | + /// <param name="configureOptions">An optional action to configure <see cref="DistributedCacheEventStreamStoreOptions"/>.</param> |
| 75 | + /// <returns>The builder provided in <paramref name="builder"/>.</returns> |
| 76 | + /// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception> |
| 77 | + /// <remarks> |
| 78 | + /// <para> |
| 79 | + /// An <see cref="IDistributedCache"/> implementation must be registered in the service collection before calling this method. |
| 80 | + /// The registered cache is automatically assigned to <see cref="DistributedCacheEventStreamStoreOptions.Cache"/>. |
| 81 | + /// </para> |
| 82 | + /// <para> |
| 83 | + /// To use a specific <see cref="IDistributedCache"/> instance instead of the one registered in DI, |
| 84 | + /// set the <see cref="DistributedCacheEventStreamStoreOptions.Cache"/> property in the <paramref name="configureOptions"/> callback. |
| 85 | + /// </para> |
| 86 | + /// </remarks> |
| 87 | + public static IMcpServerBuilder WithDistributedCacheEventStreamStore(this IMcpServerBuilder builder, Action<DistributedCacheEventStreamStoreOptions>? configureOptions = null) |
| 88 | + { |
| 89 | + ArgumentNullException.ThrowIfNull(builder); |
| 90 | + |
| 91 | + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<DistributedCacheEventStreamStoreOptions>, DistributedCacheEventStreamStoreOptionsSetup>()); |
| 92 | + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IValidateOptions<DistributedCacheEventStreamStoreOptions>, DistributedCacheEventStreamStoreOptionsValidator>()); |
| 93 | + builder.Services.AddSingleton<ISseEventStreamStore, DistributedCacheEventStreamStore>(); |
| 94 | + |
| 95 | + if (configureOptions is not null) |
| 96 | + { |
| 97 | + builder.Services.Configure(configureOptions); |
| 98 | + } |
| 99 | + |
| 100 | + return builder; |
| 101 | + } |
67 | 102 | } |
0 commit comments