|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | 4 | using System.Globalization; |
| 5 | +using System.Text.Json.Nodes; |
5 | 6 | using System.Web; |
6 | 7 | using Aspire.Cli.Backchannel; |
| 8 | +using Aspire.Dashboard.Model; |
7 | 9 | using Microsoft.Extensions.Logging; |
| 10 | +using Microsoft.Extensions.Logging.Abstractions; |
8 | 11 | using ModelContextProtocol; |
| 12 | +using ModelContextProtocol.Protocol; |
9 | 13 |
|
10 | 14 | namespace Aspire.Cli.Mcp.Tools; |
11 | 15 |
|
@@ -111,4 +115,116 @@ private static bool IsLocalhostTld(string host) |
111 | 115 |
|
112 | 116 | return null; |
113 | 117 | } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Checks whether a resource snapshot has the <c>resource.excludeFromMcp</c> property set to true. |
| 121 | + /// Resources with this property should be excluded from all MCP tool results. |
| 122 | + /// </summary> |
| 123 | + internal static bool IsExcludedFromMcp(ResourceSnapshot snapshot) |
| 124 | + { |
| 125 | + if (snapshot.Properties.TryGetValue(KnownProperties.Resource.ExcludeFromMcp, out var value) && value is not null) |
| 126 | + { |
| 127 | + if (value is JsonValue jsonValue) |
| 128 | + { |
| 129 | + if (jsonValue.TryGetValue<bool>(out var boolValue)) |
| 130 | + { |
| 131 | + return boolValue; |
| 132 | + } |
| 133 | + |
| 134 | + if (jsonValue.TryGetValue<string>(out var stringValue) && bool.TryParse(stringValue, out var parsedBool)) |
| 135 | + { |
| 136 | + return parsedBool; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// Gets the error message text for a resource that is excluded from MCP. |
| 146 | + /// </summary> |
| 147 | + internal static string GetResourceNotAvailableMessage(string resourceName) => |
| 148 | + $"Resource '{resourceName}' is not available."; |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// Gets resource snapshots from the backchannel and checks whether the specified resource is excluded from MCP. |
| 152 | + /// Returns an error <see cref="CallToolResult"/> if the resource is excluded, or <c>null</c> if it is not excluded. |
| 153 | + /// </summary> |
| 154 | + internal static async Task<CallToolResult?> CheckResourceExcludedAsync( |
| 155 | + IAuxiliaryBackchannelMonitor auxiliaryBackchannelMonitor, |
| 156 | + string resourceName, |
| 157 | + CancellationToken cancellationToken) |
| 158 | + { |
| 159 | + var excludedNames = await GetExcludedResourceNamesAsync(auxiliaryBackchannelMonitor, cancellationToken).ConfigureAwait(false); |
| 160 | + return CreateExcludedResult(excludedNames, resourceName); |
| 161 | + } |
| 162 | + |
| 163 | + /// <summary> |
| 164 | + /// Checks whether the specified resource is excluded from MCP using an existing connection. |
| 165 | + /// Returns an error <see cref="CallToolResult"/> if the resource is excluded, or <c>null</c> if it is not excluded. |
| 166 | + /// </summary> |
| 167 | + internal static async Task<CallToolResult?> CheckResourceExcludedAsync( |
| 168 | + IAppHostAuxiliaryBackchannel connection, |
| 169 | + string resourceName, |
| 170 | + CancellationToken cancellationToken) |
| 171 | + { |
| 172 | + var excludedNames = await GetExcludedResourceNamesAsync(connection, cancellationToken).ConfigureAwait(false); |
| 173 | + return CreateExcludedResult(excludedNames, resourceName); |
| 174 | + } |
| 175 | + |
| 176 | + private static CallToolResult? CreateExcludedResult(HashSet<string> excludedNames, string resourceName) |
| 177 | + { |
| 178 | + if (excludedNames.Contains(resourceName)) |
| 179 | + { |
| 180 | + return new CallToolResult |
| 181 | + { |
| 182 | + Content = [new TextContentBlock { Text = GetResourceNotAvailableMessage(resourceName) }], |
| 183 | + IsError = true |
| 184 | + }; |
| 185 | + } |
| 186 | + |
| 187 | + return null; |
| 188 | + } |
| 189 | + |
| 190 | + /// <summary> |
| 191 | + /// Gets the set of resource names that are excluded from MCP. |
| 192 | + /// </summary> |
| 193 | + internal static async Task<HashSet<string>> GetExcludedResourceNamesAsync( |
| 194 | + IAuxiliaryBackchannelMonitor auxiliaryBackchannelMonitor, |
| 195 | + CancellationToken cancellationToken) |
| 196 | + { |
| 197 | + var connection = await AppHostConnectionHelper.GetSelectedConnectionAsync(auxiliaryBackchannelMonitor, NullLogger.Instance, cancellationToken).ConfigureAwait(false); |
| 198 | + if (connection is null) |
| 199 | + { |
| 200 | + return []; |
| 201 | + } |
| 202 | + |
| 203 | + return await GetExcludedResourceNamesAsync(connection, cancellationToken).ConfigureAwait(false); |
| 204 | + } |
| 205 | + |
| 206 | + /// <summary> |
| 207 | + /// Gets the set of resource names that are excluded from MCP using an existing connection. |
| 208 | + /// </summary> |
| 209 | + internal static async Task<HashSet<string>> GetExcludedResourceNamesAsync( |
| 210 | + IAppHostAuxiliaryBackchannel connection, |
| 211 | + CancellationToken cancellationToken) |
| 212 | + { |
| 213 | + var snapshots = await connection.GetResourceSnapshotsAsync(includeHidden: true, cancellationToken).ConfigureAwait(false); |
| 214 | + var excludedNames = new HashSet<string>(StringComparers.ResourceName); |
| 215 | + |
| 216 | + foreach (var snapshot in snapshots) |
| 217 | + { |
| 218 | + if (IsExcludedFromMcp(snapshot)) |
| 219 | + { |
| 220 | + excludedNames.Add(snapshot.Name); |
| 221 | + if (snapshot.DisplayName is not null) |
| 222 | + { |
| 223 | + excludedNames.Add(snapshot.DisplayName); |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + return excludedNames; |
| 229 | + } |
114 | 230 | } |
0 commit comments