|
| 1 | +using ModelContextProtocol.Protocol; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | + |
| 4 | +namespace ModelContextProtocol.Server; |
| 5 | + |
| 6 | +#pragma warning disable MCPEXP002 // this collection is itself part of the experimental ResultOrAlternate seam |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Stores alternate-result filters keyed by JSON-RPC method name, so that augmenting a Core-dispatched |
| 10 | +/// method to return an alternate <see cref="Result"/> subtype does not require a new |
| 11 | +/// <c><Method>WithAlternateHandler</c>/<c><Method>WithAlternateFilters</c> pair per method. |
| 12 | +/// </summary> |
| 13 | +/// <remarks> |
| 14 | +/// <para> |
| 15 | +/// Each method may only be registered with a single (<c>TParams</c>, <c>TResult</c>) pair, matching the |
| 16 | +/// types Core dispatches that method with. Registering the same method with different types throws, because |
| 17 | +/// the mismatched filters could never be invoked. |
| 18 | +/// </para> |
| 19 | +/// <para> |
| 20 | +/// <see cref="McpRequestFilters.CallToolWithAlternateFilters"/> is a typed view over the |
| 21 | +/// <see cref="RequestMethods.ToolsCall"/> entry of this collection. |
| 22 | +/// </para> |
| 23 | +/// </remarks> |
| 24 | +[Experimental(Experimentals.Extensibility_DiagnosticId, UrlFormat = Experimentals.Extensibility_Url)] |
| 25 | +public sealed class AlternateResultFilterCollection |
| 26 | +{ |
| 27 | + private readonly Dictionary<string, Entry> _entries = new(StringComparer.Ordinal); |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Gets the filter list registered for <paramref name="method"/>, creating an empty one if the method |
| 31 | + /// has not been registered yet. |
| 32 | + /// </summary> |
| 33 | + /// <typeparam name="TParams">The request parameter type Core dispatches <paramref name="method"/> with.</typeparam> |
| 34 | + /// <typeparam name="TResult">The result type Core dispatches <paramref name="method"/> with.</typeparam> |
| 35 | + /// <param name="method">The JSON-RPC method name, for example <see cref="RequestMethods.PromptsGet"/>.</param> |
| 36 | + /// <returns>The mutable filter list for <paramref name="method"/>.</returns> |
| 37 | + /// <exception cref="InvalidOperationException"> |
| 38 | + /// <paramref name="method"/> was already registered with a different parameter or result type. |
| 39 | + /// </exception> |
| 40 | + public IList<McpRequestInvocationFilter<TParams, ResultOrAlternate<TResult>>> GetOrAdd<TParams, TResult>(string method) |
| 41 | + where TResult : Result |
| 42 | + { |
| 43 | + Throw.IfNullOrWhiteSpace(method); |
| 44 | + |
| 45 | + if (_entries.TryGetValue(method, out var entry)) |
| 46 | + { |
| 47 | + return Cast<TParams, TResult>(entry, method); |
| 48 | + } |
| 49 | + |
| 50 | + var filters = new List<McpRequestInvocationFilter<TParams, ResultOrAlternate<TResult>>>(); |
| 51 | + _entries[method] = new Entry(typeof(TParams), typeof(TResult), filters, () => filters.Count); |
| 52 | + return filters; |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Replaces the filter list registered for <paramref name="method"/>. |
| 57 | + /// </summary> |
| 58 | + /// <typeparam name="TParams">The request parameter type Core dispatches <paramref name="method"/> with.</typeparam> |
| 59 | + /// <typeparam name="TResult">The result type Core dispatches <paramref name="method"/> with.</typeparam> |
| 60 | + /// <param name="method">The JSON-RPC method name.</param> |
| 61 | + /// <param name="filters">The filter list to store.</param> |
| 62 | + public void Set<TParams, TResult>( |
| 63 | + string method, |
| 64 | + IList<McpRequestInvocationFilter<TParams, ResultOrAlternate<TResult>>> filters) |
| 65 | + where TResult : Result |
| 66 | + { |
| 67 | + Throw.IfNullOrWhiteSpace(method); |
| 68 | + Throw.IfNull(filters); |
| 69 | + |
| 70 | + _entries[method] = new Entry(typeof(TParams), typeof(TResult), filters, () => filters.Count); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Gets the method names that currently have at least one registered filter. |
| 75 | + /// </summary> |
| 76 | + internal IEnumerable<string> NonEmptyMethods |
| 77 | + { |
| 78 | + get |
| 79 | + { |
| 80 | + foreach (var entry in _entries) |
| 81 | + { |
| 82 | + if (entry.Value.Count > 0) |
| 83 | + { |
| 84 | + yield return entry.Key; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + internal bool TryGetNonEmpty<TParams, TResult>( |
| 91 | + string method, |
| 92 | + [NotNullWhen(true)] out IList<McpRequestInvocationFilter<TParams, ResultOrAlternate<TResult>>>? filters) |
| 93 | + where TResult : Result |
| 94 | + { |
| 95 | + if (_entries.TryGetValue(method, out var entry) && entry.Count > 0) |
| 96 | + { |
| 97 | + filters = Cast<TParams, TResult>(entry, method); |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + filters = null; |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + private static IList<McpRequestInvocationFilter<TParams, ResultOrAlternate<TResult>>> Cast<TParams, TResult>( |
| 106 | + Entry entry, |
| 107 | + string method) |
| 108 | + where TResult : Result |
| 109 | + { |
| 110 | + if (entry.Filters is IList<McpRequestInvocationFilter<TParams, ResultOrAlternate<TResult>>> typed) |
| 111 | + { |
| 112 | + return typed; |
| 113 | + } |
| 114 | + |
| 115 | + throw new InvalidOperationException( |
| 116 | + $"Alternate-result filters for method '{method}' were registered with " + |
| 117 | + $"({entry.ParamsType}, {entry.ResultType}), but ({typeof(TParams)}, {typeof(TResult)}) was requested. " + |
| 118 | + $"A method can only carry alternate-result filters for the types the server dispatches it with."); |
| 119 | + } |
| 120 | + |
| 121 | + private sealed class Entry(Type paramsType, Type resultType, object filters, Func<int> count) |
| 122 | + { |
| 123 | + public Type ParamsType { get; } = paramsType; |
| 124 | + |
| 125 | + public Type ResultType { get; } = resultType; |
| 126 | + |
| 127 | + public object Filters { get; } = filters; |
| 128 | + |
| 129 | + public int Count => count(); |
| 130 | + } |
| 131 | +} |
0 commit comments