-
Notifications
You must be signed in to change notification settings - Fork 697
Add MCP Apps extension support (typed metadata, attribute, and helpers) #1484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
8
commits into
main
Choose a base branch
from
copilot/add-mcp-apps-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bb32d0e
Initial plan
Copilot 35e7e59
feat: Add MCP Apps extension support (F1-F3, F6, F7)
Copilot fccaa0a
Move MCP Apps support to its own package
mikekistler 5673bd7
Address review feedback: rename, move types, remove Core coupling
mikekistler 7753c53
Add WithMcpApps() builder extension for IMcpServerBuilder
mikekistler 7f54b8c
Add MCP Apps conceptual documentation
mikekistler b14455c
Merge branch 'main' into copilot/add-mcp-apps-support
mikekistler 5d38a5b
Add WeatherAppServer sample demonstrating MCP Apps extension
mikekistler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace ModelContextProtocol.Server; | ||
|
|
||
| /// <summary> | ||
| /// Specifies MCP Apps UI metadata for a tool method. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Apply this attribute alongside <see cref="McpServerToolAttribute"/> to associate an MCP Apps | ||
| /// UI resource with the tool. When processed, it populates the structured <c>_meta.ui</c> object | ||
| /// in the tool's metadata. | ||
| /// </para> | ||
| /// <para> | ||
| /// This attribute takes precedence over any raw <c>[McpMeta("ui", ...)]</c> attribute on the | ||
| /// same method. | ||
| /// </para> | ||
| /// </remarks> | ||
| /// <example> | ||
| /// <code language="csharp"> | ||
| /// [McpServerTool] | ||
| /// [McpAppUi(ResourceUri = "ui://weather/view.html")] | ||
| /// [Description("Get current weather for a location")] | ||
| /// public string GetWeather(string location) => ...; | ||
| /// | ||
| /// // Restrict visibility to model only: | ||
| /// [McpServerTool] | ||
| /// [McpAppUi(ResourceUri = "ui://weather/view.html", Visibility = [McpUiToolVisibility.Model])] | ||
| /// public string GetWeatherModelOnly(string location) => ...; | ||
| /// </code> | ||
| /// </example> | ||
| [AttributeUsage(AttributeTargets.Method)] | ||
| [Experimental(Experimentals.Apps_DiagnosticId, UrlFormat = Experimentals.Apps_Url)] | ||
| public sealed class McpAppUiAttribute : Attribute | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the URI of the UI resource associated with this tool. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This should be a <c>ui://</c> URI pointing to the HTML resource registered | ||
| /// with the server (e.g., <c>"ui://weather/view.html"</c>). | ||
| /// </remarks> | ||
| public string? ResourceUri { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the visibility of the tool, controlling which principals can invoke it. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Allowed values are <see cref="McpUiToolVisibility.Model"/> and <see cref="McpUiToolVisibility.App"/>. | ||
| /// When <see langword="null"/> or empty, the tool is visible to both the model and the app (the default). | ||
| /// </para> | ||
| /// </remarks> | ||
| public string[]? Visibility { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using ModelContextProtocol.Protocol; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Text.Json; | ||
|
|
||
| namespace ModelContextProtocol.Server; | ||
|
|
||
| /// <summary> | ||
| /// Internal helper methods for MCP Apps integration within the Core package. | ||
| /// The public MCP Apps API surface is in the ModelContextProtocol.ExtApps package. | ||
| /// </summary> | ||
| internal static class McpAppsInternal | ||
| { | ||
| /// <summary> | ||
| /// Applies UI tool metadata to a <see cref="System.Text.Json.Nodes.JsonObject"/>, setting the | ||
| /// <c>ui</c> object key if not already present. | ||
| /// </summary> | ||
| /// <param name="appUi">The UI tool metadata to apply.</param> | ||
| /// <param name="meta">The <see cref="System.Text.Json.Nodes.JsonObject"/> to populate.</param> | ||
| internal static void ApplyUiToolMetaToJsonObject(McpUiToolMeta appUi, System.Text.Json.Nodes.JsonObject meta) | ||
| { | ||
| // Populate the structured "ui" object if not already present. | ||
| if (!meta.ContainsKey("ui")) | ||
| { | ||
| var uiNode = JsonSerializer.SerializeToNode(appUi, McpJsonUtilities.JsonContext.Default.McpUiToolMeta); | ||
| if (uiNode is not null) | ||
| { | ||
| meta["ui"] = uiNode; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/ModelContextProtocol.Core/Server/McpUiClientCapabilities.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace ModelContextProtocol.Server; | ||
|
|
||
| /// <summary> | ||
| /// Represents the MCP Apps capabilities advertised by a client. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// This object is the value associated with the <c>"io.modelcontextprotocol/ui"</c> key in the | ||
| /// <see cref="Protocol.ClientCapabilities.Extensions"/> dictionary. | ||
| /// </para> | ||
| /// </remarks> | ||
| [Experimental(Experimentals.Apps_DiagnosticId, UrlFormat = Experimentals.Apps_Url)] | ||
| public sealed class McpUiClientCapabilities | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the list of MIME types supported by the client for MCP App UI resources. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// A client that supports MCP Apps must include <c>"text/html;profile=mcp-app"</c> in this list. | ||
| /// </remarks> | ||
| [JsonPropertyName("mimeTypes")] | ||
| public IList<string>? MimeTypes { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace ModelContextProtocol.Server; | ||
|
|
||
| /// <summary> | ||
| /// Represents the Content Security Policy (CSP) domain allowlists for an MCP Apps UI resource. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// These allowlists are used by the MCP host to construct the Content-Security-Policy HTTP header | ||
| /// for the sandboxed iframe that hosts the UI resource. | ||
| /// </para> | ||
| /// <para> | ||
| /// Each list contains origins (e.g., <c>"https://api.example.com"</c>) that are permitted for | ||
| /// the corresponding CSP directive. | ||
| /// </para> | ||
| /// </remarks> | ||
| [Experimental(Experimentals.Apps_DiagnosticId, UrlFormat = Experimentals.Apps_Url)] | ||
| public sealed class McpUiResourceCsp | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the list of origins allowed for fetch, XMLHttpRequest, WebSocket, and EventSource | ||
| /// connections (<c>connect-src</c> CSP directive). | ||
| /// </summary> | ||
| [JsonPropertyName("connectDomains")] | ||
| public IList<string>? ConnectDomains { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the list of origins allowed for loading scripts, stylesheets, images, and fonts | ||
| /// (<c>script-src</c>, <c>style-src</c>, <c>img-src</c>, <c>font-src</c> CSP directives). | ||
| /// </summary> | ||
| [JsonPropertyName("resourceDomains")] | ||
| public IList<string>? ResourceDomains { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the list of origins allowed for loading nested frames | ||
| /// (<c>frame-src</c> CSP directive). | ||
| /// </summary> | ||
| [JsonPropertyName("frameDomains")] | ||
| public IList<string>? FrameDomains { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the list of allowed base URIs | ||
| /// (<c>base-uri</c> CSP directive). | ||
| /// </summary> | ||
| [JsonPropertyName("baseUris")] | ||
| public IList<string>? BaseUris { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace ModelContextProtocol.Server; | ||
|
|
||
| /// <summary> | ||
| /// Represents the UI metadata associated with an MCP resource in the MCP Apps extension. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This metadata is placed under the <c>ui</c> key in the resource's <c>_meta</c> object. | ||
| /// It provides Content Security Policy (CSP) configuration, sandbox permissions, CORS origin, and | ||
| /// visual boundary preferences for the UI resource served by this MCP server. | ||
| /// </remarks> | ||
| [Experimental(Experimentals.Apps_DiagnosticId, UrlFormat = Experimentals.Apps_Url)] | ||
| public sealed class McpUiResourceMeta | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the Content Security Policy configuration for this resource. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Specifies the allowed origins for network requests, resource loads, and nested frames. | ||
| /// </remarks> | ||
| [JsonPropertyName("csp")] | ||
| public McpUiResourceCsp? Csp { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the sandbox permissions for this resource. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Controls which browser sandbox features the UI resource is allowed to use. | ||
| /// </remarks> | ||
| [JsonPropertyName("permissions")] | ||
| public McpUiResourcePermissions? Permissions { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the dedicated origin domain for this resource. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// When set, the host will serve the resource from this dedicated origin, | ||
| /// enabling OAuth flows and CORS without wildcard exceptions. | ||
| /// </remarks> | ||
| [JsonPropertyName("domain")] | ||
| public string? Domain { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a value indicating whether the host should render a visual border around the UI. | ||
| /// </summary> | ||
| [JsonPropertyName("prefersBorder")] | ||
| public bool? PrefersBorder { get; set; } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.