|
| 1 | +using ModelContextProtocol.Protocol; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using System.Text.Json; |
| 4 | + |
| 5 | +namespace ModelContextProtocol.Server; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Provides constants and helper methods for building MCP Apps-enabled servers. |
| 9 | +/// </summary> |
| 10 | +/// <remarks> |
| 11 | +/// <para> |
| 12 | +/// MCP Apps is an extension to the Model Context Protocol that enables MCP servers to deliver |
| 13 | +/// interactive user interfaces — dashboards, forms, visualizations, and more — directly inside |
| 14 | +/// conversational AI clients. |
| 15 | +/// </para> |
| 16 | +/// <para> |
| 17 | +/// Use the constants in this class when populating the <c>extensions</c> capability and the |
| 18 | +/// <c>_meta</c> field of tools and resources. Use <see cref="GetUiCapability"/> to check whether |
| 19 | +/// the connected client supports the MCP Apps extension. |
| 20 | +/// </para> |
| 21 | +/// </remarks> |
| 22 | +public static class McpApps |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// The MIME type used for MCP App HTML resources. |
| 26 | + /// </summary> |
| 27 | + /// <remarks> |
| 28 | + /// This MIME type should be used when registering UI resources with |
| 29 | + /// <c>text/html;profile=mcp-app</c> to indicate they are MCP App resources. |
| 30 | + /// </remarks> |
| 31 | + public const string ResourceMimeType = "text/html;profile=mcp-app"; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// The extension identifier used for MCP Apps capability negotiation. |
| 35 | + /// </summary> |
| 36 | + /// <remarks> |
| 37 | + /// This key is used in the <see cref="ClientCapabilities.Extensions"/> and |
| 38 | + /// <see cref="ServerCapabilities.Extensions"/> dictionaries to advertise support for |
| 39 | + /// the MCP Apps extension. |
| 40 | + /// </remarks> |
| 41 | + public const string ExtensionId = "io.modelcontextprotocol/ui"; |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// The legacy flat <c>_meta</c> key for the UI resource URI. |
| 45 | + /// </summary> |
| 46 | + /// <remarks> |
| 47 | + /// <para> |
| 48 | + /// This key is used for backward compatibility with older MCP hosts that do not support |
| 49 | + /// the nested <c>_meta.ui</c> object. When populating UI metadata, both this key and the |
| 50 | + /// <c>ui</c> object should be set to the same resource URI value. |
| 51 | + /// </para> |
| 52 | + /// <para> |
| 53 | + /// This key is considered legacy; prefer <see cref="McpUiToolMeta.ResourceUri"/> for new implementations. |
| 54 | + /// </para> |
| 55 | + /// </remarks> |
| 56 | + public const string ResourceUriMetaKey = "ui/resourceUri"; |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Gets the MCP Apps client capability, if advertised by the connected client. |
| 60 | + /// </summary> |
| 61 | + /// <param name="capabilities">The client capabilities received during the MCP initialize handshake.</param> |
| 62 | + /// <returns> |
| 63 | + /// A <see cref="McpUiClientCapabilities"/> instance if the client advertises support for the MCP Apps extension; |
| 64 | + /// otherwise, <see langword="null"/>. |
| 65 | + /// </returns> |
| 66 | + /// <remarks> |
| 67 | + /// Use this method to determine whether the connected client supports the MCP Apps extension |
| 68 | + /// and to read the client's supported MIME types. |
| 69 | + /// </remarks> |
| 70 | + [Experimental(Experimentals.Apps_DiagnosticId, UrlFormat = Experimentals.Apps_Url)] |
| 71 | + public static McpUiClientCapabilities? GetUiCapability(ClientCapabilities? capabilities) |
| 72 | + { |
| 73 | + if (capabilities?.Extensions is not { } extensions || |
| 74 | + !extensions.TryGetValue(ExtensionId, out var value)) |
| 75 | + { |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + if (value is JsonElement element) |
| 80 | + { |
| 81 | + return element.ValueKind == JsonValueKind.Null ? null : |
| 82 | + JsonSerializer.Deserialize(element, McpJsonUtilities.JsonContext.Default.McpUiClientCapabilities); |
| 83 | + } |
| 84 | + |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Applies UI tool metadata to a <see cref="System.Text.Json.Nodes.JsonObject"/>, setting both the |
| 90 | + /// <c>ui</c> object key and the legacy <c>ui/resourceUri</c> flat key for backward compatibility. |
| 91 | + /// Keys already present in <paramref name="meta"/> are not overwritten. |
| 92 | + /// </summary> |
| 93 | + /// <param name="appUi">The UI tool metadata to apply.</param> |
| 94 | + /// <param name="meta">The <see cref="System.Text.Json.Nodes.JsonObject"/> to populate.</param> |
| 95 | + internal static void ApplyUiToolMetaToJsonObject(McpUiToolMeta appUi, System.Text.Json.Nodes.JsonObject meta) |
| 96 | + { |
| 97 | + // Populate the structured "ui" object if not already present. |
| 98 | + if (!meta.ContainsKey("ui")) |
| 99 | + { |
| 100 | + var uiNode = JsonSerializer.SerializeToNode(appUi, McpJsonUtilities.JsonContext.Default.McpUiToolMeta); |
| 101 | + if (uiNode is not null) |
| 102 | + { |
| 103 | + meta["ui"] = uiNode; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // Populate the legacy flat "ui/resourceUri" key if not already present. |
| 108 | + if (!meta.ContainsKey(ResourceUriMetaKey) && appUi.ResourceUri is not null) |
| 109 | + { |
| 110 | + meta[ResourceUriMetaKey] = appUi.ResourceUri; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments